Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How convert rows to columns using SQL?

Posted on 2014-09-23
4
Medium Priority
?
168 Views
Last Modified: 2014-09-30
I want to convert rows from the result to column.
Attached Input file is the sample data, I want to the sql server query to return like the result in Result file.
Input.xlsx
Result.xlsx
0
Comment
Question by:Raj G
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40340439
Hi,

you can use this:

select col2, sum(Test1) as Test1, sum(Test2) as Test2, sum(Test3) as Test3
from(
select col2 , 
case when col1='TEST1' then 1 else 0 end as Test1, 
case when col1='TEST2' then 1 else 0 end as Test2, 
case when col1='TEST3' then 1 else 0 end as Test3
from table_name
group by col2 
)a group by col2

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40340499
input/resultThis query will match the requested result:
SELECT
        param
      , max(case when col1 = 'TEST1' then col3 end) AS test1
      , max(case when col1 = 'TEST2' then col3 end) AS test2
      , max(case when col1 = 'TEST3' then col3 end) AS test3
FROM Input
CROSS JOIN (SELECT 'COL2' AS param UNION ALL SELECT 'COL3' AS param) cj
GROUP BY
        param
;

Open in new window

But what happens to 'english' as a value from the input table?

{+ edit, details}
    CREATE TABLE Input
    	([COL1] varchar(5), [COL2] varchar(7), [COL3] int)
    ;
    	
    INSERT INTO Input
    	([COL1], [COL2], [COL3])
    VALUES
    	('TEST1', 'english', 3),
    	('TEST2', 'english', 3),
    	('TEST3', 'english', 3)
    ;

**Query 1**:

    SELECT
            param
          , max(case when col1 = 'TEST1' then col3 end) AS test1
          , max(case when col1 = 'TEST2' then col3 end) AS test2
          , max(case when col1 = 'TEST3' then col3 end) AS test3
    FROM Input
    CROSS JOIN (SELECT 'COL2' AS param UNION ALL SELECT 'COL3' AS param) cj
    GROUP BY
            param
    

**[Results][2]**:
    
    | PARAM | TEST1 | TEST2 | TEST3 |
    |-------|-------|-------|-------|
    |  COL2 |     3 |     3 |     3 |
    |  COL3 |     3 |     3 |     3 |



  [1]: http://sqlfiddle.com/#!3/b84060/5

Open in new window

0
 

Author Comment

by:Raj G
ID: 40341651
Is there a way I can have 'english' value also in the result set?
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 40343229
You need to ensure that each column is a data type that can deal with the source data, so here I have converted col3 to varchar; but this is only because it appears col3 is an integer, if it already a (n)varchar then the conversion would not be needed.
SELECT
        param
      , max(case when col1 = 'TEST1' and param = 'COL2' then col2 
                 when col1 = 'TEST1' and param = 'COL3' then cast(col3 as varchar) end) AS test1
      , max(case when col1 = 'TEST2' and param = 'COL2' then col2 
                 when col1 = 'TEST2' and param = 'COL3' then cast(col3 as varchar) end) AS test2
      , max(case when col1 = 'TEST3' and param = 'COL2' then col2 
                 when col1 = 'TEST3' and param = 'COL3' then cast(col3 as varchar) end) AS test3
FROM Input
CROSS JOIN (SELECT 'COL2' AS param UNION ALL SELECT 'COL3' AS param) cj
GROUP BY
        param
;

Open in new window

| PARAM |   TEST1 |   TEST2 |   TEST3 |
|-------|---------|---------|---------|
|  COL2 | english | english | english |
|  COL3 |       3 |       3 |       3 |

http://sqlfiddle.com/#!3/b84060/10

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question