【问题标题】:SQL Union all statementSQL Union all 语句
【发布时间】:2013-09-01 14:02:00
【问题描述】:

我使用 UNION ALL 加入了 4 条 sql 语句。

由于WHERE条件相同,是否可以将WHERE条件合二为一?

select 'Transfer In' as MovementType, * from vHRIS_StaffMovement_TransferIn 
where cur_deptid in (1,2,3,4,5)
        and cast(EffectiveDate as date) <='2013-08-02'
        and cast(EffectiveDate as date) >= '2012-08-01'
        and StaffType in (1,2,3,4,5)

union all

        select 'Terminate' as MovementTyep, * from vHRIS_StaffMovement_Terminate  
where cur_deptid in (1,2,3,4,5)
        and cast(EffectiveDate as date) <='2013-08-02'
        and cast(EffectiveDate as date) >= '2012-08-01'
        and StaffType in (1,2,3,4,5)

union all

        select 'New Hire' as MovementTyep, * from vHRIS_StaffMovement_NewHire 
where cur_deptid in (1,2,3,4,5)
        and cast(EffectiveDate as date) <='2013-08-02'
        and cast(EffectiveDate as date) >= '2012-08-01'
        and StaffType in (1,2,3,4,5)      

union all

select 'Transfer Out' as MovementType, * from vHRIS_StaffMovement_TransferOut 
where cur_deptid in (1,2,3,4,5)
        and cast(EffectiveDate as date) <='2013-08-02'
        and cast(EffectiveDate as date) >= '2012-08-01'
        and StaffType in (1,2,3,4,5)

【问题讨论】:

    标签: sql union where-clause union-all


    【解决方案1】:

    你可以这样做:

        select * from (
            select 'Transfer In' as MovementType, * from vHRIS_StaffMovement_TransferIn 
            union all
            select 'Terminate' as MovementTyep, * from vHRIS_StaffMovement_Terminate  
            union all
            select 'New Hire' as MovementTyep, * from vHRIS_StaffMovement_NewHire 
            union all
            select 'Transfer Out' as MovementType, * from vHRIS_StaffMovement_TransferOut ) as a
    where cur_deptid in (1,2,3,4,5)
            and cast(EffectiveDate as date) <='2013-08-02'
            and cast(EffectiveDate as date) >= '2012-08-01'
            and StaffType in (1,2,3,4,5)
    

    【讨论】:

      【解决方案2】:

      您可以使用子查询:

      SELECT X.* 
      FROM   (SELECT 'Transfer In' AS MovementType, * 
              FROM   vhris_staffmovement_transferin 
      
              UNION ALL 
      
              SELECT 'Terminate' AS MovementTyep, * 
              FROM   vhris_staffmovement_terminate 
      
              UNION ALL 
      
              SELECT 'New Hire' AS MovementTyep,  * 
              FROM   vhris_staffmovement_newhire
      
              UNION ALL 
      
              SELECT 'Transfer Out' AS MovementType, * 
              FROM   vhris_staffmovement_transferout ) X 
      WHERE  cur_deptid IN ( 1, 2, 3, 4, 5 ) 
             AND Cast(effectivedate AS DATE) <= '2013-08-02' 
             AND Cast(effectivedate AS DATE) >= '2012-08-01' 
             AND stafftype IN ( 1, 2, 3, 4, 5 ) 
      

      【讨论】:

        【解决方案3】:

        你可以试试这个:

        SELECT *
          FROM T_IP_CHARGES WITH (NOLOCK)
          WHERE PostingDate BETWEEN '01/01/2014' AND '01/31/2014'
            AND DetailTotalCharges > 0
          UNION ALL
          SELECT *
          FROM T_OP_CHARGES WITH (NOLOCK)
          WHERE PostingDate BETWEEN '01/01/2014' AND '01/31/2014'
          AND DetailTotalCharges > 0
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多