This should be an easy one.
I have a table with rows:
CREATE TABLE FURNITURE
(ROOM VARCHAR2(50) CONSTRAINT PK_DEPT PRIMARY KEY,
APPLIANCE VARCHAR2(50));
INSERT INTO FURNITURE VALUES
('BEDROOM','BED');
INSERT INTO FURNITURE VALUES
('LIVING ROOM', 'SOFA');
INSERT INTO FURNITURE VALUES
('TV ROOM', 'TV');
INSERT INTO FURNITURE VALUES
('ALL', 'LAMP');
The desired output is:
| Room | Appliance |
|---|---|
| BEDROOM | BED |
| BEDROOM | LAMP |
| LIVING ROOM | SOFA |
| LIVING ROOM | LAMP |
| TV ROOM | TV |
| TV ROOM | LAMP |
(sorting doesn't matter).
I have one very naive way of doing it:
select f1.room,
f1.appliance
from furniture f1 where room <> 'ALL'
union
select all_rooms.room,
f2.appliance
from furniture f2
cross join
(select distinct room from furniture where room <> 'ALL') all_rooms
where f2.room = 'ALL';
Surely there's a better way?
I was trying to come up with a solution using CROSS APPLY but I'm failing.
SQL Fiddle at http://sqlfiddle.com/#!4/0db734/10