本文介紹了sql選擇具有計(jì)數(shù)>的記錄1 至少有一條記錄有價(jià)值的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我試圖讓所有在表中擁有超過(guò) 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
);
這篇關(guān)于sql選擇具有計(jì)數(shù)>的記錄1 至少有一條記錄有價(jià)值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!