有没有办法从单列的输出中删除重复数据

btxsgosb  于 2022-10-03  发布在  其他
关注(0)|答案(1)|浏览(150)

有没有办法去掉重复的客户ID,而只把第一个客户ID留给每个客户?

declare @orderTable table (
customerid int,
orderAmt decimal(18,4)
)

insert @orderTable
values
(1,10),
(1,20),
(1,20),
(1,20),
(3,10),
(3,15),
(3,30),
(3,10)

select * from @orderTable
order by customerid

预期输出:

customerid  orderAmt
1   10.0000
    20.0000
    20.0000
    20.0000
3   10.0000
    15.0000
    30.0000
    10.0000
x33g5p2x

x33g5p2x1#

真正属于表示层。也就是说,您还需要一个适当的顺序来维护一个明确的顺序。

您可能希望将... order by (Select null) ...替换为... order by SomeCol ...

select customerid = case when row_number() over (partition by Customerid order by (Select null)) = 1 then customerid end 
      ,orderAmt
 From  @orderTable O
 order by O.customerid

结果

编辑-如果不希望为空

select customerid = concat('',case when row_number() over (partition by Customerid order by (Select null)) = 1 then customerid end)
      ,orderAmt
 From  @orderTable O

相关问题