July 26, 2019 at 7:42 am
create table #testtbl(EmpName varchar(50),Item varchar(40))
insert into #testtbl values('ram','XX')
insert into #testtbl values('madhu','XX')
insert into #testtbl values('madhu','YY')
insert into #testtbl values('rahul','XX')
insert into #testtbl values('rahul','YY')
insert into #testtbl values('rahul','ZZ')
select * from #testtbl
I need following Output
EmpName item1 item2 item3
ram xx - --
madhu xx yy -
rahul xx yy zz
In this above output empname and items is dynamic value.not a static values.Please share me query.
July 26, 2019 at 10:11 am
What was your answer first? It's no good us answering your interview questions for you, it's not us sitting the interview.
If you tell us what you think the answer is though, we'll be happy to discuss your answer, and give you other ideas, pointers, etc.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
July 26, 2019 at 11:49 am
If you knew the items were fixed you could get them back using a PIVOT query:
SELECT *
FROM #testtbl src
PIVOT (MAX(Item) FOR Item IN (XX, YY, ZZ)) as piv
As you say they are dynamic you need to generate that SQL from the contents of the table then execute it:
DECLARE @Items nvarchar(MAX) =
(SELECT STUFF((SELECT DISTINCT ', ' + Item
FROM #testtbl
ORDER BY 1
FOR XML PATH(''),TYPE).value('.','nvarchar(MAX)'),1,2,''))
DECLARE @Sql nvarchar(MAX) =
'SELECT *
FROM #testtbl src
PIVOT (MAX(Item) FOR Item IN (' + @Items + ')) as piv'
EXEC(@Sql)
July 26, 2019 at 3:17 pm
If you knew the items were fixed you could get them back using a PIVOT query:
SELECT *
FROM #testtbl src
PIVOT (MAX(Item) FOR Item IN (XX, YY, ZZ)) as pivAs you say they are dynamic you need to generate that SQL from the contents of the table then execute it:
DECLARE @Items nvarchar(MAX) =
(SELECT STUFF((SELECT DISTINCT ', ' + Item
FROM #testtbl
ORDER BY 1
FOR XML PATH(''),TYPE).value('.','nvarchar(MAX)'),1,2,''))
DECLARE @Sql nvarchar(MAX) =
'SELECT *
FROM #testtbl src
PIVOT (MAX(Item) FOR Item IN (' + @Items + ')) as piv'
EXEC(@Sql)
I'm not convinced that this needs to be dynamic. He's not updating the columns names based on the values, but rather using Item1, Item2, and Item3 regardless of the values, which can be accomplished with a ROW_NUMBER()
.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 26, 2019 at 3:20 pm
Like so:
SELECT EmpName,
MAX(CASE WHEN row_num = 1 THEN Item END) AS item1,
MAX(CASE WHEN row_num = 2 THEN Item END) AS item2,
MAX(CASE WHEN row_num = 3 THEN Item END) AS item3,
MAX(CASE WHEN row_num = 4 THEN Item END) AS item4
FROM (
SELECT EmpName, Item, ROW_NUMBER() OVER(PARTITION BY EmpName ORDER BY Item) AS row_num
FROM #testtbl
) AS derived
GROUP BY EmpName
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
July 26, 2019 at 3:24 pm
In this above output empname and items is dynamic value.not a static values.Please share me query.
This is an interview and this is actually pretty basic stuff. So, no... I'm not going to do you the disservice of simply providing you a query. What I will do is give you the opportunity to learn. Please see the following articles and then you solve the given problem. You are, after all, going to have to be able to actually do such things if you get the job.
https://www.sqlservercentral.com/articles/cross-tabs-and-pivots-part-2-dynamic-cross-tabs
--Jeff Moden
Change is inevitable... Change for the better is not.
July 26, 2019 at 3:46 pm
Like so:
SELECT EmpName,
MAX(CASE WHEN row_num = 1 THEN Item END) AS item1,
MAX(CASE WHEN row_num = 2 THEN Item END) AS item2,
MAX(CASE WHEN row_num = 3 THEN Item END) AS item3,
MAX(CASE WHEN row_num = 4 THEN Item END) AS item4
FROM (
SELECT EmpName, Item, ROW_NUMBER() OVER(PARTITION BY EmpName ORDER BY Item) AS row_num
FROM #testtbl
) AS derived
GROUP BY EmpName
That's a good solution, and I think the correct one. One of the differences is that it won't necessarily have the same items in each column so, for example, you could have XX and YY in the same column. Whereas the pivot method ensures that each item code has its own column.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply