![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
Hi everybody, this is probably a lame question so I apologize in advance. In a table like this: Fld1, Fld2, Fld3 i launch a query like this: SELECT MAX(Fld2) FROM table GROUP BY Fld2; I would recover the value of Fld3 corresponding MAX(Fld2). In msAccess I would have done a sort of Fld2 and then a First(Fld3), but I know this is not valid in pgsql. Are there smarter commands (perhaps a subquery) other than doing a join with the same table? Thank you in advance. |
#2
| |||
| |||
|
|
Hi everybody, this is probably a lame question so I apologize in advance. In a table like this: Fld1, Fld2, Fld3 i launch a query like this: SELECT MAX(Fld2) FROM table GROUP BY Fld2; I would recover the value of Fld3 corresponding MAX(Fld2). In msAccess I would have done a sort of Fld2 and then a First(Fld3), but I know this is not valid in pgsql. Are there smarter commands (perhaps a subquery) other than doing a join with the same table? Thank you in advance. |
#3
| |||
| |||
|
|
In a table like this: Fld1, Fld2, Fld3 i launch a query like this: SELECT MAX(Fld2) FROM table GROUP BY Fld2; I would recover the value of Fld3 corresponding MAX(Fld2). |
|
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but I know this is not valid in pgsql. Are there smarter commands (perhaps a subquery) other than doing a join with the same table? |
#4
| |||
| |||
|
|
In a table like this: Fld1, Fld2, Fld3 i launch a query like this: SELECT MAX(Fld2) FROM table GROUP BY Fld2; I would recover the value of Fld3 corresponding MAX(Fld2). |
|
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but I know this is not valid in pgsql. Are there smarter commands (perhaps a subquery) other than doing a join with the same table? |
#5
| |||
| |||
|
|
In a table like this: Fld1, Fld2, Fld3 i launch a query like this: SELECT MAX(Fld2) FROM table GROUP BY Fld2; I would recover the value of Fld3 corresponding MAX(Fld2). |
|
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but I know this is not valid in pgsql. Are there smarter commands (perhaps a subquery) other than doing a join with the same table? |
#6
| |||
| |||
|
|
In a table like this: Fld1, Fld2, Fld3 i launch a query like this: SELECT MAX(Fld2) FROM table GROUP BY Fld2; I would recover the value of Fld3 corresponding MAX(Fld2). |
|
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but I know this is not valid in pgsql. Are there smarter commands (perhaps a subquery) other than doing a join with the same table? |
#7
| |||
| |||
|
|
In a table like this: Fld1, Fld2, Fld3 i launch a query like this: SELECT MAX(Fld2) FROM table GROUP BY Fld2; I would recover the value of Fld3 corresponding MAX(Fld2). |
|
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but I know this is not valid in pgsql. Are there smarter commands (perhaps a subquery) other than doing a join with the same table? |
#8
| |||
| |||
|
|
Another option is to use DISTINCT ON, so something like SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC; But it's rather ugly. As usual, test with your own data to see how its performance compares with other approaches. |
#9
| |||
| |||
|
|
Another option is to use DISTINCT ON, so something like SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC; But it's rather ugly. As usual, test with your own data to see how its performance compares with other approaches. |
#10
| |||
| |||
|
|
Another option is to use DISTINCT ON, so something like SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC; But it's rather ugly. As usual, test with your own data to see how its performance compares with other approaches. |
![]() |
| Thread Tools | |
| Display Modes | |
| |