Extracting Specfic Subset with all Remaining Records

  • Hello,

    I need to pull records from single table such that I get a subset defined like this:

    (

    acctcode = 'xh364'

    and

    product = 'T&E'

    )

    And return all of the rest of the records:

    (

    acctcode = '%'

    and

    product = '%'

    )

    Can I do this within a WHERE clause, or will this require CASE / ELSE? There will be other specific acctcode/product rules that will be added later. I could do this with a UNION, but I need to avoid that if possible.

    Thank you for your help!

    CSDunn

  • I am not sure I understand what you are trying to do. You say that you want a subset of data, but you also want to return all the other records... This does not make sense, am I missing something?? Why not just return the whole dataset?

  • Sorry for the confusion. Each Acctcode can be associated with up to seven products. I need to return all acctcodes with each assciated product unless the acctcode = 'xh364'. In that case, the only product I want to return is 'T&E'.

    Does that make more sense?

    I've just tried the following, and it seems to work:

    SELECT DISTINCT AcctCode, Product

    FROM MonthlyMCTxnDetail

    WHERE

    (

    NOT AcctCode = 'xh364'

    OR

    (AcctCode = 'xh364' AND Product = 'T&E')

    )

    AND

    (

    NOT AcctCode = 'dq300'

    OR

    (Acctcode = 'dq300' AND Product = 'Gasoline')

    )

    ORDER BY AcctCode, Product

  • Your query looks good. Thanks for the clarification though. I was a little confused :hehe:

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

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