简体   繁体   中英

Create a Dynamic Query into a Temp table instead of hard coding union statements

I have hardcoded a bunch of union statements to create a row for each region, for each month for each fiscal year (the example is just for the fiscal year 2017). Instead of all these lines of code, I would really like to 'build a loop' that dynamically creates the temp table instead of the hundreds of lines of code and union statements.

*****Update code from Gordon's proposed solution

SELECT dt.fulldate AS trade_month, 
       t.trade_gy  AS trade_gy, 
       r.dvsregion_id, 
       r.dvsregion_name 
FROM   dimdate dt 
       CROSS JOIN (VALUES ('2017'), 
                          ('2018')) AS t(trade_gy) 
       CROSS JOIN (VALUES ('1', 
                  'Midwest'), 
                          ('2', 
                  'Northeast')) as r(dvsregion_id, dvsregion_name) 
WHERE  Year(dt.fulldate) >= 2012 
       AND dt.fulldate = dt.firstofmonthdate 
       AND dt.fulldate IN (SELECT p.fulldate 
                           FROM   dimdate p 
                           WHERE  ( Year(p.fulldate) ) >= 2012 
                                  AND p.lastdayofgasyear <= '2018-03-31') 
ORDER  BY trade_gy ASC, 
          trade_month ASC 
  • Start date would be 1-1-2012*

  • End date changes for each fiscal year.

  • Our fiscal year ends on March 31st (2017 ends 3-31-18, and so on).

  • Fiscal years includes 2017 - 2023

  • Regions: Midwest, Northeast, Western, Canada

Any suggestions or links that will help me is greatly appreciated!

Thanks

Sample of the code for the Fiscal year 2017. All other years are the same format, except I update p.LastDayofGasYear <= '3-31-2018') for each fiscal year.

 Select *
into #Regions
From (
--Create regions temp table for GY 2017
select
dt.FullDate as trade_month ,
'2017' as trade_gy,
'1' as dvsregion_id, 
'Midwest' as dvsregion_name
from dimDate dt
where  (year(dt.FullDate)) >= 2012  
and dt.FullDate = dt.FirstOfMonthDate
and dt.FullDate IN (
Select p.FullDate
from dimDate p
where (year(p.FullDate)) >= 2012 and p.LastDayofGasYear <= '3-31-2018')
Union
select
dt.FullDate as trade_month ,
'2017' as trade_gy,
'2' as dvsregion_id, 
'Northeast' as dvsregion_name
from dimDate dt
where  (year(dt.FullDate)) >= 2012  
and dt.FullDate = dt.FirstOfMonthDate
and dt.FullDate IN (
Select p.FullDate
from dimDate p
where (year(p.FullDate)) >= 2012 and p.LastDayofGasYear <= '3-31-2018')
Union
select
dt.FullDate as trade_month ,
'2017' as trade_gy,
'3' as dvsregion_id, 
'Western' as dvsregion_name
from dimDate dt
where  (year(dt.FullDate)) >= 2012  
and dt.FullDate = dt.FirstOfMonthDate
and dt.FullDate IN (
Select p.FullDate
from dimDate p
where (year(p.FullDate)) >= 2012 and p.LastDayofGasYear <= '3-31-2018')
Union
select
dt.FullDate as trade_month ,
'2017' as trade_gy,
'5' as dvsregion_id, 
'Canada' as dvsregion_name
from dimDate dt
where  (year(dt.FullDate)) >= 2012  
and dt.FullDate = dt.FirstOfMonthDate
and dt.FullDate IN (
Select p.FullDate
from dimDate p
where (year(p.FullDate)) >= 2012 and p.LastDayofGasYear <= '3-31-2018')

End Results will look something like this

It seems like you just want cross join s on constant values:

select dt.FullDate as trade_month ,
       t.trade_gy as trade_gy,
       r.dvsregion_id, 
       r.dvsregion_name
from dimDate dt cross join
     (values ('2017', '2018', . . .) as t(trade_gy) cross join
     (values ('1', 'Midwest'),
             ('2', 'Northeast'),
             . . .
     ) r(dvsregion_id, dvsregion_name)
where year(dt.FullDate)) >= 2012 and
      dt.FullDate = dt.FirstOfMonthDate and
      dt.FullDate in (Select p.FullDate
                      from dimDate p
                      where (year(p.FullDate)) >= 2012 and p.LastDayofGasYear <= '2018-03-31'
                     );

I'm no sure if I fully follow all the logic in your quite complex query, but I think it can be structured like this.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM