October 11, 2017 at 2:43 am
Hello,
Im trying to combine multiple lines to one like the below example.
I looked at piviot but as im not doing any calculations i didnt think that would be the most relevant. I also looked at group_concat but i can only find solutions for multiple columns in MySQL and not SQL.
The idea is to group multiple orders into one line and the max orders anyone can place is 4.
Does anyone have any suggestions ?
Thank you 🙂
October 11, 2017 at 3:11 am
What about if a name has Order A, B And C. Are you expecting 3 columns, or are the only options A and B? Could it go all the way of to Z? Maybe more (ZZZ?)?
Generally this sort of query is frowned upon at SQL level. If you need to pivot data, you should do it in your presentation layer.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
October 11, 2017 at 7:44 am
Add a ROW_NUMBER() function that partitions by Name and Address, and then ORDER BY [Order], and then you can use CASE statements for a sort of CROSS TAB query that uses GROUP BY Name, Address and does a MAX(CASE WHEN RowNum = 1 THEN [Order] ELSE NULL END) AS Order1, and similar lines for the other 3 possible orders. I assume here that you use a CTE to derive the row numbers and call that column RowNum.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
October 11, 2017 at 8:57 am
nicolestrain07 - Wednesday, October 11, 2017 2:43 AMHello,Im trying to combine multiple lines to one like the below example.
I looked at piviot but as im not doing any calculations i didnt think that would be the most relevant. I also looked at group_concat but i can only find solutions for multiple columns in MySQL and not SQL.The idea is to group multiple orders into one line and the max orders anyone can place is 4.
Does anyone have any suggestions ?
Thank you 🙂
Please see the following article. Use MAX instead of SUM.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply