i have 3 columns.
column_a, column_b, column_c
I am trying to get all the rows with making distinct only in column_a.
when i write
select distinct column_a,column_b,column_c
i think it gives me distinct pairs. So i have
value1 - a - b
value1 - a - c
I want to keep the distinct values of column_a following by column_b and column_c values cause i will create a table from that sql query and i want to add PK the column_a column.
I think the safest way is to use row_number()
:
select a, b, c
from (select t.*,
row_number() over (partition by a order by a) as seqnum
from t
) t
where seqnum = 1;
This will return an arbitrary row for each a
value, but a
will not be repeated in the result set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.