Union All

  • Just a quick question in regards to using UNION ALL: Is there anyway I can use one where claus to effect every select in a UNION ALL. LIke this

    Select * from cub_elec_hist

    UNION ALL

    Select * from msh_elec_hist

    WHERE TYPE = 'RES' and mtyr_period = '12/1/2002'

    or does it have to be like this.

    Select * from cub_elec_hist

    WHERE TYPE = 'RES' and mtyr_period = '12/1/2002'

    UNION ALL

    Select * from msh_elec_hist

    WHERE TYPE = 'RES' and mtyr_period = '12/1/2002'

  • Just wrap the union in another select:

    select * from

    (

    select * from a

    union all

    select * from b

    )

    where whatever=something

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

  • I tried this and I got the following error:

    Incorrect syntax near the keyword 'WHERE'.

    Select * from

    (

    Select * from cub_elec_hist

    UNION ALL

    Select * from msh_elec_hist

    )

    WHERE TYPE = 'RES' and mtyr_period = '12/1/2002'

  • Dont have access to a server, so it's off the cuff! Try adding an alias after the close paren.

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

  • Works Great!

    Select * from

    (

    Select * from cub_elec_hist

    UNION ALL

    Select * from msh_elec_hist

    ) a

    WHERE TYPE = 'RES' and mtyr_period = '12/1/2002'

    Thanks Andy

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply