给出以下结果集:
firstname lastname scheduled posted EventAdded
Student One 2014-04-28 00:00:00.000 2014-04-28 12:02:07.037 NULL
Student One 2014-04-28 00:00:00.000 2014-04-28 12:02:07.037 later
OtherKid Two 2014-04-28 00:00:00.000 2014-04-28 21:09:33.457 NULL
如何消除第一条记录?
如果学生有许多EventAddeds - 我们只想要EventAdded不为null的那个。 如果学生有许多非空的EventAdded - 我们想要EventAdded不为null的任何记录。 如果学生只有一条记录(不管EventAdded是否为空) - 我们想要记录。
我们想要的结果是:
firstname lastname scheduled posted EventAdded
Student One 2014-04-28 00:00:00.000 2014-04-28 12:02:07.037 later
OtherKid Two 2014-04-28 00:00:00.000 2014-04-28 21:09:33.457 NULL
我们尝试了比我们列出的更多的东西。
答案 0 :(得分:1)
如果要对查询执行此操作,可以使用row_number()
:
select firstname, lastname, scheduled, posted, eventadded
from (select rs.*,
row_number() over (partition by firstname, lastname
order by (case when eventadded is not null then 1 else 2 end)
) as seqnum
from ResultSet rs
) rs
where seqnum = 1;
答案 1 :(得分:1)
如果您有旧服务器(没有row_number()等)
select T.* -- replace by delete of course to whack 'em
from tab1 T
where exists (select * from tab1 TIN where
(TIN.lastname = T.lastname
and TIN.FirstName = T.FirstName and TIN.EventAdded is not null))
and T.EventAdded is null