Converting multiple rows to multiple columns in SQL

Converting multiple rows to multiple columns in SQL



I have a data something like this:


Team Month Count_of_Stores
a 4 10
b 4 4
c 4 6
a 5 8
b 5 14
e 5 9
a 6 7
b 6 3
f 6 1



I working to get an output something like this converting the rows to columns:



Team Month Count_of_Stores Team Month Count_of_Stores Team Month Count_of_Stores
a 4 10 a 5 8 a 6 7
b 4 4 b 5 14 b 6 3
c 4 6 e 5 9 f 6 1



I am sure pivot should be of great help here, but confused in the appropriate usage here. any help is much appreciated.






You can use conditional aggregation here but how do you know the order of the results? You have no way in the data posted to order the results.

– Sean Lange
Sep 17 '18 at 21:32






@SeanLange I actually do group it based on another criteria which is teams which i haven't mentioned here. So in that way the data is being ordered and grouped.

– Tamil
Sep 17 '18 at 21:35







OK but with the data you provided it is impossible to be consistent with ordering. Perhaps you can share some more details? Here is a great place to start.

– Sean Lange
Sep 18 '18 at 12:57






@SeanLange Thanks sean! Thats right. I think my data is incomplete and the code suggested by "IsItGreyOrGray" looks almost close below. What do you think?

– Tamil
Sep 18 '18 at 15:01






The code provided in that answer is about all you can get with no way to ensure ordering. In other words you will get values for months 4,5 and 6 but have no way to know which value of each will appear next to each other. Meaning one time you may get 4,10-5,8-6,7 and another get 4,6-5,14-6,7 on the same row.

– Sean Lange
Sep 18 '18 at 15:04




1 Answer
1



I'm inferring team association based on the data provided. Something like the below could work for you (demo: http://rextester.com/GQHPV34978)


CREATE TABLE temp
(
[teamID] INT,
[month] INT,
[count_of_stores] INT
)

INSERT INTO temp
VALUES
(1,4,10),
(2,4,4),
(3,4,6),
(1,5,8),
(2,5,14),
(3,5,9),
(1,6,7),
(2,6,3),
(3,6,1)

SELECT [teamID],
MAX(CASE WHEN MONTH = 4 THEN MONTH END )AS Month,
MAX(CASE WHEN MONTH = 4 THEN count_of_stores END ) AS Count_of_Stores,
MAX(CASE WHEN MONTH = 5 THEN MONTH END )AS Month,
MAX(CASE WHEN MONTH = 5 THEN count_of_stores END ) AS Count_of_Stores ,
MAX(CASE WHEN MONTH = 6 THEN MONTH END )AS Month,
MAX(CASE WHEN MONTH = 6 THEN count_of_stores END ) AS Count_of_Stores
FROM temp
GROUP BY teamID



Updating with the following based on new information (demo:http://rextester.com/JIZQX61960)


create TABLE #temp
(
[teamID] varchar,
[month] INT,
[count_of_stores] INT
)

INSERT INTO #temp
VALUES
('a',4,10),
('b',4,4),
('c',4,6),
('a',5,8),
('b',5,14),
('e',5,9),
('a',6,7),
('b',6,3),
('f',6,1);


WITH monthGrouping AS
(
SELECT row_number() over (partition by month order by month) as rn, [teamID], [month],[count_of_stores] FROM #temp
)

SELECT
MAX(CASE WHEN MONTH = 4 THEN [teamID] END )AS [teamID],
MAX(CASE WHEN MONTH = 4 THEN MONTH END )AS Month,
MAX(CASE WHEN MONTH = 4 THEN count_of_stores END ) AS Count_of_Stores,
MAX(CASE WHEN MONTH = 5 THEN [teamID] END )AS [teamID],
MAX(CASE WHEN MONTH = 5 THEN MONTH END )AS Month,
MAX(CASE WHEN MONTH = 5 THEN count_of_stores END ) AS Count_of_Stores ,
MAX(CASE WHEN MONTH = 6 THEN [teamID] END )AS [teamID],
MAX(CASE WHEN MONTH = 6 THEN MONTH END )AS Month,
MAX(CASE WHEN MONTH = 6 THEN count_of_stores END ) AS Count_of_Stores
FROM monthGrouping
GROUP BY rn






Thanks! This looks good. this works for me. But What if i have different teams names or Id's. Say i have nine different team names or ID's for all the nine entries. In that case, is it possible to still split them and arrange? When i implement this logic, think i will be left with lot of NULLS values if i am not wrong. IS that right? what do you think?

– Tamil
Sep 18 '18 at 15:05







Yes, you can be left with nulls if you are using different names or IDs. If you have different teams spanning across the months you could row_number() them by month and use the same type of data rotation by grouping off of that... similar to link. In order to give the best implementation advice, we really need to see an example of what the final results would be expected to look like.

– GreyOrGray
Sep 18 '18 at 15:34






Team Month Count_of_Stores a 4 10 b 4 4 c 4 6 a 5 8 b 5 14 e 5 9 a 6 7 b 6 3 f 6 1

– Tamil
Sep 18 '18 at 15:41






And i would like to get theo/p like this Team Month Count_of_Stores Team Month Count_of_Stores Team Month Count_of_Stores a 4 10 a 5 8 a 6 7 b 4 4 b 5 14 b 6 3 c 4 6 e 5 9 f 6 1

– Tamil
Sep 18 '18 at 15:42







That's awesome! Worked like magic. Thanks a lot. And apologies for the confusion and incomplete data in the beginning.

– Tamil
Sep 18 '18 at 16:22



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế