我有以下SQL语句:
SELECT C.LINKED_TABLE_ID AS CLIENT_DIWOR, I.STATUS AS AML_STATUS, dbo.CLIENT_MASTER.CLIENTCODE
FROM AML_INFORMATION AS I INNER JOIN
dbo.COMM_ENTRY AS C ON C.NAME_ID = I.CONTACT_ID AND C.TABLE_ID = 'C' AND C.PRIMARY_FLAG = 'Y' INNER JOIN
dbo.CLIENT_MASTER ON C.LINKED_TABLE_ID = dbo.CLIENT_MASTER.DIWOR
WHERE I.CONTACT_ID = 234
AND I.[STATUS] = 'CC'
AND (CLIENT_MASTER.DIWOR = I.CONTACT_ID)
AND (CLIENT_MASTER.POSTING_STATUS <> '')
AND ((SELECT COUNT(CONTACT_ID) FROM AML_ID_DOCUMENT GROUP BY CONTACT_ID HAVING CONTACT_ID = 234) >1)
如果我运行它会返回0条记录,但是如果我删除最后一条AND语句AND ((SELECT COUNT(CONTACT_ID) FROM AML_ID_DOCUMENT GROUP BY CONTACT_ID HAVING CONTACT_ID = 234) >1)
,它会返回我期望的记录。
是否可以这样使用COUNT()?顺便说一下,这个例子中的COUNT()返回2条记录,如果我把它包含在SELECT语句中,我也会得到0条记录。
有人能指出我正确的方向吗?
提前致谢。
答案 0 :(得分:8)
你展示的剧本永远不应该归还任何东西...... 你的部分看起来像这样
and (Select Count(*) from AML_ID_DOCUMENT where CONTACT_ID =I.CONTACT_ID)>1
虽然它应该按原样工作......
答案 1 :(得分:6)
最后一部分不应该是这样的:
AND EXISTS (SELECT CONTACT_ID FROM AML_ID_DOCUMENT
WHERE CONTACT_ID = 234
GROUP BY CONTACT_ID
HAVING COUNT(CONTACT_ID)>1)
答案 2 :(得分:0)
尝试此查询:
select count(portfolio_id) as porfolioCount
from engineering_module_tracker_v2
where r0_approval__category='Cat I' & 'Cat II';
Cat意味着按类别划分。