I have the query of defining new columns using values of a row. Now I want another column that will be the summation of the new columns. I tried this,
SET @SQL = '
SELECT SC.Name AS BUYER,
CASE WHEN PPC.FactoryName = ''ANANTA'' THEN OM.OrderQty END AS Unit1_Qty,
CASE WHEN PPC.FactoryName = ''ACWL'' THEN OM.OrderQty END AS Unit2_Qty,
CASE WHEN PPC.FactoryName = ''DNV'' THEN OM.OrderQty END AS Unit3_Qty,
CASE WHEN PPC.FactoryName = ''ANANTA'' THEN OM.OrderQty * FR.Rate END AS Unit1_Value,
CASE WHEN PPC.FactoryName = ''ACWL'' THEN OM.OrderQty * FR.Rate END AS Unit2_Value,
CASE WHEN PPC.FactoryName = ''DNV'' THEN OM.OrderQty * FR.Rate END AS Unit3_Value,
(Unit1_Qty + Unit2_Qty + Unit3_Qty) as TotalQty
FROM LineAllocation LA
INNER JOIN OrderMaster OM ON LA.OrderRef= OM.OrderRefID
INNER JOIN AmanOTS..FileRef FR ON FR.FileRefID = OM.FileRefID
INNER JOIN SystemManager..Contacts SC ON SC.Code=OM.BuyerCode
INNER JOIN SystemManager..ProductionProcessCostCenter PPC ON PPC.CostCenter= LA.Line
INNER JOIN OrderMasterCostBreakdown OCB ON OCB.OrderRefID= OM.OrderRefID
INNER JOIN SystemManager..ProductionProcess PP ON PP.ProcessID = PPC.ProcessId
where UseDate = '''+Cast(@MonthName As Varchar)+''' AND ProcessName =''Sewing''
'
Exec(@sql)
But it does not work And showing error for using the alias name.
Msg 207, Level 16, State 1, Line 10
Invalid column name 'Unit1_Qty'.Msg 207, Level 16, State 1, Line 10
Invalid column name 'Unit2_Qty'.Msg 207, Level 16, State 1, Line 10
Invalid column name 'Unit3_Qty'.
How can I do it?
One method to use the column aliases in the expression is with a common table expression. The example below also is a parameterized query instead of string concatenation.
SET @SQL = '
WITH
allocations AS (
SELECT SC.Name AS BUYER,
CASE WHEN PPC.FactoryName = ''ANANTA'' THEN OM.OrderQty END AS Unit1_Qty,
CASE WHEN PPC.FactoryName = ''ACWL'' THEN OM.OrderQty END AS Unit2_Qty,
CASE WHEN PPC.FactoryName = ''DNV'' THEN OM.OrderQty END AS Unit3_Qty,
CASE WHEN PPC.FactoryName = ''ANANTA'' THEN OM.OrderQty * FR.Rate END AS Unit1_Value,
CASE WHEN PPC.FactoryName = ''ACWL'' THEN OM.OrderQty * FR.Rate END AS Unit2_Value,
CASE WHEN PPC.FactoryName = ''DNV'' THEN OM.OrderQty * FR.Rate END AS Unit3_Value
FROM LineAllocation LA
INNER JOIN OrderMaster OM ON LA.OrderRef= OM.OrderRefID
INNER JOIN AmanOTS..FileRef FR ON FR.FileRefID = OM.FileRefID
INNER JOIN SystemManager..Contacts SC ON SC.Code=OM.BuyerCode
INNER JOIN SystemManager..ProductionProcessCostCenter PPC ON PPC.CostCenter= LA.Line
INNER JOIN OrderMasterCostBreakdown OCB ON OCB.OrderRefID= OM.OrderRefID
INNER JOIN SystemManager..ProductionProcess PP ON PP.ProcessID = PPC.ProcessId
where UseDate = @MonthName AND ProcessName =''Sewing''
)
SELECT
BUYER,
Unit1_Qty,
Unit2_Qty,
Unit3_Qty,
Unit1_Value,
Unit2_Value,
Unit3_Value,
(Unit1_Qty + Unit2_Qty + Unit3_Qty) as TotalQty
FROM allocations;'
EXEC sp_executesql
@sql
,N'@MonthName varchar(30)'
,@MonthName = @MonthName;
There's nothing complicated about it. For any given row, you will only have one qty and one value according to the factory name.
Factory Qty1 Qty2 Qty3 Val1 Val2 Val3
======= ==== ==== ==== ==== ==== ====
ANANTA 10 NULL NULL 12.5 NULL NULL
ACWL NULL 20 NULL NULL 22.5 NULL
DNV NULL NULL 30 NULL NULL 32.5
So just calculate the value indiscriminately at the end to get what you want:
...
CASE WHEN PPC.FactoryName = ''DNV'' THEN OM.OrderQty * FR.Rate END AS Unit3_Value,
OM.OrderQty * FR.Rate as TotalVal
...
You'll end up with something like this:
Factory Qty1 Qty2 Qty3 Val1 Val2 Val3 TotalVal
======= ==== ==== ==== ==== ==== ==== ========
ANANTA 10 NULL NULL 12.5 NULL NULL 12.5
ACWL NULL 20 NULL NULL 22.5 NULL 22.5
DNV NULL NULL 30 NULL NULL 32.5 32.5
Addendum: Just as a parting shot, if the three Valx fields may have values in any or all of them, then a summation field would look like this.
...
S1.OrderQty * FR.Rate END AS Unit1_Value,
S2.OrderQty * FR.Rate END AS Unit2_Value,
S3.OrderQty * FR.Rate END AS Unit3_Value,
( Nvl( S1.OrderQty, 0 )
+ Nvl( S2.OrderQty, 0 )
+ Nvl( S3.OrderQty, 0 )) * FR.Rate END AS TotalVal
Then the result would look like this:
... Qty1 Qty2 Qty3 Val1 Val2 Val3 TotalVal
=== ==== ==== ==== ==== ==== ==== ========
... 10 20 30 12.5 25.0 37.5 75.00
... 10 null 30 12.5 null 37.5 50.00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.