如果重复,用空字符串替换行值 [英] Replace row value with empty string if duplicate

查看:32
本文介绍了如果重复,用空字符串替换行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果发现重复值,是否可以用空字符串替换行值?

is it possible to replace row value with empty string if duplicate value found?

例如

SELECT ProductCode, Color FROM Product

--------------------
ProductCode | Color
--------------------
   00A0B    |  Red
   00A0B    |  Blue
   00A0C    |  Red
   00A0C    |  Black
   00A0C    |  White
--------------------

--------------------
ProductCode | Color
--------------------
   00A0B    |  Red
            |  Blue
   00A0C    |  Red
            |  Black
            |  White
--------------------

我使用的是 SQL Server 2012.

I'm using SQL Server 2012.

推荐答案

通常,这种类型的转换最好在应用层完成,因为结果集不是SQL-ish".也就是说,排序对于理解行很重要.

Often, this type of transformation is better done at the application layer, because the result-set is not "SQL-ish". That is, the ordering is important for understanding the rows.

但是,您可以这样做:

select (case when row_number() over (partition by ProductCode order by (select NULL)) = 1
             then ProductCode
        end) as ProductCode
       Color
from Product
order by ProductCode;

这篇关于如果重复,用空字符串替换行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆