Quantcast
Channel: Questions in topic: "cte"
Viewing all articles
Browse latest Browse all 75

using results of cte (common table expression) in a query

$
0
0
DECLARE @year date = dateadd(year, datediff(year, 0, '2017-01-01'), 1);
 WITH weeks(WeekNumber) AS
 (
 SELECT 0
 UNION ALL
 SELECT WeekNumber + 1 
 FROM weeks
 WHERE WeekNumber < 51
 )
SELECT  dateadd(week, WeekNumber, @year) FROM weeks AS WorkWeek 

The payday is each monday of the week, the results of this cte is returning those dates for the year I have attemptd to write the results of this cte to a temp table:

declare @tempTable table(WeekNumber date)
DECLARE @year date = dateadd(year, datediff(year, 0, '2017-01-01'), 1);
 WITH weeks(WeekNumber) AS
 (
 SELECT 0
 UNION ALL
 SELECT WeekNumber + 1 
 FROM weeks
 WHERE WeekNumber < 51
 )
 insert into @tempTable
SELECT  dateadd(week, WeekNumber, @year) FROM weeks AS WorkWeek 

I have the following query, where I want to user the values from @tempTable

select hrlyrate, reghrs, othrs from tableb
where loginId in (select distinct empid from tableb where workday between date and '2017-12-31'
and payday > '2017-01-01'
note: where date is an iteration of the dates returned from the query select * from @tempTable
I have tried using a while loop to no avail. Is there a better way to get this information and to perform the query?

Viewing all articles
Browse latest Browse all 75