Click here to Skip to main content
15,883,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys,

Today I faced a strange question in interview.I can't even have any idea of it how to do

Question is

I have a student table.

SQL
Create table #student(sname varchar(20),medium varchar(20),class varchar(20))

insert into #student
values
('std1','Hindi','class-1'),
('std2','Hindi','class-1'),
('std3','Hindi','class-1'),
('std4','English','class-1'),
('std5','English','class-1'),
('std6','Hindi','class-2'),
('std7','Hindi','class-2'),
('std8','french','class-3'),
('std9','french','class-3'),
('std10','french','class-3'),
('std11','russian','class-3'),
('std12','russian','class-3')


this table has student name ,medium of study and class.

Now he wants resultset some thing like below format in a single column.

class
medium
students under this class and medium

like
DETAILS
class-1
English
std1   
std2   
std3   
class-1
Hindi  
std4   
std5   
class-2
Hindi  
std6   
std7   
class-3
french 
std8   
std9   
std10  
class-3
russian
std11  
std12  


He gave me a clue that use CTE's and level concepts.

Anyhow I got rejected because of this.But I want to know the solution

Could any one help me out how to achieve this
Posted

1 solution

This article[^] from Pinal Dave explains how to use Recursive CTEs to produce the Hierarchical results that you are after.

I came back to this to give a fuller explanation in light of your specific question as it can also be achieved using nested CTEs.

I decided that my anchor statement was
select distinct medium as detail, 2 as detlev, medium, class from #student
as I can easily get the class names and I know they will be level 1.
To get my recursion I added
SQL
UNION ALL

SELECT S.sname as detail, M1.detlev + 1, s.medium, S.class FROM #student S
INNER JOIN CTE1 M1 ON S.class = M1.Class and S.Medium = M1.detail
which gave me results of
English	2	English	class-1
french	2	french	class-3
Hindi	2	Hindi	class-1
Hindi	2	Hindi	class-2
russian	2	russian	class-3
std11	3	russian	class-3
std12	3	russian	class-3
std6	3	Hindi	class-2
std7	3	Hindi	class-2
std1	3	Hindi	class-1
std2	3	Hindi	class-1
std3	3	Hindi	class-1
std8	3	french	class-3
std9	3	french	class-3
std10	3	french	class-3
std4	3	English	class-1
std5	3	English	class-1
I then needed to UNION those results with the class names
select distinct class as detail, class, CAST(NULL as varchar(20)) as medium, 1 as detlev from #student
so my entire query became
SQL
;WITH CTE1 AS
(
    select distinct medium as detail, 2 as detlev, medium, class from #student

    UNION ALL

    SELECT S.sname as detail, M1.detlev + 1, s.medium, S.class FROM #student S
    INNER JOIN CTE1 M1 ON S.class = M1.Class and S.Medium = M1.detail
),
CTE2 AS
(
    SELECT DISTINCT detail, class, medium, detlev FROM CTE1 c1
    UNION ALL
    select distinct class as detail, class, CAST(NULL as varchar(20)) as medium, 1 as detlev from #student
)
SELECT detail FROM CTE2
order by class, medium, detlev,detail
giving me my final results of
class-1
English
std4
std5
Hindi
std1
std2
std3
class-2
Hindi
std6
std7
class-3
french
std10
std8
std9
russian
std11
std12
Note these results are different to those in your original question - that threw me for a moment until I determined that the expected results you quoted are not quite right given the data supplied.
 
Share this answer
 
v2
Comments
Maciej Los 31-Mar-15 16:03pm    
5ed!
CHill60 31-Mar-15 17:29pm    
Thank you! I think I got a little carried away with this one ;-)
Maciej Los 1-Apr-15 1:41am    
;)
Member 11283674 1-Apr-15 4:47am    
superb... I really every time learning very good things from here.I really appreciate for your logic. Thanks for sharing knowledge.
CHill60 1-Apr-15 4:57am    
My pleasure!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900