chetan
chetan

Reputation: 76

Query to select record

table name medcn_recrd

Name   Qty   Id 
a       12   asc
a       0    asdc
b       0    asfg
c       12   ascd
c       15   acs

Query to select Select id where name has qty =0 and total qty of name > 0 In above example asdc is selected id .

Upvotes: 1

Views: 55

Answers (2)

lc.
lc.

Reputation: 116528

I'll assume this is the part that's tripping you up:

SELECT t.Id
FROM theTable t
INNER JOIN
(
    SELECT Name
    FROM theTable
    GROUP BY Name
    HAVING SUM(Qty)>0
) sumN ON t.Name = sumN.Name
WHERE t.Qty = 0

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166476

How about something like

SELECT  *
FROM    TABLE t
WHERE   Qty = 0
AND     EXISTS  (
                    SELECT  Name
                    FROM    TABLE t1
                    WHERE   t1.Name = t.Name
                    GROUP BY Name
                    HAVING SUM(Qty) > 0
                )

Upvotes: 5

Related Questions