如果找到重复的值,是否可以用空字符串替换行值?
例如
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。
答案 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