1

我想替换 sql server 记录列表中重复的客户名称

下面是查询结果的样子...

CustomerName      ||      Product

CustomerA         ||      Product    X
CustomerA         ||      Product    y
CustomerA         ||      Product    z
CustomerB         ||      Product    i  
CustomerB         ||      Product    j  

我想得到如下结果...

CustomerName      ||      Product

CustomerA         ||      Product    X
"No Name"         ||      Product    y
"No Name"         ||      Product    z
CustomerB         ||      Product    i  
"No Name"         ||      Product    j  

请建议,实现上述结果的最佳方法是什么......

谢谢阿曼

4

1 回答 1

3

如果您希望或需要将其作为查询来执行,您可以使用该ROW_NUMBER()函数来确定每个客户的“第一个”产品是什么,哪些不是。在这里,我将“第一个”定义为具有词法上最小的产品名称(小提琴):

;WITH CustomerProducts AS
(
    SELECT CustomerName, Product, 
        ROW_NUMBER() OVER(PARTITION BY CustomerName ORDER BY Product) rn
    FROM Table1
)
SELECT CASE WHEN rn=1 THEN CustomerName ELSE '"Same Customer"' END CustomerName,
    Product
FROM CustomerProducts
ORDER BY CustomerProducts.CustomerName, Product
于 2013-08-14T07:21:22.007 回答