Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Posts
    233

    Unanswered: Exclude based on 2 columns of a sub qry

    I have a qry that select 5 columns 2 of which are pNo and fNo I need to exclude all records that have a sum of 0 when i group by just those 2 columns originally it was just 1 column so i did

    where pNo not in(

    select pNo from (

    SELECT pNo,Sum(amt)total
    FROM Accounts
    GROUP BY pNo
    HAVING Sum(amt) =0

    )

    but now i have to use both columns to group and i can do both columns:

    Where pNo and fNo not in(

    SELECT fNo ,pNo FROM(

    SELECT fNo ,pNo,Sum(amt)total
    FROM Accounts
    GROUP BY fNo,pNo
    HAVING Sum(amt) >0)

    So what do i do? Also this is inline sql in a classic asp file so i cant do temp tbl or smthing like that

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    It would help to see all of your query, but you want to use a NOT EXISTS clause with a CORRELATED SUBQUERY OR what is wrong with this...
    SELECT fNo ,pNo,Sum(amt)total
    FROM Accounts
    GROUP BY fNo,pNo
    HAVING Sum(amt) =0
    “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
    Oct 2003
    Posts
    233
    All the sql
    SELECT i.fundno,
    i.pfundno,
    CONVERT(VARCHAR, i.i_date, 1) AS idate,
    hf.name,
    ai.anndate,
    CAST(NULL AS FLOAT) AS incomebal
    INTO #temp
    FROM dbo.income i
    LEFT JOIN dbo.anniversary_info AS ai
    ON ai.fundno = i.fundno
    LEFT JOIN liquidity_info li
    ON li.fundno = i.fundno
    LEFT JOIN hedgefunds hf
    ON hf.fundno = i.fundno
    WHERE ( i.TYPE = 8 )
    AND ( i.fundno < 9901 )
    AND ( ai.fundno IS NULL )
    AND ( li.period LIKE '%an%' )
    GROUP BY i.fundno,
    i.pfundno,
    CONVERT(VARCHAR, i.i_date, 1),
    hf.name,
    ai.anndate
    ORDER BY i.fundno,
    i.pfundno

    SELECT fundno,
    pfundno,
    SUM(amt) s
    INTO #delete
    FROM income
    GROUP BY fundno,
    pfundno
    HAVING Abs(SUM(amt)) = 0

Posting Permissions

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