简体   繁体   English

SQL返回结果,其中不同相关实体的数量大于1

[英]SQL Return results where number of different related entities is greater than 1

I have a data set that looks like the following: 我有一个数据集,如下所示:

Account      Property
446006253   303004622
446006253   303004622
446006253   303004622
446006253   303004622
446006253   303004622
446007309   969002609
446007309   969002612

I need to return only the Account Number that has multiple unique property numbers attached. 我只需要返回附有多个唯一属性编号的帐号。 In the example of data above, it would return the number 446007309 and not the other. 在上面的数据示例中,它将返回数字446007309,而不返回另一个。

However the best I have managed is the following: 但是,我所管理的最好的方法如下:

SELECT account 
FROM table
WHERE GROUP BY account HAVING COUNT(account) > 1

Ive managed to get to this stage using this website, but I didnt find any examples that are havign a similar issue. 我已经使用此网站成功进入了这个阶段,但是我没有找到任何遇到类似问题的例子。

Thanks. 谢谢。

Try this query 试试这个查询

SELECT account 
FROM table
WHERE GROUP BY account ,Property HAVING COUNT(Property ) > 1

Try below query 尝试以下查询

  select account from (
    SELECT account, COUNT(account) over (partition by Property) rowid 
    FROM table ) AA where AA.rowid >1 

Your own code is very close, but you need to remove the WHERE from your code to get it to run correctly, as it doesn't have a corresponding condition. 您自己的代码非常接近,但是您需要从代码中删除WHERE才能使其正常运行,因为它没有相应的条件。 I put your example into this sqlfiddle and it works just fine if you do so. 我将您的示例放入此sqlfiddle中 ,如果您这样做的话,它就可以正常工作。

SELECT account 
FROM mytable
GROUP BY account HAVING COUNT(account) > 1
   SELECT 
         distinct [account number] FROM
        table GROUP BY
         [account number],property HAVING 
         COUNT(*) = 1

Try following, 尝试跟随,

select account, count(property) 'Total Pro from 
(
    SELECT distinct account , property FROM table
)T
GROUP BY account HAVING count(property) > 1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM