我目前正在制作一份大型报告,并且需要一些重要部分的帮助,而这些部分我已经讽刺了几个星期
我有一份报告根据一些事情计算数量 但是我想要做的是当“PREQuantity”列为空并且一年中的一周= 1时,从上一年的52周获得数量 我知道,案件陈述尚未完成,但基本上我需要报告在前一年的第52周强制执行,以取消优先顺序
SELECT DR.DivNo
,p.[ProductCode]
,p.ProductClass
,p.EmpNo
,[Description]
,[CGNo]
,[SCGNo]
,dr.Retail
,bd.[Buying Director]
INTO #Product
FROM [PRODUCT] p
LEFT JOIN [DIVRETAIL] DR ON p.ProductCode = DR.ProductCode
LEFT JOIN vwAllBuyingDirectors bd ON p.EmpNo = bd.bd_BuyingDirector
WHERE dr.ValidTo IS NULL
AND dr.DivNo NOT LIKE '8__'
GROUP BY DR.DivNo
,p.[ProductCode]
,p.ProductClass
,p.EmpNo
,bd.[Buying Director]
,[Description]
,[CGNo]
,[SCGNo]
,dr.Retail
--select * from #Product
SELECT [Datetime]
,[FirstDayOfWeek]
,[FirstDayOfMonth]
,c.YearWeek
,s.NoOfStores
,s.DivNo
INTO #stores
FROM [Calendar] c
LEFT JOIN Stores s ON c.FirstDayOfMonth = s.Validfrom
WHERE c.[Datetime] = firstdayofweek
--- GIVE STORE COUNT TO EACH DATETIME
SELECT s.DivNo
,c.[Datetime]
,c.WeekofYear
,c.FirstdayofWeek
,c.YearWeek
,s.NoOfStores
INTO #weeklystores
FROM .Calendar c
LEFT JOIN #stores s ON c.FirstDayOfWeek = s.FirstDayOfWeek
-- Join numbers from #stores according to first day of week. Each week onmly has the first day of the week's store count as a reference.
------------------------------------------PREVIOUS WEEK--------------------- --------------------
SELECT [Datetime]
,[FirstDayOflastWeek]
,[FirstDayOflastMonth]
,c.YearWeek
,s.NoOfStores
,s.DivNo
INTO #storesv2
FROM [Calendar] c
LEFT JOIN Stores s ON c.FirstDayOfMonth = s.Validfrom
WHERE c.[Datetime] = FirstDayOfWeek
--- GIVE STORE COUNT TO EACH DATETIME
SELECT s.DivNo
,c.[Datetime]
,c.WeekofYear
,c.FirstdayoflastWeek
,c.YearWeek
,s.NoOfStores
INTO #preweeklystores
FROM .Calendar c
LEFT JOIN #storesv2 s ON c.FirstDayOfWeek = s.FirstDayOfLastWeek
/*
ASSIGN STORE COUNTS AND WEEK NUMBERS TO POS DATA
*/
CREATE TABLE #salescore (
[DivNo] NVARCHAR(max)
,[ProductCode] INT
,[Description] NVARCHAR(max)
,[CGNo] NVARCHAR(max)
,[SCGNo] NVARCHAR(max)
,Retail DECIMAL(38, 2)
,[WeekOfYear] TINYINT
,FirstDayOfWeek DATETIME
,[Quantity] INT
,[Sales] DECIMAL(38, 2)
,[NoOfStores] INT
,[USW] DECIMAL(38, 0)
)
INSERT INTO #salescore
SELECT pos.DivNo
,pos.ProductCode
,[Description]
,[CGNo]
,[SCGNo]
,p.Retail
,w.WeekOfYear
,w.FirstDayOfWeek
,Sum(Quantity) [Quantity]
,sum(pos.Retail) [Retail2]
,w.NoOfStores
,(Sum(Quantity) / w.NoOfStores) USW
FROM .pos pos
LEFT JOIN #weeklystores w ON pos.PosDate = w.[DATETIME]
LEFT JOIN #Product p ON p.ProductCode = pos.ProductCode
AND p.DivNo = pos.DivNo
AND pos.DivNo = w.DivNo
WHERE pos.DivNo NOT LIKE '8__' /* Get rid of IRL Regions */
and w.YearWeek >2013
and w.YearWeek IN (
SELECT Item
FROM DataWarehouse.dbo.ufnSplit(@YEAR, ',')
)
AND p.EmpNo IN (
SELECT Item
FROM ufnSplit(@BD, ',')
)
AND p.CGNo IN (
SELECT Item
FROM .ufnSplit(@CGNo, ',')
)
AND p.SCGNo IN (
SELECT Item
FROM ufnSplit(@SCGNo, ',')
)
AND p.ProductClass IN (
SELECT Item
FROM ufnSplit(@ProductClass, ',')
)
AND p.ProductCode IN (
SELECT Item
FROM ufnSplit(@ProductCode, ',')
)
AND pos.DivNo IN (
SELECT Item
FROM ufnSplit(@Region, ',')
)
AND w.WeekOfYear IN (
SELECT Item
FROM ufnSplit(@WOY, ',')
)
AND w.WeekOfYear IS NOT NULL /* Get rid of pos before goldthorp opening day */
GROUP BY pos.DivNo
,pos.ProductCode
,w.WeekOfYear
,w.NoOfStores
,w.FirstDayOfWeek
,[Description]
,[CGNo]
,[SCGNo]
,p.Retail
----------------------------------------PreviousWeekSales---------------------------------------------
CREATE TABLE #presales (
[DivNo] NVARCHAR(max)
,[ProductCode] INT
,[Description] NVARCHAR(max)
,[CGNo] NVARCHAR(max)
,[SCGNo] NVARCHAR(max)
,Retail DECIMAL(38, 2)
,PrevWOY TINYINT
,FirstDayOfWeek DATETIME
,[PreQuantity] INT
,[Sales] DECIMAL(38, 2)
,[NoOfStores] INT
,[USW] DECIMAL(38, 0)
)
INSERT INTO #presales
SELECT pos.DivNo
,pos.ProductCode
,[Description]
,[CGNo]
,[SCGNo]
,p.Retail
,w.WeekOfYear as PrevWOY
,w.FirstDayOfLastWeek
,SUM(Quantity) Quantity
,sum(pos.Retail) [Retail3]
,w.NoOfStores
,(Sum(Quantity) / w.NoOfStores) USW
FROM .pos pos
LEFT JOIN #preweeklystores w ON pos.PosDate = w.[DATETIME]
LEFT JOIN #Product p ON p.ProductCode = pos.ProductCode
AND p.DivNo = pos.DivNo
AND pos.DivNo = w.DivNo
WHERE pos.DivNo NOT LIKE '8__' /* Get rid of IRL Regions */
and w.YearWeek >2013
and w.YearWeek IN (
SELECT Item
FROM .ufnSplit(@YEAR, ',')
)
AND p.EmpNo IN (
SELECT Item
FROM ufnSplit(@BD, ',')
)
AND p.CGNo IN (
SELECT Item
FROM ufnSplit(@CGNo, ',')
)
AND p.SCGNo IN (
SELECT Item
FROM ufnSplit(@SCGNo, ',')
)
AND p.ProductClass IN (
SELECT Item
FROM ufnSplit(@ProductClass, ',')
)
AND p.ProductCode IN (
SELECT Item
FROM ufnSplit(@ProductCode, ',')
)
AND pos.DivNo IN (
SELECT Item
FROM ufnSplit(@Region, ',')
)
--AND w.WeekOfYear IN (
-- SELECT Item
-- FROM ufnSplit(@WOY, ',')
-- )
AND w.WeekOfYear IS NOT NULL /* Get rid of pos before goldthorp opening day */
GROUP BY pos.DivNo
,pos.ProductCode
,w.WeekOfYear
,w.NoOfStores
,w.FirstDayOfLastWeek
,[Description]
,[CGNo]
,[SCGNo]
,p.Retail
CREATE TABLE #sales (
[DivNo] NVARCHAR(max)
,[ProductCode] INT
,[Description] NVARCHAR(max)
,[CGNo] NVARCHAR(max)
,[SCGNo] NVARCHAR(max)
,Retail DECIMAL(38, 2)
,[WeekOfYear] TINYINT
,FirstDayOfWeek DATETIME
,[PreQuantity] int
,[Quantity] INT
,[Sales] DECIMAL(38, 2)
,[NoOfStores] INT
,[USW] DECIMAL(38, 0)
)
INSERT INTO #sales
SELECT pos.DivNo
,pos.ProductCode
,p.Description
,p.CGNo
,p.SCGNo
,p.Retail
,pos.WeekOfYear
,pos.FirstDayOfWeek
,SUM(ps.PreQuantity)
,Sum(pos.Quantity) [Quantity]
,sum(pos.Sales) Sales
,pos.NoOfStores
,(Sum(pos.Quantity) / pos.NoOfStores) USW
FROM #salescore pos
LEFT JOIN #presales PS on (pos.WeekOfYear -1) = ps.PrevWOY and pos.ProductCode = ps.ProductCode and pos.DivNo= ps.DivNo
LEFT JOIN #Product p ON p.ProductCode = pos.ProductCode
AND p.DivNo = pos.DivNo
WHERE pos.DivNo NOT LIKE '8__' /* Get rid of IRL Regions */
Group By
pos.DivNo
,pos.ProductCode
,p.Description
,p.CGNo
,p.SCGNo
,p.Retail
,pos.WeekOfYear
,pos.FirstDayOfWeek
,pos.NoOfStores
SELECT *
FROM #Sales
ORDER BY 3 ASC
DROP TABLE #weeklystores
DROP TABLE #stores
DROP TABLE #storesv2
DROP TABLE #sales
DROP TABLE #Product
END
我按照@Abhay
的建议做了这个改变 ,case when Sum(PreQuantity)= null and pos.WeekOfYear = 1 then (Select Sum(PreQuantity) from #presales where WeekOfYear =52 and @year = @Year-1) else SUM(PreQuantity) end as quantity
它没有用
更改后
答案 0 :(得分:0)
我正在尝试根据我的理解完成您的案例陈述:
case when Sum(PreQuantity)= null and WeekOfYear = 1 then
(Select Sum(Quantity)= null from Sometable a where a.WeekOfYear =52 and a.year = Year-1)
else null end
注意: 假设您有一个列或可以派生一年的列
Sometable可以是多个表连接在一起,您可以从中计算总和(数量)
(年)-1将来自您原来的外部查询,即
FROM pos pos
LEFT JOIN #preweeklystores w ON pos.PosDate = w.[DATETIME]
LEFT JOIN #Product p ON p.ProductCode = pos.ProductCode
AND p.DivNo = pos.DivNo
AND pos.DivNo = w.DivNo
WHERE pos.DivNo NOT LIKE '8__' /* Get rid of IRL Regions */
and w.YearWeek >2013
如果你可以使用tablename的别名作为每列的前缀,我将能够给出一个确切的查询。另外,你有一个日期/年份列吗?