我有一个表,它有30多个需要查询的列,还有一个列有重复的条目。如何通过一个列查询表,而不列出要在select中显示的每个列。
例如 -
Select *
from Event
where subject_ID = 49
and date between '01-NOV-2019' and '14-NOV-2019'
这将显示100行,但应该只有50行,而case_id列是区分重复项的唯一列。我需要查看表中的所有列,但不需要重复的case_id行,也不想列出select中的所有列。
尝试过
select distinct case_id, *
from event
where subject_ID = 49
and date between '01-NOV-2019' and '14-NOV-2019'
没起作用。
我应该澄清,每一行都是重复的。每组数据有2行,case_id是唯一区分重复数据的数据。有没有一种方法可以说只显示奇数行?
发布于 2019-11-14 18:14:46
导致您的distinct
的是您的*,您可以使用row_number()
和partition by case_id
来获得每个case_id
的top
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
发布于 2019-11-14 18:24:29
如果您有一个列,并且希望每个值都有一行,则可以使用row_number()
。在下面的代码中,case_id
是这个列:
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;
https://stackoverflow.com/questions/58869360
复制相似问题