1

表名 medcn_recrd

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

查询以选择 Select id where name has qty =0 and total qty of name > 0 在上面的示例中 asdc 被选中 id 。

4

2 回答 2

5

像这样的东西怎么样

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
                )
于 2012-09-14T03:57:44.240 回答
0

我会假设这是让你绊倒的部分:

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
于 2012-09-14T03:57:58.533 回答