表名medcn_recrd
Name Qty Id
a 12 asc
a 0 asdc
b 0 asfg
c 12 ascd
c 15 acs
查询选择选择ID,其中name具有qty = 0和name的总数量> 0 在上面的示例中,asdc被选为id。
答案 0 :(得分: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
)
答案 1 :(得分: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