Solved

How convert rows to columns using SQL?

Posted on 2014-09-23
4
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 48

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 48

Accepted Solution

by:
PortletPaul earned 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Job - date manual 1 47
SQL DATEADD 10 87
Parse this column 6 38
Http Post send by Store Procedure Help 5 20
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.

740 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