Simple SELECT for Invoices with certain Items

  • Hello All,

    Not sure what my issue is...this should be straight forward.

    I have a table with invoices...each invoice can include 1 or many items.

    InvoiceID | ItemID

    ------------------------

    1 | A

    1 | B

    1 | C

    2 | A

    2 | B

    2 | D

    3 | B

    3 | C

    3 | D

    4 | A

    For this example I what to return any invoice that doesn't include Item "C"...Invoice 2 and 4.

    My query looks like the one below

    SELECT InvoiceID

    FROM TestTable

    WHERE 'C' NOT IN (SELECT ItemID

    FROM TestTable

    GROUP BY InvoiceID, ItemID)

    Any ideas?

    Thanks for your help,

    Jason

  • I would use NOT EXISTS instead of NOT IN like this:

    SELECT DISTINCT a.InvoiceID

    FROM TestTable a

    WHERE NOT EXISTS

    (SELECT * FROM TestTable b

    WHERE a.InvoiceID = b.InvoiceID AND b.ItemID = 'C')

    Greg

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

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