简体   繁体   中英

Inline View Qustion

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
jon@a.com
art@b.com

Then I have a table, called UPLOAD_HISTORY. Sample data:

EMAIL        FILETYPE   FILENAME
jon@a.com    1          file1.txt 
jon@a.com    2          filex.txt 
jon@a.com    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
jon@a.com    1          file1.txt
jon@a.com    2          filex.txt
             3
jon@a.com    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
jon@a.com    1          file1.txt     1
jon@a.com    2          filex.txt     1
jon@a.com    3                        0
jon@a.com    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
jon@a.com    1          file1.txt     1
jon@a.com    2          filex.txt     1
jon@a.com    3                        0
jon@a.com    4          fileb.txt     1
art@b.com    1                        0
art@b.com    2                        0
art@b.com    3                        0
art@b.com    4                        0

However, I could ask for just File Type 1:

EMAIL     FILETYPE     FILENAME    STATUS
jon@a.com    1          file1.txt     1
art@b.com    1                        0

Thanks

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM