我正在尝试获得每周销售数量为零的商店数量,但我对分区功能的行进行了错误。
这是我的代码:
IF object_id('tempdb..#All', 'U') IS NOT NULL
DROP TABLE #All;
IF object_id('tempdb..#Zeros', 'U') IS NOT NULL
DROP TABLE #Zeros;
Select distinct [FiscalYr]
,[FiscalWk]
,[Store]
,[UPCCode]
,[ItemSku]
,[SumUnits]
,[SumSales]
,[ProdCatg]
,Description=Descripton
into #All
FROM [Scans].[dbo].[Scans] s
left outer join [Tableau].[dbo].[Umpqua_Dairy_Authorizations] a
on s.UPCCode= a.[UPC]
where FiscalYr = '2015'
and
Descripton = a.Descripton
SELECT * ,
ROW_NUMBER() OVER (PARTITION BY UPCCode ORDER BY FiscalWk) RN
FROM #All
where SumSales = 0
group by UPCCode,[FiscalYr]
,[FiscalWk]
,[Store]
,[ItemSku]
,[SumUnits]
,[SumSales]
,[ProdCatg]
,Description
现在它正在计算每个UPCCode的行数,因此第一个UPC有1-7行,第二个UPC有1-5行。我希望它看起来像这样:
FiscalWk UPCCode #StoresZeroSales 2 48500017753 7 3 48500017753 5
有谁知道怎么做?
答案 0 :(得分:0)
你根本不应该使用ROW_NUMBER
,这很简单COUNT
:
SELECT FiscalWk,
UPCCode,
COUNT(*) [#StoresZeroSales]
FROM #All
WHERE SumSales = 0
GROUP BY FiscalWk,
UPCCode;