Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

For example

SELECT ProductCode, Color FROM Product

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

to

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

I'm using SQL Server 2012.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
293 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

But, you can do this as:

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...