Skip to content
Advertisement

default rows from select query

I have a select query (Oracle database) with group by clause that needs to return 4 rows if all required data exist. It works perfectly. How can I write a select query that returns same 4 rows even if no data exist. I want the field C value to be zero if no data. Please let know. Thanks.

enter image description here

Advertisement

Answer

Is this what you want?

select x.a, x.b, count(t.a)
from (select 'ORG-1' as A, 'CAR' as b from dual union all
      select 'ORG-1' as A, 'BIKE' as b from dual union all
      select 'ORG-2' as A, 'CAR' as b from dual union all
      select 'ORG-2' as A, 'BIKE' as b from dual 
     ) x left join
     t
     on x.a = t.a and x.b = t.b
group by x.a, x.b
      
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement