Reputation: 562
I have been dealing with this for quite sometime now and I could not be able get around this. It is like this, I have table with case_no
(alphanumeric), first_name
, last_name
and description
.
What I want do is to select only one row for each distinct first_name/last_name combination. So, for example
case_no | first_name | last_name
---------------------+----------------------------+------------------------
AAAAAAA | john | doe
BBBBBBB | Dog | dawg
CCCCCCC | Cat | kitten
DDDDDDD | john | doe
Given that example I want to retrieve the following rows:
case_no | first_name | last_name
---------------------+----------------------------+------------------------
BBBBBBB | Dog | dawg
CCCCCCC | Cat | kitten
DDDDDDD | john | doe
How do I do this?
Upvotes: 1
Views: 234
Reputation: 656241
In PostgreSQL:
SELECT DISTINCT ON (2, 3)
case_no, first_name, last_name
FROM tbl
ORDER BY 2, 3, case_no DESC NULLS LAST;
About DISTINCT ON
:
Difference to the version with max()
:
first_name, last_name
. You may or may not want that.max()
may be faster.Upvotes: 3
Reputation: 1980
Try this,
SELECT MAX(CASE_NO), FIRST_NAME, LAST_NAME
FROM TABLE
GROUP BY FIRST_NAME, LAST_NAME;
Upvotes: 6