以下是查询:
SELECT xxx.NSN, xxx.CN
, xxx.PN
, xxx.[BNAME]
, xxx.[BTYPE]
, xxx.[BSYS]
, yyy.[BCODE]
, Sum(yyy.QTY) AS SumOfQTY
, Sum(xxx.[UNIT COST]) AS [SumOfUNITCOST]
, xxx.SS
, yyy.[Hold]
, yyy.[SCI]
FROM xxx INNER JOIN yyy ON xxx.CN = yyy.CN
WHERE xxx.PN='11'
GROUP BY xxx.NSN, xxx.CN
, xxx.PN
, xxx.[BNAME
, xxx.[BTYPE]
, xxx.[BSYS]
, yyy.[BCODE]
, xxx.SS
, yyy.[Hold]
, yyy.[SCI]
, yyy.LOC
HAVING (((yyy.[Hold]) Is Null)
AND ((yyy.[SCI]) Is Null)
AND ((yyy.LOC) != 'SHIPPED'))
ORDER BY xxx.PN;
以下是返回的数据:
NSN CN PN BNAME BTYPE BSYS BCODE SumOfQTY SumOfUNITCOST SS Hold SCI
-----------------------------------------------------------------------------------------
304 CO 11 LINK AB-12 SUB F 9 4536.00 B17 NULL NULL
304 CO 11 LINK AB-12 SUB F 5 2520.00 B17 NULL NULL
现在查看数据,我希望得到这样的结果:
NSN CN PN BNAME BTYPE BSYS BCODE SumOfQTY SumOfUNITCOST SS Hold SCI
-----------------------------------------------------------------------------------------
304 CO 11 LINK AB-12 SUB F 14 7056.00 B17 NULL NULL
为什么将两者分开而不是将它们分组?
答案 0 :(得分:2)
据推测,记录在LOC
不同。因此,请从GROUP BY
。
然后你必须处理HAVING
条款。也许你打算:
HAVING (yyy.[Hold]) Is Null) AND
(yyy.[SCI]) Is Null) AND
SUM(CASE WHEN yyy.LOC = 'SHIPPED' THEN 1 ELSE 0 END) = 0