从sql查询输出中删除不同的记录

时间:2015-11-12 02:57:30

标签: sql sql-server

对于我的以下查询:

select sb.case, sb.Trace, sb.Amount, sp.edge, sp.UserId
from Budget sb join Sap sp 
     on sb.Trace= sp.Trace
where sb.Trace in 
(select Trace from Sap where sb.UserId in('R5','HB') 
order by sp.edge desc;

我的输出为:

case        trace       amount  edge        userId
111243557   41432048    95.00   21631781    HB
111243557   40828826    95.00   21631781    R5
111243551   40828819    194.00  21631779    R5
111243545   40828600    71.00   21631773    R5
111243545   41431960    71.00   21631773    HB

如何从输出中删除不同的行。 就像这里输出edge = 21631779是不同的,需要删除。 有人可以告诉我吗?

2 个答案:

答案 0 :(得分:0)

如果要输出多次出现的边,则可以使用窗口函数:

with cte as (
      select sb.case, sb.Trace, sb.Amount, sp.edge, sp.UserId,
             count(*) over (partition by sp.edge) as cnt
      from Budget sb join
           Sap sp 
           on sb.Trace = sp.Trace
      where sb.Trace in (select Trace from Sap where sb.UserId in ('R5', 'HB'))
     )
select cte.*
from cte
where cnt > 1
order by edge desc;

答案 1 :(得分:0)

按行顺序更改边缘的sp.edge

order by edge desc;