从表的列中删除重复的值而不删除表的行

时间:2012-09-17 14:37:26

标签: 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 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

如何从表格的前两列中删除重复值 如上图所示? 我没有在这里删除任何行,而只是删除重复的值并将该字段保留为空。

我该怎么做?

0 个答案:

没有答案