Vertical results from multiple columns

Copper Contributor

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.

 

Binarak_0-1661473399265.png

 

Results

 

Binarak_2-1661473104734.png

What I am trying to get an output of something like this...

Study IDB10Value
   
Study1B10-1123
Study1B10-2234
Study1B10-3345
Study2B25-1147

 

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!

5 Replies

@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

@olafhelper 

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 Ronen_Ariely_0-1652869736515.png

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.

@Ronen_Ariely 

I apologize. I am very new to in depth SQL. I am a virtualization and Azure admin. Just trying to help with something I don't know. I know the difference between an image and text.

I was just not sure how to post it in a presentable way, because when I copy and pasted, it looked like a disaster. I was probably over concerned about the look of my post. Is there a specific way to format the query or I just cope as plain text?

Hi @Binarak 

 

As you probably noticed, I am not coming here every day so I might respond late. I am a lot more active in the Microsoft QnA forums where I also serve as Moderator like many others from the community :smile:

 

> I was just not sure how to post it in a presentable way, because when I copy and pasted, it looked like a disaster.

 

As I wrote, you should provide queries and not spend time on formatting text in a way it presented in a client side.

 

If you provide for example the queries:

CREATE TABLE RonenTbl (id int, txt NVARCHAR(100))
GO
INSERT RonenTbl (id, txt) values (1,'yes'), (2,'you'), (3,'can')
GO

 Then no need for any work on presenting the content of the table since we can create the table in our server and insert the sample data

 

This is what you need to provide! queries and not images :smile:

 

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.