本文介紹了sql選擇具有計數>的記錄1 至少有一條記錄有價值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我試圖讓所有在表中擁有超過 1 條記錄的參與者,其中至少有一條記錄的 IsCurrent = 0 和 IsActive = 1
I'm trying to get all participants that have more than 1 record in the table where at lease one of those records has IsCurrent = 0 and IsActive = 1
這是我目前所擁有的,但它不起作用:
This is what I have so far, but it's not working:
SELECT ParticipantId
FROM Contact
WHERE (IsCurrent = 0 AND IsActive = 1 AND ContactTypeId = 1)
Group by ParticipantId
Having COUNT(ParticipantId) > 1
此查詢帶回與該描述匹配的記錄,但我需要與該描述匹配的所有記錄,還有更多.
This query brings back a record that matches that description, but I need all of the records that match that description, there are more.
推薦答案
您可以使用 存在:
SELECT ParticipantId
FROM Contact
WHERE EXISTS
( SELECT 1
FROM Contact c2
WHERE c2.ParticipantID = c.ParticipantId
AND ContactTypeId = 1
GROUP BY ParticipantID
HAVING COUNT(*) > 1
AND COUNT(CASE WHEN IsCurrent = 0 AND IsActive = 1 THEN 1 END) >= 1
);
這篇關于sql選擇具有計數>的記錄1 至少有一條記錄有價值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!