1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
|
;with P as
(
SELECT ItemNumber, List, Contract, DateCode,
ROW_NUMBER() OVER( PARTITION BY ItemNumber ORDER BY DateCode DESC ) rn
FROM GSDatabase.dbo.Pricing
)
SELECT Combined.Territory, Combined.CustomerNo, Combined.ProductLine, Combined.ItemCode, Combined.List,Combined.SalesPrice,
t1."Jan Units" as Actual_JAN, t2."JAN UNITS" as Forecast_JAN, t1."FEB Units" as Actual_FEB, t2."FEB UNITS" as Forecast_FEB,
t1."Mar Units" as Actual_MAR, t2."Mar Units" as Forecast_MAR, t1."APR UNITS" as Actual_APR, t2."APR UNITS" as Forecast_APR,
t2."MAY UNITS" as Actual_MAY, t2."MAY UNITS" as Forecast_MAY, t1."JUN Units" as Actual_JUN, t2."JUN UNITS" as Forecast_JUN,
t1."Jul Units" as Actual_JUL, t2."JUL Units" as Forecast_JUL, t1."Aug Units" as Actual_AUG, t2."AUG Units" as Forecast_AUG,
t1."Sep Units" as Actual_Sep, t2."Sep Units" as Forecast_SEP, t1."OCT Units" as Actual_OCT, t2."OCT Units" as Forecast_OCT,
t1."NOV Units" as Actual_NOV, t2."NOV Units" as Forecast_NOV, t1."Dec Units" as Actual_Dec, t2."Dec Units" as Forecast_Dec
FROM (SELECT Territory, CustomerNo, ProductLine, ItemCode, p.List, CAST( 0 AS INT) AS SalesPrice FROM dbo.Tmp_Actual A inner Join P on A.ItemCode = P.ItemNumber and p.rn=1
UNION
SELECT Territory, CustomerNo, ProductLine, ItemCode, List, 2010_Sales_Price FROM dbo.Tmp_Forecast F inner join Product Pd on F.ItemCode = Pd.ItemNumber and pd.Quarter = 2010100
) AS Combined
Left join dbo.Tmp_Actual t1 on Combined.ItemCode = t1.ItemCode and Combined.CustomerNo = t1.CustomerNo and Combined.Territory = t1.Territory
Left Join dbo.Tmp_Forecast t2 on Combined.ItemCode = t2.ItemCode and Combined.CustomerNo = t2.CustomerNo and Combined.Territory = t2.Territory
GROUP BY Combined.Territory, Combined.CustomerNo, Combined.ProductLine, Combined.ItemCode, Combined.List,Combined.SalesPrice,
t1."Jan Units" ,t2."JAN UNITS", t1."FEB UNITS" ,t2."FEB UNITS", t1."Mar Units" ,t2."Mar Units", t1."APR UNITS", t2."APR UNITS", t1."MAY UNITS", t2."MAY UNITS",
t1."JUN UNITS" ,t2."JUN UNITS", t1."Jul Units" ,t2."Jul Units", t1."Aug Units" ,t2."Aug Units", t1."Sep Units", t2."Sep Units", t1."Oct Units", t2."Oct Units",
t1."Nov Units" ,t2."Nov Units", t1."Dec Units" ,t2."Dec Units"
Order by CustomerNo, ItemCode
|