[英]SQL Return results where number of different related entities is greater than 1
我有一个数据集,如下所示:
Account Property
446006253 303004622
446006253 303004622
446006253 303004622
446006253 303004622
446006253 303004622
446007309 969002609
446007309 969002612
我只需要返回附有多个唯一属性编号的帐号。 在上面的数据示例中,它将返回数字446007309,而不返回另一个。
但是,我所管理的最好的方法如下:
SELECT account
FROM table
WHERE GROUP BY account HAVING COUNT(account) > 1
我已经使用此网站成功进入了这个阶段,但是我没有找到任何遇到类似问题的例子。
谢谢。
试试这个查询
SELECT account
FROM table
WHERE GROUP BY account ,Property HAVING COUNT(Property ) > 1
尝试以下查询
select account from (
SELECT account, COUNT(account) over (partition by Property) rowid
FROM table ) AA where AA.rowid >1
您自己的代码非常接近,但是您需要从代码中删除WHERE才能使其正常运行,因为它没有相应的条件。 我将您的示例放入此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
尝试跟随,
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.