Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 October SQL Puzzle: SQL Advance Query – Print comma separated Divisions

SQL Puzzle: SQL Advance Query – Print comma separated Divisions

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to print comma separated divisions for all the employees.

Input data for Division Table:

1
2
3
4
5
6
divids divisionname
----------- -------------------------
1 Animation
2 Database
3 Production
4 Tester

Input data for Employee Table:

1
2
3
4
5
6
7
8
empid empname divids
----------- ---------- ----------
1 Anvesh 1,2
2 Roy 1,2,3
3 Martin 2,4
4 Jenny 3
5 Nupur 3,4
6 Rajesh 1,4

Expected Output:

1
2
3
4
5
6
7
8
empid empname RecruiterNames
----------- ---------- ---------------------------------
1 Anvesh Animation, Database
2 Roy Animation, Database, Production
3 Martin Database, Tester
4 Jenny Production
5 Nupur Production, Tester
6 Rajesh Animation, Tester

Create sample tables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
CREATE TABLE tbl_emp
(
empid INT
,empname VARCHAR(10)
,divids VARCHAR(10)
)
GO
CREATE TABLE tbl_divisions
(
divids INT
,divisionname VARCHAR(25)
)
GO
 
Insert Into tbl_divisions Values
(1,'Animation'),(2,'Database'),(3,'Production'),(4,'Tester')
GO
Insert Into tbl_emp Values
(1,'Anvesh','1,2')
,(2,'Roy','1,2,3')
,(3,'Martin','2,4')
,(4,'Jenny','3')
,(5,'Nupur','3,4')
,(6,'Rajesh','1,4')
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
;WITH CTE AS
(
SELECT empid,empname,divids FROM
(
SELECT empid,empname,CAST((''+replace(divids,',' ,'')+'') AS XML) xmlcol
FROM tbl_emp
) s
OUTER APPLY
(
SELECT ProjectData.D.value('.', 'VARCHAR(2)') divids
FROM s.xmlcol.nodes('X') ProjectData(D)
) a
)
,CTE1 AS
(
SELECT empid,empname,divisionname
FROM CTE c
INNER JOIN tbl_divisions r on r.divids = c.divids
)
SELECT empid, empname,
STUFF
((
SELECT ', ' + a.divisionname
FROM CTE1 a
WHERE ( a.empid = b.empid )
FOR XML PATH('')
) ,1,2,'')
AS RecruiterNames
FROM CTE1 b
GROUP BY empid, empname
ORDER BY empid

Please try the different solution for this puzzle and share it via comment...

Oct 7, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Solve the challenge of DISTINCTSQL Puzzle: SQL Advance Query - Find the Column name which has all NULL
Comments: 2
  1. Dinesh I.S
    October 10, 2017 at 12:51 pm

    —Method-1
    ;
    WITH CTE
    As
    (
    Select A.EmpID,empname,B.Divids From (
    Select EMpID,empName,Cast(”+Replace(Divids,’,’,”)+” As XML) As XMLData
    From TBL_EMP) A
    Outer Apply (Select r.value(‘.’,’NvarChar(max)’) As Divids
    From XMLData.nodes(‘t’) As Records(r)
    ) B
    )
    Select EmpID,EmpName,(Select Convert(VarChar(500),DivisionName)+’,’ From CTE C Inner JOin TBL_Divisions D On D.Divids=C.Divids Where C.EmpID=A.EmpID For XML PATH(”)) As Divisions
    From TBL_Emp A

    ReplyCancel
  2. Dinesh I.S
    October 10, 2017 at 12:52 pm

    –Method-2
    Declare @DivisionsDetails Table(EmpID Int,DividsID Int)
    Declare @EmpID Int;
    Select @EmpID=Min(EmpID) From TBL_Emp
    Declare @Len As Int;
    Declare @Divids VarChar(4000)
    While @EmpID Is Not Null
    Begin

    Select @Divids=Divids From TBL_Emp Where EmpID=@EmpID

    While Len(@Divids)>0
    Begin
    Set @Len=(Case CharIndex(‘,’,@Divids) When 0 Then Len(@Divids) Else CharIndex(‘,’,@Divids)-1 End)
    Insert Into @DivisionsDetails
    Values(@EmpID,SubString(@Divids,1,@Len))

    Set @Divids=(Case Len(@Divids)-@Len When 0 Then Null Else Right(@Divids,Len(@Divids)-@Len-1) End)
    End

    Select @EmpID=Min(EmpID) From TBL_Emp Where EmpID>@EmpID
    End

    Select EmpID,EmpName,(Select Convert(VarChar(500),DivisionName)+’,’ From TBL_Divisions D Inner JOin @DivisionsDetails DL On DL.DividsID=D.Divids Where DL.EmpID=E.EmpID For XML PATH(”)) As DivisionName
    From TBL_EMP E

    ReplyCancel

Leave a Reply to Dinesh I.S Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 7, 2017 2 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....