I have a table that can contain three different types of files. If file type A is present, select A; otherwise, if file type B is present and there is no Cs type with the same client_id, select B, otherwise select type C.
Some other magic will happen later, which will remove the selected file from the table.
I have the following table in an Oracle 10g SQL database:
ID | TYPE | CLIENT_ID file1 | A | 1 file2 | B | 1 file3 | C | 1 file4 | B | 2
and for those who want to follow at home, sqlfidde or sql:
create table files ( id varchar(8) primary key, type varchar(4), client_id number ); insert into files values ('file1', 'A', 1); insert into files values ('file2', 'B', 1); insert into files values ('file3', 'C', 1); insert into files values ('file4', 'B', 2);
I hope to create a big nasty request to capture the following file based on the above criteria, which should lead to the following order if the request was run four times:
The attempt that made me the most remote was to write three separate queries for each type:
--file type 'A' selector select * from files where type = 'A' --file type 'B' selector select * from files where type = 'B' and client_id = ( select client_id from files group by client_id having count(*) = 1 ); --file type 'C' selector select * from files where type = 'C'
I want to check the number of rows returned after each, and if it is 0, use the following selection, but all in one SQL statement.
sql oracle oracle10g select
cazzer
source share