They don't appear to be related...how doesWITH(SELECT g4.*,SUM (rolloffs) OVER (PARTITION BY employee) AS total_rolloffsFROM FirstCalculation g4)
Relate to this:SecondCalculation asSELECT m_atschunk.calculationDate as calcdateFROM M_ATSCHUNKSELECT g1.*,CASEWHEN calcdate IS NULL THEN 1END as Final_CalcDateFROM SecondCalculation g1
You appear to want to use the 2nd part (SecondCalculation) as another CTE, but it is unclear how you intend to use the FirstCalculation.To separate CTE's you need to use COMMA..and after the last one, do your final select. It appears SecondCalculation is intended to be another CTE, but it doesn't appear to use or relate to the first one. This appears to be to entirely separate queries;WITH CTE1 AS (Select ....), CTE2 AS (Select....)Select * FROM CTE2
I keep staring it this and I have no idea what you are trying to do..you need to be more clear, provide your results from each query (with the full query that produced them) along with your desired output . Sample data with structures and insert statements would help as well.Based on what you have provided, help is almost impossible.
Poor planning on your part does not constitute an emergency on my part.