Question : T-SQL Union

The query Below has no Syntax error but it is not returning what I need

I am having a problem with the SalesPrice Column

This Statement returns a column with 0's pre filled: OK thats fine thats what I need since this column does not exist in the table
SELECT Territory, CustomerNo, ProductLine, ItemCode, p.List, CAST( 0 AS INT) AS SalesPrice FROM dbo.Tmp_Actual


This statement returns value 2010. This is not what I want
SELECT  Territory, CustomerNo, ProductLine, ItemCode, List, 2010_Sales_Price FROM dbo.Tmp_Forecast F

in this table there are values I want to return back from the table..

I created a dummy column on the first query because its a union and Columns must be equal. But I must be doing something wrong if its not letting me return the values that are in Forecast...

How can I combine both and have SalesPrice Return is true value instead of 2010

thanks!


Code Snippet:
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

Answer : T-SQL Union

You can try using UNION ALL instead which will treat the values separately.  If you can have duplicates within each of the queries just add back DISTINCT keyword there so you will get unique from each, but duplicates when appearing in both.

SELECT DISTINCT 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 ALL
SELECT DISTINCT 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

Random Solutions  
 
programming4us programming4us