Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to get Rows to Columns (SQL Server)?

Asked by Ankur Vaish Jul 12, 2021 1.3K views 1 answer
Share

About this question

I have a table that has two columns, one with the type of posting and another column with the value. Source table: +-----+-------+ |Type | Value | +-----+-------+ | C | 381.22| | D | 25.49 | | C | 25.49 | | D | 25.48 | | C | 705.56| | D | 80.00 | +-----+-------+ I need to create a view that separates types C and D, and list their values. I need it to look like this: +------+-----+ |Credit|Debit| +------+-----+ |381.22|25.49| |25.49 |25.48| |705.56|80.00| +------+-----+ I tried to create two separate virtual columns with SELECTS, just to list the values for the respective types. SELECT (SELECT [value] FROM [table] WHERE [type] IN ('D')) AS Debit, (SELECT [value] FROM [table] WHERE [type] IN ('C')) AS Credit And the error occurs because both SELECTS return more than one result: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Also try to use the PIVOT statement, but still unsuccessful. Could you give me a suggestion to solve this problem?

How do I get SQL server rows to columns?

Your answer

1 Answer

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.