![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
color | date --------+------------ red | 2004-01-19 blue | 2004-05-24 red | 2004-04-12 blue | 2004-05-24 How do I select the most recent entry for 'red'? |
#3
| |||
| |||
|
|
Scott, On Nov 12, 2004, at 10:00 AM, Scott Frankel wrote: color | date --------+------------ red | 2004-01-19 blue | 2004-05-24 red | 2004-04-12 blue | 2004-05-24 How do I select the most recent entry for 'red'? SELECT color, MAX(date) FROM giventable WHERE color = 'red' -- omit this line if you'd like to see the latest date for each color GROUP BY color; OT hint: You might want to take a look at the list of PostgreSQL Keywords in the documentation and avoid using them (such as date) to help you avoid naming issues in the future. Hope this helps. Michael Glaesemann grzm myrealbox com ---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org) |
#4
| |||
| |||
|
|
Scott, On Nov 12, 2004, at 10:00 AM, Scott Frankel wrote: color | date --------+------------ red | 2004-01-19 blue | 2004-05-24 red | 2004-04-12 blue | 2004-05-24 How do I select the most recent entry for 'red'? SELECT color, MAX(date) FROM giventable WHERE color = 'red' -- omit this line if you'd like to see the latest date for each color GROUP BY color; OT hint: You might want to take a look at the list of PostgreSQL Keywords in the documentation and avoid using them (such as date) to help you avoid naming issues in the future. Hope this helps. Michael Glaesemann grzm myrealbox com ---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org) |
#5
| |||
| |||
|
|
Scott, On Nov 12, 2004, at 10:00 AM, Scott Frankel wrote: color | date --------+------------ red | 2004-01-19 blue | 2004-05-24 red | 2004-04-12 blue | 2004-05-24 How do I select the most recent entry for 'red'? SELECT color, MAX(date) FROM giventable WHERE color = 'red' -- omit this line if you'd like to see the latest date for each color GROUP BY color; |
|
OT hint: You might want to take a look at the list of PostgreSQL Keywords in the documentation and avoid using them (such as date) to help you avoid naming issues in the future. |
|
Hope this helps. Michael Glaesemann grzm myrealbox com ---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org) |
#6
| |||
| |||
|
|
On Nov 11, 2004, at 5:09 PM, Michael Glaesemann wrote: Scott, On Nov 12, 2004, at 10:00 AM, Scott Frankel wrote: color | date --------+------------ red | 2004-01-19 blue | 2004-05-24 red | 2004-04-12 blue | 2004-05-24 How do I select the most recent entry for 'red'? SELECT color, MAX(date) FROM giventable WHERE color = 'red' -- omit this line if you'd like to see the latest date for each color GROUP BY color; Unless I'm missing something, this returns every listing for color=red, in max order. |
#7
| |||
| |||
|
|
How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color | date --------+------------ red | 2004-01-19 blue | 2004-05-24 red | 2004-04-12 blue | 2004-05-24 How do I select the most recent entry for 'red'? |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
-----Original Message----- From: pgsql-general-owner (AT) postgresql (DOT) org [mailto gsql-general-owner (AT) postgresql (DOT) org] On Behalf Of Scott FrankelSent: Thursday, November 11, 2004 11:13 PM To: pgsql-general (AT) postgresql (DOT) org Subject: Re: [GENERAL] simple query question: return latest ORDER BY DESC LIMIT 1 is much simpler and more readable than a sub-query. Though the sub-query approach looks to be a good template for ensuring more accurate results by being more explicit. Thanks to all who responded! Scott SELECT * FROM colortable WHERE color = 'red' ORDER BY date DESC LIMIT 1; SELECT g.color, g.date, g.entered_by FROM giventable g WHERE g.color = 'red' AND g.date = (SELECT MAX(g2.date) FROM giventable g2 WHERE g2.color = g.color ) ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org |
#10
| |||
| |||
|
|
Scott Frankel wrote: ORDER BY DESC LIMIT 1 is much simpler and more readable than a sub-query. Though the sub-query approach looks to be a good template for ensuring more accurate results by being more explicit. |
|
If 2 days are equal for color red, you still would get 2 rows returned. |
![]() |
| Thread Tools | |
| Display Modes | |
| |