Forum Discussion
Vertical results from multiple columns
Hello all,
I am very new to SQL and I don't have so much experience. I am trying to help with a report and I am trying to gather results from 3 columns and stack it vertically. This is the query I have. I simplified it a bit for demo.
Results
What I am trying to get an output of something like this...
Study ID | B10 | Value |
Study1 | B10-1 | 123 |
Study1 | B10-2 | 234 |
Study1 | B10-3 | 345 |
Study2 | B25-1 | 147 |
If you guys can give me a hand I'd really appreciate it. I've been thinking of "Union", "Pivot" but I am not sure to how to apply for this scenario.
Please help!
Thank you all!
- olafhelperBronze Contributor
Binarak , I can't see anything in that screenshot in micro-format, but I guess you are looking for the UNPIVOT command, see
Using PIVOT and UNPIVOT - SQL Server | Microsoft Docs
For a more detailed answer please post table design as DDL, some sample data as DML statement and the expected result.
Olaf
- BinarakCopper Contributor
Hello good sir. Thank you so much for taking time to respond. I thought that the screenshot might be too small. I wasn't sure how to give a clear view. Ill give you a better picture.
Thank you so much.
Hi Binarak
First, welcome to the family of SQL Server users
Even if the image is big and we can read the content, it is still an image and not queries - text which we can copy->test->fix...
What do you think will be simpler and faster. Option one: each one of the 100 people that come to the forum to help others instead of helping 10 people, will spend all his time to manually check the image -> type the query. Or maybe option two: one person who asked the question and has all the information will simply provide the query instead images?!?
Please provide:
1) Queries to CREATE your table(s)
2) Queries to INSERT sample data.
3) The desired result given the sample.
4) A short description of the business rules.
5) Which version of SQL Server you are using.