select DISTINCT po. *
from purchaseorder po
inner join poTask pt on (po.purchaseorderid = pt.purchaseorderid)
inner join poTaskline ptl1 on (pt.potaskid = ptl1.potaskid) and ptl1.poTasklinetype = 'M'
inner join poTaskline ptl2 on (pt.potaskid = ptl2.potaskid) and ptl2.poTasklinetype = 'D'
where po.PoStatus = 6
这给了我所有订单,其中包含D和M的Tasklinetypes
但是我如何才能获得它只显示只有D和M而不再有M的采购订单?
我很新,我花了半天时间才弄清楚上面的情况。所以我现在真的很难过。我希望你不需要更多的背景。
答案 0 :(得分:1)
我使用(NOT) EXISTS
检查poTaskline
表中是否存在这些条目:
SELECT po.*
FROM purchaseorder po
INNER JOIN poTask pt ON po.purchaseorderid = pt.purchaseorderid
WHERE po.PoStatus = 6 -- PoStatus of 6
AND EXISTS (SELECT 1 FROM poTaskLine ptl WHERE ptl.potaskid = pt.potaskid AND ptl.poTasklinetype = 'M') -- there's a poTaskLine entry for 'M' for this potaskid
AND EXISTS (SELECT 1 FROM poTaskLine ptl WHERE ptl.potaskid = pt.potaskid AND ptl.poTasklinetype = 'D') -- there's a poTaskLine entry for 'D' for this potaskid
AND NOT EXISTS (SELECT 1 FROM poTaskLine ptl WHERE ptl.potaskid = pt.potaskid AND ptl.poTasklinetype NOT IN ('D', 'M')) -- there aren't any poTaskLine entries for anything else
答案 1 :(得分:1)
您可以使用HAVING
子句过滤您的群组。
select po. *
from purchaseorder po
inner join poTask pt
on po.purchaseorderid = pt.purchaseorderid
inner join poTaskline ptl1
on pt.potaskid = ptl1.potaskid
and
(ptl1.poTasklinetype = 'M'
OR ptl1.poTasklinetype = 'D')
where po.PoStatus = 6
GROUP BY po.col1, po.col2... -- You must mention here all necessary fields from
-- purchaseorder which must be in select list
HAVING COUNT(po.id) = 2
答案 2 :(得分:0)
您可以尝试使用以下修改后的查询:
select DISTINCT po. *
from purchaseorder po
inner join poTask pt on (po.purchaseorderid = pt.purchaseorderid)
inner join poTaskline ptl1 on (pt.potaskid = ptl1.potaskid) and ptl1.poTasklinetype in ('M', 'D')
where po.PoStatus = 6
答案 3 :(得分:0)
使用group by
并比较having
子句中的值。以下内容获取所有任务信息。它按采购订单ID聚合:
select po.*
from purchaseorder po inner join
poTask pt
on (po.purchaseorderid = pt.purchaseorderid) inner join
poTaskline ptl
on (pt.potaskid = ptl.potaskid
where po.PoStatus = 6
group by po.purchaseorderid
having sum( ptl.poTasklinetype not in ('D', 'M')) = 0;
最后,having
子句计算线型既不是D
也不是M
的次数。 = 0
表示没有这种类型。