-1
select 
    IMEI_ID, dev_status, nvl(count(1), 0) AS Count
from 
    device
where 
    IMEI_ID IN (284700, 250779, 320700, 339622, 323400)
    and Dev_status = 'ACTIVE'
group by 
    IMEI_ID,dev_status;

For a few IMEI_ID, no rows are returned. I want the count to be Zero (0) for these missing rows.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

For Oracle

with i(ime_id) as
( 
    SELECT 284700 FROM DUAL UNION ALL
    SELECT 250779 FROM DUAL UNION ALL
    SELECT 320700 FROM DUAL UNION ALL
    SELECT 339622 FROM DUAL UNION ALL
    SELECT 323400 FROM DUAL 
)
select i.IMEI_ID, d.dev_status, nvl(count(d.ime_id),0) AS Count
from i
left join device d on i.ime_id = d.IMEI_ID
   and d.Dev_status='ACTIVE'
group by i.IMEI_ID, d.dev_status;
Serg
  • 22,285
  • 5
  • 21
  • 48
0

Create a (temporary) table with the requested IMEI_ID and then LEFT OUTER JOIN on the device:

SELECT          imei_ids.imei_id,
                device.dev_status,
                COUNT(device.dev_status)
FROM            imei_ids
LEFT OUTER JOIN device
             ON imei_ids.imei_id = device.imei_id
            AND device.dev_status = 'ACTIVE'
GROUP BY        imei_ids.imei_id,
                device.dev_status;
Robert Kock
  • 5,795
  • 1
  • 12
  • 20