Skip to content
Advertisement

How to ungroup the grouped record based on count column using SQL

I have below data set obtained after group by dept and loc.

dept id loc count

100 a 2

300 c 1

I need out put like below

deptid loc

100 a

100 a

300 c

please help

Advertisement

Answer

Another option is to use Recursive Queries to get the desired result.

with cte(deptid, loc, cnt) as 
(select deptid, loc, cnt - 1 from dept
union all
select cte.deptid, cte.loc, cte.cnt - 1
from cte, dept
where dept.deptid = cte.deptid and dept.loc = cte.loc
and cte.cnt > 0)
select deptid, loc from cte order by deptid, loc;

DB Fiddle Link: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=0580680ecf0b47b4933e3a4731b62b4a

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement