0

I have the following database table

tbl_setup

id
peer
gw

I have the following records in there

1 | HA | GW1
2 | HA | GW2
3 | HA | GW3
4 | AA | GW1
5 | AB | GW2
6 | AB | GW3
7 | AB | GW4
8 | EE | GW3

Trying to figure out a query to find out which GW are missing data so trying to figure out a query to give me the following results, these would be the GW's missing

HA | GW4
AA | GW2
AA | GW3
AA | GW4
AB | GW1
EE | GW1
EE | GW2
EE | GW4

There should only be 4 GW's GW1 - GW4, will never be more than that

2
  • Please confirm master data for peer and gw column. Commented Aug 5, 2019 at 16:13
  • The data that you are seeing in the first code block is what is in there now, and i just need to be able to run a query to see whats missing Commented Aug 5, 2019 at 16:14

2 Answers 2

1

With a cross join of the distinct values of the 2 columns and then not exists:

select *
from (select distinct gw from tablename) g 
cross join (select distinct peer from tablename) p 
where not exists (
  select 1 from tablename
  where gw = g.gw and peer = p.peer
)
order by gw, peer

See the demo.
Or:

select g.gw, p.peer
from (select distinct gw from tablename) g 
cross join (select distinct peer from tablename) p 
left join tablename t
on t.gw = g.gw and t.peer = p.peer
where t.id is null
order by g.gw, p.peer

See the demo.
Results:

| gw  | peer |
| --- | ---- |
| AA  | GW2  |
| AA  | GW3  |
| AA  | GW4  |
| AB  | GW1  |
| EE  | GW1  |
| EE  | GW2  |
| EE  | GW4  |
| HA  | GW4  |
Sign up to request clarification or add additional context in comments.

5 Comments

Arr, you beat me to it ;) Was going to suggest the same with Minus
That was perfect, thank you sooo much, exactly what i needed. Just curious though what exactly does this line "where t.id is null" , i don't it would ever see a null value there since the id's are auto-increment, and they would never be null
This code joins all the possible combinations of gw and peer to the table. What you want is the non matching pairs because the matching exist in the table. These are the rows with id is null.
This does not mean that there are nulls in the column id. Just that there is not such an id.
Ohh ok i understand now, thank you very much for the explanation, and i just accepted the answer
0

The idea is to do a cross join to get all the rows and then filter out the ones that exist. There are several ways to do the filtering. I usually use left join, although not exists,not in, andexcept` are all reasonable alternatives.

If all the peers and gws are in the original table, then you can use that. However, you suggests that there is a separate list, at least for the gw values. You can list those explicitly:

select p.peer, g.gw
from (select distinct peer from t) p cross join
     (values ('GW1'), ('GW2'), ('GW3'), ('GW4')) g(gw) left join
     t
     on t.peer = p.peer and t.gw = g.gw
where t.peer is null;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.