使用Union为类似的表创建临时表

时间:2013-11-25 11:46:05

标签: sql sql-server

我有5个具有类似结构和类似名称的表,如Subscriptions_123,Subscriptions_456等。

我需要创建一个临时表,根据条件将所有这五个表数据组合在一起。

现在我首先创建一个临时表,然后将数据插入其中。是否有办法在另一个查询的from子句中创建临时表(使用此查询)。

查询

Select
       MOB_NO
     , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
     , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
  from Subscriptions 
 where Subscription_Id in (select Distinct ServiceID 
                             from CommonLookup 
                            where ShortCode in (816602,816603,816604))
 UNION 
Select MOB_NO
     , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
     , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
  from Subscriptions_125
 where Subscription_Id in (select Distinct ServiceID 
                             from CommonLookup 
                            where ShortCode in (816602,816603,816604))
 UNION 
Select MOB_NO
     , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
     , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
  from Subscriptions_467
 where Subscription_Id in (select Distinct ServiceID 
                             from CommonLookup 
                            where ShortCode in (816602,816603,816604))
 UNION 
Select MOB_NO
     , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
     , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
  from Subscriptions_Campaign
 where Subscription_Id in (select Distinct ServiceID 
                             from CommonLookup 
                            where ShortCode in (816602,816603,816604))
 UNION 
Select MOB_NO
     , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
     , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
  from Subscriptions_Kenan1
 where Subscription_Id in (select Distinct ServiceID 
                             from CommonLookup 
                            where ShortCode in (816602,816603,816604))

谢谢, 维杰

2 个答案:

答案 0 :(得分:1)

你可以使用select作为select的子选择,并以任何理由创建一个临时表:

select *
  into temp_table
  from (
        Select
               MOB_NO
             , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
             , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
          from Subscriptions 
         where Subscription_Id in (select Distinct ServiceID 
                                     from CommonLookup 
                                    where ShortCode in (816602,816603,816604))
         UNION 
        Select MOB_NO
             , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
             , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
          from Subscriptions_125
         where Subscription_Id in (select Distinct ServiceID 
                                     from CommonLookup 
                                    where ShortCode in (816602,816603,816604))
         UNION 
        Select MOB_NO
             , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
             , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
          from Subscriptions_467
         where Subscription_Id in (select Distinct ServiceID 
                                     from CommonLookup 
                                    where ShortCode in (816602,816603,816604))
         UNION 
        Select MOB_NO
             , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
             , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
          from Subscriptions_Campaign
         where Subscription_Id in (select Distinct ServiceID 
                                     from CommonLookup 
                                    where ShortCode in (816602,816603,816604))
         UNION 
        Select MOB_NO
             , CONVERT(DATE,Subscription_Start_Date) SUB_DATE
             , CONVERT(DATE,Unsubscribed_Date)       UNSUB_DATE 
          from Subscriptions_Kenan1
         where Subscription_Id in (select Distinct ServiceID 
                                     from CommonLookup 
                                    where ShortCode in (816602,816603,816604))
       ) isel

答案 1 :(得分:0)

WITH AllData AS (
    SELECT MOB_NO
          ,Subscription_Start_Date
          ,Unsubscribed_Date
          ,Subscription_Id
    FROM Subscriptions
  UNION ALL
    SELECT MOB_NO
          ,Subscription_Start_Date
          ,Unsubscribed_Date
          ,Subscription_Id
    FROM Subscriptions_125
  UNION ALL
    ...
)
,Filtered AS (
    SELECT DISTINCT -- Only needed if your tables can contain duplicates
           MOB_NO
          ,CONVERT(DATE,Subscription_Start_Date) AS SUB_DATE
          ,CONVERT(DATE,Unsubscribed_Date) AS UNSUB_DATE 
    FROM AllTables
    WHERE SubscriptionId IN (SELECT ServiceID
                             FROM CommonLookup
                             WHERE ShortCode IN (816602,816603,816604))
)
SELECT * FROM Filtered