I have a table with Building name, such as A, B, C. These building names could be repeated. I have another column which has the floor. Such as floor 1 ,floor 2. Again there could be multiple floor 1 for every building. There is a third column which has object present such as television, bricks, fans. I want to check for every combination of building with corresponding floors, such as Building A - floor 1, Building A- floor 2, if an object 'brick' exists then 'wall' must exist.
EXAMPLE DATA: For every building and location, if 'WALL' exists , 'WINDOW', 'WINDOW1' or 'WINDOW2' must exist, ELSE FLAG
BUILDING LOCATION OBJECT
A FLOOR1 WALL
A FLOOR1 WINDOW
A FLOOR2 WALL
B FLOOR1 WALL
C FLOOR1 WALL
C FLOOR1 WINDOW
DESIRED OUPUT
BUILDING LOCATION ACTION
A FLOOR2 FLAG
B FLOOR1 FLAG
I have tried using GROUP BY, DISTINCT, WHERE EXISTS, but I cant seem to come up with the right logic.
nested tables is what you want. Something like
fyi: I reccomend you use this as a start, but for your exact case, id imagine this needs to be modified
You can
group by building, location
for the rowswhere object in ('WALL', 'WINDOW')
:The condition
count(distinct object) < 2
in thehaving
clause returns combination ofbuilding, location
where'WALL'
and'WINDOW'
do not both exist.See the demo.
Results:
Or with NOT EXISTS:
See the demo.