简体   繁体   中英

NOT IN/ NOT EXISTS || I am trying to get the IDs missing in a specific date. The table now tracks date and ID if manifested only

I have a table with Date, ID and Volume such as in below:

Date ID Volume
23/11/22 999 10
23/11/22 888 10
23/11/22 777 10
22/11/22 888 10
22/11/22 777 10

I would like the ID to appear in the ID column in 22/11/22 even though it manifested 0 volume and to create a flag such as below telling Day over Day (DoD) which ones are "Not Active":

Date ID Volume Status
23/11/22 999 10 Active
23/11/22 888 10 Active
23/11/22 777 10 Active
22/11/22 999 0 Inactive
22/11/22 888 10 Active
22/11/22 777 10 Active

I attempted the following but return 0 rows and I do not know what to do next..

` select distinct csmp.date csmp.mcid as id,count(*) as volume

from volume csmp

where id NOT IN
       (select csmp.mcid
        from calc.swa_manifested_packages csmp               
        GROUP BY csmp.mc
        )

GROUP BY
csmp.mcid`

thank you all!

try

select 
    a.Date
    ,b.id
    ,coalesce(c.volume,0) volume
    ,coalesce(c.status,'Inactive') volume
from 
(Select distinct Date from volume) a
cross join (Select distinct id from volume) b
left join volume c
on a.date=b.date
and b.id=c.id

Basically we create all combinations of ID and DATE, and left join them on the volume table.

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