Results 1 to 7 of 7

Thread: SQL query help

  1. #1
    Join Date
    Sep 2003
    Posts
    176

    Unanswered: SQL query help

    Hi all,

    In the list below,

    Product Type Amt
    A 1 10
    A 2 20
    A 3 30

    B 1 10
    B 2 -10

    C 1 10
    C 2 20

    I need to select products that are of the type 1&2 Only and have an amt greater than zero.

    Therefore, I need only C from the list.

    A is elimated coz it contains type 3 although it has a positive amt. B is elimated coz although it has only type 1 & 2, the amt nets to zero.

    Can you help me with this query?

    Thanks much.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Its Freyr's day, so lets give some code away...

    Code:
    SELECT Product,COUNT(DISTINCT Type)
    FROM MyTable
    WHERE Amt > 0
    AND Type in (1,2)
    GROUP BY Product
    HAVING COUNT(DISTINCT Type) = 2
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Sep 2003
    Posts
    176
    Thanks much. That helps.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    i guess you get what you pay for

    the Amt condition in the WHERE clause eliminates B 2, and therefore it looks like B has only 1, and so it fails the HAVING... note that the specs referred to B being rejected because of the net of zero, so if B 2 were -9 instead of -10, B should be returned

    the Type condition in the WHERE clause eliminates A 3, and therefore it looks like A has only 1 and 2, and so it satisfies the HAVING, but according to the specs, A should not be returned

    try again?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    you are right. I did not read the spec that thoroughly.

    although before I retry, I would have to ask the OP if the Amount <> 0 refers to the aggregate or whether we are simply eliminating single records where the Amt = 0.

    I suck.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    SELECT Product
    FROM MyTable
    GROUP BY Product
    HAVING SUM(Amt) > 0
    AND COUNT(CASE WHEN Type in (1,2) THEN 937 ELSE NULL END) = 2
    AND COUNT(CASE WHEN Type in (1,2) THEN NULL ELSE 937 END) = 0
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  7. #7
    Join Date
    Sep 2003
    Posts
    176
    The amount <> 0 refers to the aggregate. Would this work?

    SELECT Product
    FROM MyTable
    GROUP BY Product
    HAVING SUM(Amt) > 0
    AND COUNT(CASE WHEN Type in (1,2) THEN 1 END) = 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •