SQL Server为每个产品获取唯一值

时间:2014-02-19 06:39:59

标签: sql-server unique-values

我正在尝试进行查询,其中不包含与其他产品具有相同折扣百分比的产品。我也想使用子查询。这就是我想出来的,但是我收到一条错误,说“子查询返回的值超过1。当子查询跟随=,!=,<,< =,>,>时,这是不允许的。 ; =或当子查询用作表达式时。“

SELECT ProductName,
 (SELECT DiscountPercent
 FROM Products
 GROUP BY DiscountPercent
 HAVING COUNT(DiscountPercent) = 1
 )
FROM Products

Fender Stratocaster 30.00
Gibson Les Paul 30.00
Gibson SG   52.00
Yamaha FG700S   38.00
Washburn D10S   0.00
Rodriguez Caballero 11  39.00
Fender Precision    30.00  
Hofner Icon 25.00
Ludwig 5-piece Drum Set with Cymbals    30.00
Tama 5-Piece Drum Set with Cymbals  15.00

2 个答案:

答案 0 :(得分:0)

试试这个,

select * from (
select *,row_number()over (partition by Discount order by ProductName)rn from @t
)t4 where rn=1

答案 1 :(得分:0)

SELECT 
[ProductName],
[DiscountPercent]
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY DiscountPercent ORDER BY ProductName) AS [ProductRank],
[ProductName],
[DiscountPercent]
FROM
[Products]
) AS Data
WHERE [ProductRank] = 1