Reputation: 839
Perhaps someone can assist with designing a query to meet these requirements. I'm going to need this to be part of an inline view which will be joined to a larger query.
I have the basic table of email addresses:
EMAIL
[email protected]
[email protected]
Then I have a table, called UPLOAD_HISTORY. Sample data:
EMAIL FILETYPE FILENAME
[email protected] 1 file1.txt
[email protected] 2 filex.txt
[email protected] 4 fileb.txt
Then there is the table of file types:
FILE ID FILE TYPE
1 TYPE A
2 TYPE B
3 TYPE C
4 TYPE D
If I perform a query, and outer join the UPLOAD_HISTORY and FILE_TYPES, I would get something like this:
EMAIL FILETYPE FILENAME
[email protected] 1 file1.txt
[email protected] 2 filex.txt
3
[email protected] 4 fileb.txt
What I need for the missing record, is to fill in the missing values from the UPLOAD_HISTORY table. My ideal result set would look like this:
EMAIL FILETYPE FILENAME STATUS
[email protected] 1 file1.txt 1
[email protected] 2 filex.txt 1
[email protected] 3 0
[email protected] 4 fileb.txt 1
I am looking to get all file types, an email for every line, and a status of 1 if there is a record for that file type, or 0 if there is not........
Sometimes there may be criteria passed. I may ask for a specific file type, or not.
If a particular email does not have any entries in the UPLOAD_HISTORY table, is it possible to get empty records?
EMAIL FILETYPE FILENAME STATUS
[email protected] 1 file1.txt 1
[email protected] 2 filex.txt 1
[email protected] 3 0
[email protected] 4 fileb.txt 1
[email protected] 1 0
[email protected] 2 0
[email protected] 3 0
[email protected] 4 0
However, I could ask for just File Type 1:
EMAIL FILETYPE FILENAME STATUS
[email protected] 1 file1.txt 1
[email protected] 1 0
Thanks
Upvotes: 0
Views: 44
Reputation: 146299
Use a CROSS JOIN on email
and file_type
to get all the combinations, then LEFT OUTER JOIN on upload_history
to get the results you want.
Assuming there is at most one record in upload_history
for each combination this should work nicely:
select eft.email
, eft.filetype
, uh.filename
, nvl2(uh.filename, 1, 0) as status
from ( select email, file_id, filetype
from email
cross join file_type ) eft
left outer join upload_history uh
on uh.email = eft.email
and uh.filetype = eft.filetype
order by eft.email
, eft.filetype
You haven't specified what you want to happen if there are multiple records in upload_history
for each combination. Maybe one row per filename
is correct. But the purpose of the status
column seems a bit redundant.
Upvotes: 0
Reputation: 425238
Left join from file types to history:
select EMAIL, FILETYPE, FILENAME, nvl(STATUS, 0) as STATUS
from file_type
left join upload_history on FILETYPE = FILE_ID
Upvotes: 0