Tsql 从表的列中删除重复值,而不删除表的行
我已经写了这个代码Tsql 从表的列中删除重复值,而不删除表的行,tsql,sql-server-2005,Tsql,Sql Server 2005,我已经写了这个代码 SELECT cast(isnull(x.hc,'') AS VARCHAR(500)) as A, Case When cast(isnull(p.dg,'') AS VARCHAR(500)) = '0' then '' else cast(isnull(p.dg,'') AS VARCHAR(500)) End as B, Case When cast(isnull(h.cno,'') as varchar(500)) = '0' then '' else
SELECT
cast(isnull(x.hc,'') AS VARCHAR(500)) as A,
Case
When cast(isnull(p.dg,'') AS VARCHAR(500)) = '0' then ''
else cast(isnull(p.dg,'') AS VARCHAR(500))
End as B,
Case
When cast(isnull(h.cno,'') as varchar(500)) = '0' then ''
else cast(isnull(h.cno,'') as varchar(500))
End as C, --added
cast(isnull(p.vol,0) as varchar(500)) as D,
cast(isnull(p.weigh,0) as varchar(500)) as E
FROM ship g
inner join S_M d on d.ship_id = g.ship_id
left join Pack h on h.ship_id = g.ship_id
left join pack_d p on p.pack_id = h.pack_id
left join comm x on x.comm_id = p.comm_id
WHERE g.ship_pk = @ship_pk
GROUP BY cast(isnull(x.hc,'') AS VARCHAR(500)), cast(isnull(p.dg,'') AS VARCHAR(500)), cast(isnull(h.cno,'') as varchar(500)), cast(isnull(p.vol,0) as varchar(500)), cast(isnull(p.weigh,0) as varchar(500))
这给了我一个输出
A B C D E
123 Yes AB456 26.34 28.45
123 Yes AB687 26.34 28.45
125 No AB794 22.58 24.36
125 No AB456 22.58 24.36
125 No AB687 22.58 24.36
我如何更改上面的代码,以便按如下方式获得输出
A B C D E
123 Yes AB456 26.34 28.45
AB687 26.34 28.45
125 No AB794 22.58 24.36
AB456 22.58 24.36
AB687 22.58 24.36
如何从表的前两列中删除重复值
如上图所示?
这里我没有删除任何行,只是删除重复的值并将该字段保留为空
我该怎么做 这将是非常复杂的sql编写,为什么不在程序方面这样做呢?你可以记住a和b的值,如果它们没有从以前的值更改,就不会显示它们。是的,我同意@Exception,这是一个格式问题而不是数据检索问题。为什么分组时不使用聚合而不使用distinct?你可以<当1然后b else“”结束时,code>case row_number()覆盖(按a、b顺序按a、b划分),a&b hanks Alex的。问题已解决:)