I am relatively new to SQL programming and have been working on the following script, I would like to insert the following case statement into latter script.
'Temp YTD GM%' = ROUND(CASE WHEN SUM([Temp Billing MTD])
= 0 THEN '0' ELSE SUM([Temp GM$ MTD] / [Temp Sales MTD])
END, 2)
I am using the alias (from the script below)to acomplish the above script. How can I do this without creating seperate query? Any help would be greatly appriciated.
SELECT DISTINCT
Specialty, Branch,
'Temp Hrs WTD Last WK' = SUM(CASE WHEN DATEPART(WW,
CURRENT_TIMESTAMP) = DATEPART(WW, Pay_Ending_Date)
THEN [Bill Hours] ELSE 0 END),
'Temp Sales WTD' = SUM(CASE WHEN DATEPART(WW,
CURRENT_TIMESTAMP) = DATEPART(WW, Pay_Ending_Date)
THEN [Bill Amount] ELSE 0 END),
[Temp Sales MTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
THEN [Bill Amount] ELSE 0 END),
[Temp Sales YTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date)
THEN [Bill Amount] ELSE 0 END),
'Temp GM$ Last WK' = SUM(CASE WHEN DATEPART(WW,
CURRENT_TIMESTAMP) = DATEPART(WW, Pay_Ending_Date)
THEN [Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden]) ELSE 0 END),
[Temp GM$ MTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
THEN [Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden]) ELSE 0 END),
[Temp GM$ YTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date)
THEN [Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden]) ELSE 0 END),
'Perm Sales WTD' = SUM(CASE WHEN DATEPART(WW,
CURRENT_TIMESTAMP) = DATEPART(WW, Pay_Ending_Date)
THEN [Perm_GM] ELSE 0 END),
[Perm Sales MTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
THEN [Perm_GM] ELSE 0 END),
[Perm Sales YTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) THEN [Perm_GM] ELSE 0 END),
'GMP MTD' = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
THEN ([Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden])) + Perm_GM ELSE 0 END),
'GMP YTD' = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date)
THEN ([Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden])) + Perm_GM ELSE 0 END),
[Perm Falloffs MTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
AND [Type] = 'C' THEN [Perm_GM] ELSE 0 END),
'Perm Falloffs YTD' = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
[Type] = 'C' THEN [Perm_Gm] ELSE 0 END)
FROM Master_Business
GROUP BY Specialty, Branch
ORDER BY Specialty DESC
'Temp YTD GM%' = ROUND(CASE WHEN SUM([Temp Billing MTD])
= 0 THEN '0' ELSE SUM([Temp GM$ MTD] / [Temp Sales MTD])
END, 2)
I am using the alias (from the script below)to acomplish the above script. How can I do this without creating seperate query? Any help would be greatly appriciated.
SELECT DISTINCT
Specialty, Branch,
'Temp Hrs WTD Last WK' = SUM(CASE WHEN DATEPART(WW,
CURRENT_TIMESTAMP) = DATEPART(WW, Pay_Ending_Date)
THEN [Bill Hours] ELSE 0 END),
'Temp Sales WTD' = SUM(CASE WHEN DATEPART(WW,
CURRENT_TIMESTAMP) = DATEPART(WW, Pay_Ending_Date)
THEN [Bill Amount] ELSE 0 END),
[Temp Sales MTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
THEN [Bill Amount] ELSE 0 END),
[Temp Sales YTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date)
THEN [Bill Amount] ELSE 0 END),
'Temp GM$ Last WK' = SUM(CASE WHEN DATEPART(WW,
CURRENT_TIMESTAMP) = DATEPART(WW, Pay_Ending_Date)
THEN [Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden]) ELSE 0 END),
[Temp GM$ MTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
THEN [Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden]) ELSE 0 END),
[Temp GM$ YTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date)
THEN [Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden]) ELSE 0 END),
'Perm Sales WTD' = SUM(CASE WHEN DATEPART(WW,
CURRENT_TIMESTAMP) = DATEPART(WW, Pay_Ending_Date)
THEN [Perm_GM] ELSE 0 END),
[Perm Sales MTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
THEN [Perm_GM] ELSE 0 END),
[Perm Sales YTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) THEN [Perm_GM] ELSE 0 END),
'GMP MTD' = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
THEN ([Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden])) + Perm_GM ELSE 0 END),
'GMP YTD' = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date)
THEN ([Bill Amount] - ([Gross Pay] + [Federal] + [State] + [WorkCompAmt]
+ [Burden])) + Perm_GM ELSE 0 END),
[Perm Falloffs MTD] = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
MONTH(CURRENT_TIMESTAMP) = MONTH(Pay_Ending_Date)
AND [Type] = 'C' THEN [Perm_GM] ELSE 0 END),
'Perm Falloffs YTD' = SUM(CASE WHEN YEAR(CURRENT_TIMESTAMP)
= YEAR(Pay_Ending_Date) AND
[Type] = 'C' THEN [Perm_Gm] ELSE 0 END)
FROM Master_Business
GROUP BY Specialty, Branch
ORDER BY Specialty DESC