Combining queries - how?

  • Hi, I've got 2 queries, that basically do the same thing, but require a different date range, I'd liek to know how to combine them.

    its like this:

    Query A:

    select x from tablea

    where

    z = 1

    and startdate > '2007-12-31'

    and startdate < '2008-02-01'

    Query B:

    select x from tablea

    where

    z = 0

    and startdate > '2008-01-31'

    and startdate < '2008-03-01'

    So depending upon whether z is 1 or 0 alters the date range I use.

    Any help much appreciated.

  • Can you explain what you mean by combine? Run both of them together? Run one of them based on Z?

  • sorry, yes.

    I'd like to get both sets of results but only using 1 query

    so like:

    select a

    from tablea

    where

    if z=0

    and startdate > '2008-01-31'

    and startdate < '2008-03-01'

    else if z=1

    and startdate > '2007-12-31'

    and startdate < '2008-02-01'

    endif

    is that sort of thing possible?

  • Try just simply using an OR:

    select a

    from tablea

    where

    (

    z=0

    and startdate > '2008-01-31'

    and startdate < '2008-03-01'

    )

    OR

    (

    z=1

    and startdate > '2007-12-31'

    and startdate < '2008-02-01'

    )

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • of course.

    I feel such a fool.

    couldn't see the wood for trees.

    🙂

  • It's early - grab some more caffeine - the fog will disperse soon:)

    I have weeks like that too!

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

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

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