我有一个商店的数百个品牌的销售清单,我想通过销售获得每个商店的100个品牌。
由于某些未知原因,每家商店仅返回99个品牌。 (在源表中,每个商店有超过900个品牌。)
这是我的疑问:
SELECT TOP (100) PERCENT SHOP, BRAND, SALES
FROM dbo.[DATA] AS D
WHERE (BRAND IN
(SELECT TOP (100) WITH TIES BRAND
FROM DATA
WHERE (SHOP= D.SHOP)
ORDER BY SALES DESC
)
)
ORDER BY BRAND, SALES DESC
出了什么问题?
答案 0 :(得分:0)
我认为您尝试做的事情可以通过以下查询来实现
SELECT Q.SHOP
, Q.BRAND
, Q.SALES
FROM (
SELECT SHOP
, BRAND
, SALES
, ROW_NUMBER() OVER (PARTITION BY SHOP ORDER BY SALES DESC) rn
FROM dbo.[DATA]
) Q
WHERE Q.rn <= 100
ORDER BY Q.BRAND, Q.SALES DESC