A shorter solution-- with recursive cte as ( select id, item_name, total_count, 1 as cnt from travel_items union select id, item_name, total_count, cnt+1 from cte where cte.total_count>=cnt+1 ) select id, item_name from cte order by 1;
@sameerkumardash57952 жыл бұрын
with recursive CTE as ( select id,item_name,total_count from travel_item union select id,item_name,total_count-1 from cte where total_count>1 ) select id,item_name from cte order by 1
@perumala41672 жыл бұрын
We can use generate_series simplying the query Select Id,item_name from transaction Group by Id,item_name,generate_series(1,toatlcount)
@hanishadua474 Жыл бұрын
Thank you for this solution , Thoufiq. Very new concept to me. I am still stuck as to why we have rows executed for zero count (when you had cte.total_count>0), technically it should not give any rows which are equal to zero as well.
@ShivamGupta_15 Жыл бұрын
Let's say 'x' should be printed 1 time. After the first iteration is printed (base query) and we reach the recursive query, the cte.total_count is still 1 and it will again print x because where condition has cte.total_count>0. Hence we get x printed twice which is wrong. Here we need to keep in mind that before coming to the recursive query we already have an iteration printed. P.S, writing this so that I don't forget when I'll come back to this video.
@hanishadua474 Жыл бұрын
@@ShivamGupta_15 Oh okay. Wrapping my head around it. Thank you for replying. Big help :)
@Santanu.Bhowmick Жыл бұрын
Where clause executes first then the select clause. When the count is actually 1, it satisfies the condition of ">0" and it went to select clause which printed 1-1=0.
@gauravsingh-gn4zz Жыл бұрын
@@ShivamGupta_15 or we can use count -2 in second iteration
@joefromdc2 жыл бұрын
That's a lot. Glad we got power query and Tableau prep 🤣
@sayantabarik42522 жыл бұрын
Hi Thoufiq, I have a question that why there is a need of using join in the recursive query for this solution. We can use the data from cte itself for the next iteration by reducing the total_count-1 and we can put total_count>1 as terminating condition. Please find the below solutions. Please let me know if this will cause any issue. with cte as ( select id,item_name,total_count from Travel_items union all select id,item_name,total_count-1 from cte where total_count>1 ) select id,item_name from cte order by id;
@techTFQ2 жыл бұрын
Absolutely right.. that’s a good catch.. join was unnecessary in this case..
@sayantabarik42522 жыл бұрын
@@techTFQ Thanks for the response !!
@rahulb39932 жыл бұрын
Also, the level you have used is it required as I can see it is not of use in the query or may be I'm wrong
@sayantabarik42522 жыл бұрын
@@rahulb3993 That is not required. that is for better understanding how recursive cte works. He clearly mentioned that in the video.
@deepapanneerselvam46182 жыл бұрын
@@rahulb3993 That is for the people understanding
@My_buddy_942 жыл бұрын
HI @techTFQ nice explanation i tried in ms sql ;with cta_sample as( select s.num, s.nameoffruit,s.total from sampel as s union all select cs.num,cs.nameoffruit,cs.total-1 from cta_sample as cs where cs.total>1 ) select num , nameoffruit from cta_sample order by num it worked for me
@kamalrock12 жыл бұрын
Thank you Thoufiq for this!🙏🏻 You are such a amazing tutor… 🙌🏻
@varunas97845 ай бұрын
Good one Thoufik! Here's my solution on MS SQL server solved via temp table variables ================================================ declare @t table (id int, item varchar (10)) declare @id int declare @itr int declare @cnt int set @cnt = 1 set @id = 1 while @id
@hilarylomotey70512 жыл бұрын
always on point .U r the best have a wonderful day and thanks for this
@techTFQ2 жыл бұрын
Thank you Hilary 🙏🏼
@shubhamagrawal7068 Жыл бұрын
No need to use join in recursive part. It can be done without using join : - WITH RECURSIVE cte AS( SELECT id, item_name, total_count, 1 AS cnt FROM travel_items UNION ALL SELECT id, item_name, total_count, (cnt+1) AS cnt FROM cte WHERE total_count > cnt ) SELECT id, item_name FROM cte ORDER BY 1
@fahadmahmood71502 жыл бұрын
Superb Explanation 👌
@techTFQ2 жыл бұрын
Thank you ☺️
@avi80162 жыл бұрын
Great video as usual 💯
@techTFQ2 жыл бұрын
Thank you buddy
@venkatkrishnan21722 жыл бұрын
Using temp tables, Declare @cnt int=1; Declare @cnt1 int=1; Declare @totalrows int; Select @totalrows = count(1) from travel_items; Declare @stg int; With cte as ( Select *, row_number() over(order by (select 1)) as rn from travel_items ) Select * into #temp from cte While (@cnt
@swapnilsolanki85952 жыл бұрын
I remeber this question being posted in discord channel👍
@techTFQ2 жыл бұрын
Is it.. I saw it in LinkedIn.. but a pretty good problem
@chintanmistri77472 жыл бұрын
Yupp @Swapnil Solanki, I have put it on discord channel
@chintanmistri77472 жыл бұрын
@@techTFQ Solved this question using almost same approach and posted on your discord channel, All hail to you for teaching the recursive queries in the best possible way, keep doing good work. Cannot solve this query without having foundations clear which you did in your previous videos. Thanks a lot🙌
@techTFQ2 жыл бұрын
Thanks Chintan, I realized the join was unnecessary I’m in this solution… if you have a better solution then kindly share so I can add it to my blog so others can benefit
@raghuveersangashetty48452 жыл бұрын
Oracle: select id,item_name from items i cross join lateral (select level from dual connect by level
@techTFQ2 жыл бұрын
Nice, thanks for sharing
@uditkumarpatra52362 жыл бұрын
Hey Toufique . You are a great teacher, just asking it would be really nice if you put a top 100 sql questions, that you have come across. If possible can your upload those on your blog.
@techTFQ2 жыл бұрын
Hopefully I’ll have 100 queries in the next few months in all my sql queries playlist
@kennethoppong18812 жыл бұрын
@@techTFQ I will look forward towards that sir, thank you for helping us out
@mayurjoshi14522 жыл бұрын
Sir do you have SQL database for practice, I'm practicing SQL do something for friend. 💐🙏
@Tarasankarpaul12 жыл бұрын
You are the best 💝
@techTFQ2 жыл бұрын
Thank you buddy
@fathimafarahna26332 жыл бұрын
Awesome as always👌🏻
@techTFQ2 жыл бұрын
Thank you 🙏🏼
@ShubhamGautam-k1k4 ай бұрын
from oracle : this will also work select id, item_name from (select id, item_name, total_count from table union select id, item_name, total_count-1 from table where total_count>0)
@brianligat9493 Жыл бұрын
You get the "extras" because of the base query AND the recursive part, so the recursive part just needs to one less than you first thought. Just reducing the total count is enough for the logic - no need for the level apart from for explanation.
@venkateshdeekonda86652 жыл бұрын
I have used for loop within another for in Oracle DB. One to select all items. Another to loop within the item count number.
@techTFQ2 жыл бұрын
That’s works too
@alagarrajkalaivanan26442 жыл бұрын
Can you please share the code?
@venkateshdeekonda86652 жыл бұрын
@@alagarrajkalaivanan2644 /*Ungroup the data*/ declare n number; u number; cursor c is select * from travel; begin for i in c loop n := i.item_count; u := n/n; for j in 1..n loop dbms_output.put_line(i.id||','||i.item||','||u); end loop; end loop; end;
@dantushankar44705 ай бұрын
short solution : select id, item_name from travel_items t, generate_series(1,t.total_count);
@subodhthore6454 Жыл бұрын
My solution after knowing it can be solved using Recursion: with recursive cte as ( select id,item_name,total_count, 1 as ct from travel_items union all select id,item_name,total_count, ct+1 as ct from cte where total_count>=ct+1 ) select id,item_name from cte order by id; -- first iteration with cte as (select id,item_name,total_count, 1 as ct from travel_items), -- second iteration cte1 as (select id,item_name,total_count, ct+1 as ct from cte where total_count>=(ct+1)), -- third iteration cte2 as (select id,item_name,total_count, ct+1 as ct from cte1 where total_count>=ct+1), -- fourth iteration cte3 as (select id,item_name,total_count, ct+1 as ct from cte2 where total_count>=ct+1) -- fifth iteration select id,item_name,total_count, ct+1 as ct from cte3 where total_count>=ct+1
@sameerkumardash57952 жыл бұрын
This is a better solution i guess with recursive CTE as ( select id,item_name,total_count from travel_item union select id,item_name,total_count-1 from cte where total_count>1 ) select id,item_name from cte order by 1
@souhailmainsi7800 Жыл бұрын
Why didn't you use the generate_series function in this case ?
@udaykumarakavachanam79152 жыл бұрын
I solved the problem using plpgfunction ------------------------------ create or replace function count_split() returns table(name varchar,id int) language plpgsql as $$ declare i record; count_1 int =1; begin create temporary table fruits (name_1 varchar(250) ,id_1 int); for i in select * from count_split loop count_1 = 1; while count_1
@___bala84___2 жыл бұрын
Hai thoufiq....big fan of ur videos in recent times....learned a lot from ur videos......im working in data domain....im facing a issue like im not getting the difference between mysql and oracle sql....when i work on hackerrank there are some syntax differences between these two....can u pls make a video on difference between these two pls?
@techTFQ2 жыл бұрын
Thank you Bala. The difference is these rdbms is not much.. generally difference will be in certain functions.. some functions are supported only in oracle n some other only in MySQL.. let me see if I can come up with a video highlighting such differences
@___bala84___2 жыл бұрын
@@techTFQ It ll be really helpful if u do so.....Thanks a lot fr rplying♥️.....hoping to see u with many more subscribers🙂
@techTFQ2 жыл бұрын
Thank you so much 🙏🏼
@NewsbyAkki252 жыл бұрын
Hey Hi Thoufiq can u please make video on greatest and least function in Oracle SQL like eg there is this students table and in that columns like name , sub1.sub2.sub3 know i want to know in which subject the particular student has scored highest (not the marks but the subject name should be fetched) BTW You are doing a great job Thank you🙏
@techTFQ2 жыл бұрын
Thank you and let me consider this problem
@shivaroyal92922 жыл бұрын
So if you have the source table like below NAME PHYSICS CHEMISTRY MATHS ID SIVA 85 70 95 1 HARI 98 80 79 2 KRISHNA 75 81 92 3 SAMBA 86 98 95 4 Do you want the output like below right NAME SUBJECT SIVA MATHS HARI PHYSICS KRISH MATHS SAMBA CHEMISTRY ---------Try this Query--------- SELECT NAME,SUBJECT FROM (SELECT ID,NAME,'PHYSICS' AS SUBJECT,PHYSICS AS MARKS FROM STUDENTS UNION ALL SELECT ID,NAME,'CHEMISTRY' AS SUBJECT,CHEMISTRY AS MARKS FROM STUDENTS UNION ALL SELECT ID, NAME,'MATHS' AS SUBJECT,MATHS AS MARKS FROM STUDENTS) WHERE (NAME,MARKS) IN (SELECT NAME,MAX(MARKS) FROM( SELECT NAME,'PHYSICS' AS SUBJECT,PHYSICS AS MARKS FROM STUDENTS UNION ALL SELECT NAME,'CHEMISTRY' AS SUBJECT,CHEMISTRY AS MARKS FROM STUDENTS UNION ALL SELECT NAME,'MATHS' AS SUBJECT,MATHS AS MARKS FROM STUDENTS) GROUP BY NAME) ORDER BY ID;
@ManiS-pr8ui2 жыл бұрын
Hi Toufique, Can you make another video on CTE for selecting level 2, level 3 managers
@jeffreyelvir81882 жыл бұрын
Hey, new subscriber here, I just started my journey of learning SQL. First of all, amazing content! I like the way to explain and make sure that it is very clear. regarding this example, this is my solution: WITH RECURSIVE travel AS ( SELECT * FROM travel_items UNION SELECT id, item_name, total_count - 1 AS total_count FROM travel WHERE total_count > 1 ) SELECT id,item_name FROM travel ORDER BY item_name DESC; and it gave me the exact same result, is this correct? or am I missing something?
@iqbalmu2 жыл бұрын
Sir, can you now do the opposite, group given output data?
@techTFQ2 жыл бұрын
That should be pretty straight forward right
@iqbalmu2 жыл бұрын
@@techTFQ if you could post another video it would be greatly appreciated.
@techTFQ2 жыл бұрын
I’ll make other videos for this concept bro
@sheikhsheharyar61782 жыл бұрын
Hi Toufiq please make videos on Cosmos DB
@ameynaik14132 жыл бұрын
Use groupby clause over item name, and count fun
@sangrampujari9205 Жыл бұрын
please let me know if we can your materials in Udemy..
@ravurisudheer1762 жыл бұрын
How to reach out ur blog
@parveen81222 жыл бұрын
flow of code is not clear my solution with recursive cte as( select 1 as n ,id, item_name ,total_count from travel_items union all select n+1,id, item_name ,total_count from cte where n< total_count ) select id,item_name, total_count from cte
@vijay.s-ll1yq7 ай бұрын
with cte as (select id,iteam_name,total_count,1 as rnk from fruits union all select id,iteam_name,total_count,rnk+1 from cte where rnk+1
@sureshkct2 жыл бұрын
What is the tool name you are using writing query ?
@theshanerap2 жыл бұрын
pdAdmin
@vijaymulimath65192 жыл бұрын
Select I'd,item_name,count(*) Total_count from table group by I'd,item_name Order by I'd.............is it right?? Iam using Oracle
@rakeshd71312 жыл бұрын
in script , you used union all insted of union. please make it correct.
@techTFQ2 жыл бұрын
Both works… does not matter for this problem
@vishalsonawane.890511 ай бұрын
Done
@yashsaxena77542 жыл бұрын
Sharing an alternative solution(without join) if it helps someone. with recursive cte as (select id,item_name,1 as cnt from travel_items union select id,item_name,cnt+1 from cte where cnt
@vishalsonawane.89052 жыл бұрын
Thnx it's working
@viswa61264 ай бұрын
WITH RECURSIVECTE AS( SELECT id,item_name,total_count, 1 AS Iter FROM travel_items UNION ALL SELECT id,item_name,total_count, Iter+1 FROM RECURSIVECTE WHERE Iter < total_count ) SELECT id, item_name FROM RECURSIVECTE ORDER BY id
@Shivani-yk7tw8 ай бұрын
here is my solution with cte as ( select id,item_name,total_count, 1 as rn from travel_items union all select id,item_name,total_count, rn+1 from cte where rn+1
@bhavanapatel93682 жыл бұрын
I have already sent you a mail in which a problem I faced.
@janjanamsaibindu28402 жыл бұрын
Bro I have doubt .. please help me out for SQL query. questions is who renewal the policy more than one month gap Name. Effectivedate. Terminatdate Sekar 01-01-2019. 01-10-2020 Sekar. 01-12-2020. 31-12-9999 Rajesh. 01-01-2019. 01-10-2021 Rajesh. 01-11-2021. 01-10-2022 Satish. 01-01-2019. 01-10-2020 Sravan. 01-01-2019. 01-10-2020 Sravan. 01-12-2020. 31-12-9999 Consider terminatdate with Max is active & date format (dd-mm-yyyy)
@gokulprasandh10012 жыл бұрын
We need oracle jobs
@cristinasayavedra57212 жыл бұрын
Hello Toufiq! How are you? I am a data analyst from Argentina, Im bilingual ando have an accountant background, please, please Toufiq, can you help me find a job please, can I Give you my resume? Please
@Kindness-ys8gl2 жыл бұрын
fix your website please..
@user-lx1ck9bn2j4 ай бұрын
u are using same technique in every problem so ur every soln is too complicated take look simple sol with cte as (select id,item_name,total_count,1 as l from travel_items union all select id,item_name,total_count,l+1 as l from cte where cte.l