必须有更好的方法来写这个,但我只是不确定那是什么。基本上,我试图从一列中计算不同的值,其中在单独的列中满足条件。我找到了这个链接,但不知道如何在这里应用它。
这是查询,我使用的是 SQL Server 2008R2
SELECT lot,
(SELECT COUNT(DISTINCT d.pid) FROM invdet d WHERE upk = 0 and d.lot = [invdet].lot) as noUpk,
(SELECT COUNT(DISTINCT d.pid) FROM invdet d WHERE upk = 1 and d.lot = [invdet].lot) as isUpk
FROM invdet
WHERE ([status] in ('PQ','P2','FA','F2','BH','RL','SC','LD','PS'))
GROUP BY lot
HAVING COUNT(CASE WHEN invdet.upk = 1 THEN 1 ELSE null END) > 0