简体   繁体   中英

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.

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. I put your example into this sqlfiddle and it works just fine if you do so.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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