Given table ‘x’:
Source Dest Type
A B 2
A D 2
B C 2
Now I want the total count of Source and destination removing the matching ones..
Example of above one: For type 2, Count will be 4, i.e. Count(A,B,C,D)
I tried this:
select Count(distinct Source), Count(distinct destination),Count(distinct source)+Count( distinct destination),Type
from X
where Type=2 and Src NOT IN (select destination
from X
where Type=2)
I need to simplify this query for all the types.
Let me know if there is any way I could do it.
Thanks!
An inefficient method would be to do the following
This would give with your example data 1 row with type 2 and quantity 4
In an ideal world I’d look at the database design as it feels as if it should be split into multiple tables i.e. one for sources (as you seem to be grouping “source” and “destination” which indicates they are effectively the same item). If you are able to normalise the design further this type of query will be easier to do more efficiently