如果重复,则将行值替换为空字符串

时间:2015-10-15 02:10:10

标签: sql sql-server sql-server-2012

如果找到重复的值,是否可以用空字符串替换行值?

例如

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。

3 个答案:

答案 0 :(得分:2)

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

但是,你可以这样做:

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;

答案 1 :(得分:1)

使用$letter = 'D'; $letter++; echo $letter; //outputs E $letter--; echo $letter; //outputs E ROW_NUMBER

CASE

答案 2 :(得分:0)

ROW_NUMBER() helps to find duplicate record : 

 SELECT Productcode=( CASE
                           WHEN rn > 1 THEN ''
                           ELSE Productcode
                         END ),
           color
    FROM   (SELECT Row_number()
                     OVER (
                       partition BY productcode
                       ORDER BY productcode) AS rn,
                   *
            FROM   table)a 
相关问题