aggregate function, newest date

maro

I have a table

date    name
2014-01-01  AAA
2014-01-01  BBB
2014-01-01  CCC
2014-01-01  DDD
2015-05-05  AAA
2016-09-09  AAA
2016-09-09  BBB

and I want to have only the newest information about every person:

date    name
2016-09-09  AAA
2016-09-09  BBB
2014-01-01  CCC
2014-01-01  DDD

I can simply write

SELECT MAX(date), name
FROM table1
GROUP BY name;

but if I want to add another column, it doesn't work

SELECT MAX(date), name, address
FROM table1
GROUP BY name;

(doesn't work)

SELECT MAX(date), name, address
FROM table1
GROUP BY name, address;

(it works, but I want to have only one record for one person)

How can I do it?

Zohar Peled

Assuming your dates for each name are unique, it's quite simple to solve using a derived table and join, like this:

SELECT maxdate, t.name, t.address
from table1 t
inner join
(
    SELECT MAX(date) as maxdate, name
    FROM table1
    GROUP BY name
) d on(t.date = d.maxdate and t.name = d.name)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sql conditional aggregate function with date type

From Dev

MongoDB find newest date in collection

From Dev

function return value of the newest member

From Dev

SQL concat rows with same ID and newest date

From Dev

Django date query from newest to oldest

From Dev

combine date and time and get newest datetime

From Dev

How do I sort a list by newest date?

From Dev

Copy the newest two files and append the date

From Dev

Copy the newest two files and append the date

From Dev

Rank a date from oldest to newest SQL

From Dev

Show the oldest or newest records without showing the date

From Dev

Access - Selecting unique rows with newest date

From Dev

combine date and time and get newest datetime

From Dev

How do I sort a list by newest date?

From Dev

Ubuntu wont function normally after newest update

From Dev

MongoTemplate aggregate - group by date

From Dev

Count and Aggregate Date in R

From Dev

Mongodb - Aggregate by date

From Dev

How to aggregate date by day

From Dev

Aggregate values by date

From Dev

Aggregate with a start and end of date

From Dev

Column "INVOICE.DATE_OF_ISSUE" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause

From Dev

Finding the date of the oldest and newest entity with a certain attribute in Datomic?

From Dev

Group array of objects by date and get newest in each day in Javascript

From Dev

Get the newest file from directory structure year/month/date/time

From Dev

SQL Server : select from duplicate columns where date newest

From Dev

Remove duplicate rows, and keep newest row based on date column

From Dev

How to make excel backup checker script get file with newest date

From Dev

current value of a column for every id according to the newest date

Related Related

  1. 1

    sql conditional aggregate function with date type

  2. 2

    MongoDB find newest date in collection

  3. 3

    function return value of the newest member

  4. 4

    SQL concat rows with same ID and newest date

  5. 5

    Django date query from newest to oldest

  6. 6

    combine date and time and get newest datetime

  7. 7

    How do I sort a list by newest date?

  8. 8

    Copy the newest two files and append the date

  9. 9

    Copy the newest two files and append the date

  10. 10

    Rank a date from oldest to newest SQL

  11. 11

    Show the oldest or newest records without showing the date

  12. 12

    Access - Selecting unique rows with newest date

  13. 13

    combine date and time and get newest datetime

  14. 14

    How do I sort a list by newest date?

  15. 15

    Ubuntu wont function normally after newest update

  16. 16

    MongoTemplate aggregate - group by date

  17. 17

    Count and Aggregate Date in R

  18. 18

    Mongodb - Aggregate by date

  19. 19

    How to aggregate date by day

  20. 20

    Aggregate values by date

  21. 21

    Aggregate with a start and end of date

  22. 22

    Column "INVOICE.DATE_OF_ISSUE" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause

  23. 23

    Finding the date of the oldest and newest entity with a certain attribute in Datomic?

  24. 24

    Group array of objects by date and get newest in each day in Javascript

  25. 25

    Get the newest file from directory structure year/month/date/time

  26. 26

    SQL Server : select from duplicate columns where date newest

  27. 27

    Remove duplicate rows, and keep newest row based on date column

  28. 28

    How to make excel backup checker script get file with newest date

  29. 29

    current value of a column for every id according to the newest date

HotTag

Archive