SQL - select distinct only on one column [duplicate]
A very typical approach to this type of problem is to use row_number()
:
select t.*from (select t.*, row_number() over (partition by number order by id) as seqnum from t ) twhere seqnum = 1;
This is more generalizable than using a comparison to the minimum id. For instance, you can get a random row by using order by newid()
. You can select 2 rows by using where seqnum <= 2
.
Since you don't care, I chose the max ID for each number.
select tbl.* from tblinner join (select max(id) as maxID, number from tbl group by number) maxIDon maxID.maxID = tbl.id
Query Explanation
select tbl.* -- give me all the data from the base table (tbl) from tbl inner join ( -- only return rows in tbl which match this subquery select max(id) as maxID -- MAX (ie distinct) ID per GROUP BY below from tbl group by NUMBER -- how to group rows for the MAX aggregation ) maxID on maxID.maxID = tbl.id -- join condition ie only return rows in tbl -- whose ID is also a MAX ID for a given NUMBER
You will use the following query:
SELECT * FROM [table] GROUP BY NUMBER;
Where [table]
is the name of the table.
This provides a unique listing for the NUMBER
column however the other columns may be meaningless depending on the vendor implementation; which is to say they may not together correspond to a specific row or rows.