**I have the Following query:**
SELECT t1.tradyrwkcode, t3.tradyrwkcode AS tradyrwkcode_last52wks FROM agent.tradyrweek t1 CROSS APPLY ( SELECT TOP 52 t2.tradyrwkcode FROM agent.tradyrweek t2 WHERE t2.tradyrwkcode <= t1.tradyrwkcode ORDER BY t2.tradyrwkcode DESC ) t3 --Last 13 Weeks Condition WHERE t1.tradyrwkcode >= '201701' AND t1.tradyrwkcode <= '201712' ORDER BY t1.tradyrwkcode, t3.tradyrwkcode So Far I have come up with the following solution to it.
1. Get the **t1.tradwkyrcode** in a temp table like this:
SELECT t1.tradyrwkcode
INTO tt_tradyrwkcode_tt1
FROM agent.tradyrweek t1
--Last 13 Weeks Condition
WHERE t1.tradyrwkcode >= '201701'
AND t1.tradyrwkcode <= '201712'
ORDER BY t1.tradyrwkcode
Result of above DDL:
201701
201702
201703
201704
201705
201706
201707
201708
201709
201710
201711
201712
2. For each value in above temp table get the TOP 52 as follows: SELECT TOP 52 t2.tradyrwkcode
FROM agent.tradyrweek t2
WHERE t2.tradyrwkcode <= '201701'
ORDER BY t2.tradyrwkcode DESC
Now in the second step How do I manipulate so that i can do it for all the values and not only one value as done above i.e for only '201701'