SQL Interview Question - Solution (Part - XXV) |
HTML-код
- Опубликовано: 8 сен 2024
- #dataanalyst #sqlfordataengineer #education #dataanalytics
Here are My profiles that will definitely help your preparation for data analyst or data engineer roles.
Medium: / mahendraee204
Github: github.com/mah...
Table Create and insert statements:
----------------------------------------------------------
create table items (item varchar(20), item_count int)
insert into items values ('Ball', 2),('Bat', 4), ('Glouse', 1), ('Wickets', 3)
with cte as (
select item,item_count,1 as num from items
union all
select item,item_count,num+1 from cte
where num+1
With cte as
(
select item,item_count from items
union all
select item,item_count-1 from cte
where item_count>=2
)
select * from cte
order by item,item_count
My Solution:
with recursive cte as (
select item,item_count, 1 as level from items
union all
select cte.item,cte.item_count-1, (level+1) as level
from cte
where cte.item_count>1
)
select cte.item,cte.item_count from cte
order by cte.item,cte.item_count;
hi sir i used this code for solving this problem:
code:
with recursive cte as
(
select item,1 as total
from items
union all
select distinct c.item,total + 1 as total
from cte c join items i on c.total < i.item_count and c.item = i.item
)
select *
from cte
order by item,total
with cte as(select item,item_count from items
union all
select item,item_count-1 from cte where item_count>1)
select * from cte order by item,item_count
with r_cte as
(select item,1 as number,item_count from items
union all
select item,number+1,item_count from r_cte
where number
with cte as(
select item,item_count from items1
union all
select cte.item,cte.item_count-1 from cte inner join items1 i on cte.item=i.item where cte.item_count>1
)
select item,item_count from cte order by item,item_count
Sir i am beginner in SQL
Request to post beginner tutorial 🙏🏻please sir
I want to learn SQL
with cts AS (
select item , item_count, 1 AS Ctn From items
union all
select c.item , c.item_count ,c.ctn+1 from items i
join cts c on c.item_count =i.item_count and i.item_count>c.Ctn
) select item, ctn From cts
order by item
;with cte as
(
select item, sum(item_count) total_count, 1 as level from items_item
group by item
union all
select item, total_count, level+1 from cte
where level+1
Looks more optimized than my solution
PL SQL Developer Tool
With cte as
(
Select level as lvl from dual connect by level =lvl;
WITH RECURSIVE CTE AS(SELECT *, 1 AS JUM
FROM ITEMS
UNION ALL
SELECT C.ITEM,C.ITEM_COUNT-1, JUM+1 AS LEVEL
FROM CTE AS C
JOIN ITEMS AS I
ON C.item_count = I.item_count
WHERE C.ITEM_COUNT > 1)
SELECT * FROM CTE
ORDER BY 1;
WITH recursive cte1 AS (SELECT item,item_count,1 as number FROM items
UNION ALL
SELECT item,item_count,number+1 FROM cte1
WHERE number
Data analyst interview prep krva do
Okay
For me its quite tough cte kaha se padu