简体   繁体   English

sql选择计数> 1的记录,其中至少有一条记录具有值

[英]sql select records having count > 1 where at lease one record has value

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 我试图让所有参与者在表中有超过1条记录,其中至少有一条记录的IsCurrent = 0且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. 此查询返回与该描述匹配的记录,但我需要与该描述匹配的所有记录,还有更多。

You can use EXISTS : 您可以使用EXISTS

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
        );

Use it as a subquery and join to it: 将其用作子查询并加入其中:

select * from 
(
    SELECT  ParticipantId 
    FROM Contact
    WHERE (IsCurrent = 0 AND IsActive = 1 AND ContactTypeId = 1)
    Group by ParticipantId
    Having COUNT(ParticipantId) > 1
) base
inner join Contact c on c.ParticipantId = base.ParticipantID
WHERE (IsCurrent = 0 AND IsActive = 1 AND ContactTypeId = 1)
select 
  ParticipantId
from Contact as c
group by
  ParticipantId
having 
  Count(*) > 1
  and
  Sum(Case when IsCurrent = 0 then 1 else 0 end) >= 1
  and
  Sum(Case when IsActive = 1 then 1 else 0 end) >= 1

I would first try this 我会先试试这个

I think you should just remove: 我想你应该删除:

AND ContactTypeId = 1

which seems to be an idexed column 这似乎是一个独立的专栏

  SELECT  ParticipantId 
    FROM Contact
   Group by ParticipantId 
  Having Count(*) > 1 
Intersect
  SELECT  ParticipantId 
    FROM Contact
   WHERE IsCurrent = 0 
     AND IsActive = 1 
     AND ContactTypeId = 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL有效地选择值,该值在另一个表中有记录 - SQL efficiently select values where that value has a record in another table 如果id只有一条记录,则选择一条有空值的记录,当id有多条记录时,选择非空值 - Select a record having null if the id has only one record and select not null value when the id has more than one record 选择一条记录中条件为真的记录 - select records where condition is true in one record 如何选择字段具有特定值的所有记录,直到显示具有不同值的记录? - How to select all records where a field is of a certain value until a record shows up that has a different value? SQL为每个userID记录计数一个值,除非userId的一个记录值是x - SQL count a value for each userID record except where one record value of userId is x 如何获取SQL查询HAVING count显示具有INNER JOIN存在的相同值的BOTH记录 - How to get SQL query HAVING count display BOTH records with the same value where INNER JOIN exists SQL:如果另一个具有相关记录的表没有特定值,则从一个表中选择记录 - SQL: Select records from one table if another table with related records has no specific value SQL查询返回多列中具有特定条件的记录,并且仅存在具有特定值的一条记录 - SQL Query to return records with certain conditions in several columns, and where only one record with a certain value exists Select 条记录,其中多个字段在同一记录中有数据 - 客户可以订购一本或多本书 - Select records where more than one field has data in the same record - a customer can order one or more books 如何 select 记录 SQL 其中一个 SKU 有 2 个或更多相同的结束日期 - How to select records in SQL where one SKU has 2 or more of the same end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM