简体   繁体   中英

SQL Query to find attributes that don't match for secondary matching attribute

Need to create a query that finds a set of records like the following:

ID  |  Address    |  Unit | Status

1   |555 Smith Rd | Apt A | Success
2   |555 Smith Rd | Apt B | Success
3   |555 Smith Rd | Apt C | Success
4   |555 Smith Rd | Apt D | Failure

I need to select records where Address field matches and where the status field is NOT matching. Ideally I would like to display the group of records like the example above.

You can use analytic functions for this:

select id, address, unit, status
from (select t.*, min(status) over (partition by address) as mins,
             max(status) over (partition by address) as maxs
      from table t
     ) t
where mins <> maxs
order by address;

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