简体   繁体   中英

1 table, many columns, one column of duplicates, remove duplicates

I have a table that has over 30 columns that I need to query and one column has duplicate entries. How do I query the table and distinct by the one column without listing every column to display in the select.
for example -

Select * 
from Event
where subject_ID = 49
and date between '01-NOV-2019' and '14-NOV-2019'

This displays 100 rows but there should only be 50 rows and the case_id column is the only column to distinguish the duplicates. I need to see all columns in the table but don't want the duplicate case_id rows and really don't want to list out all columns in the select.

Tried

select distinct case_id, * 
from event
where subject_ID = 49
and date between '01-NOV-2019' and '14-NOV-2019'

didn't work.

I should clarify that every other row is a duplicate. Each set of data has 2 rows and the case_id is the only data that distinguishes the duplicates. Is there a way to say only display the odd numbered rows?

what's causing your distinct is your *, you can use row_number() and partition by case_id to get top per case_id

select * from 
    (Select row_number() over (partition by case_id order by case_id) rn, * from Event 
        where subject_ID = 49 and date between '01-NOV-2019' and '14-NOV-2019'
    ) t1 
where t1.rn = 1

If you have a column and want one row for each value, you can use row_number() . In the following code, case_id is this column:

Select *
from (select e.*,
             row_number() over (partition by case_id order by case_id) as seqnum
      from Event e
      where subject_ID = 49 and date between '01-NOV-2019' and '14-NOV-2019'
     ) e
where seqnum = 1;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM