Discussion:
Columns to Rows in SQL
(too old to reply)
Manoj Kumar
2013-08-10 14:07:20 UTC
Permalink
Hi Everyone,

Is it possible to convert columns into rows.

Converting this
Date Computer Fridge
21-Jun-13 2 3
20-Jun-13 3 4
19-Jun-13 7 9

into

Items 21-Jun-13 20-Jun-13 19-Jun-13
Computer 2 3 7
Fridge 3 4 9

Please help someone.

Regards,
Manoj
Bob Barrows
2013-08-10 15:39:50 UTC
Permalink
Post by Manoj Kumar
Hi Everyone,
Is it possible to convert columns into rows.
Converting this
Date Computer Fridge
21-Jun-13 2 3
20-Jun-13 3 4
19-Jun-13 7 9
into
Items 21-Jun-13 20-Jun-13 19-Jun-13
Computer 2 3 7
Fridge 3 4 9
Very frequently-asked question. Of course it's possible. The method used
depends on the version of SQL Server you are using. So, start by googling
"pivot" or "crosstab" and "t-sql" - if you add the version of sql you are
using to the search, you will be more likely to find an answer that works
for you.
Manoj Kumar
2013-08-11 13:02:49 UTC
Permalink
Post by Bob Barrows
Post by Manoj Kumar
Hi Everyone,
Is it possible to convert columns into rows.
Converting this
Date Computer Fridge
21-Jun-13 2 3
20-Jun-13 3 4
19-Jun-13 7 9
into
Items 21-Jun-13 20-Jun-13 19-Jun-13
Computer 2 3 7
Fridge 3 4 9
Very frequently-asked question. Of course it's possible. The method used
depends on the version of SQL Server you are using. So, start by googling
"pivot" or "crosstab" and "t-sql" - if you add the version of sql you are
using to the search, you will be more likely to find an answer that works
for you.
I'm using SQL 2012 Express
Erland Sommarskog
2013-08-10 20:01:27 UTC
Permalink
Post by Manoj Kumar
Is it possible to convert columns into rows.
Converting this
Date Computer Fridge
21-Jun-13 2 3
20-Jun-13 3 4
19-Jun-13 7 9
into
Items 21-Jun-13 20-Jun-13 19-Jun-13
Computer 2 3 7
Fridge 3 4 9
SELECT MAX(CASE WHEN tbl.Date = '20130621'
THEN CASE WHEN n.n = 1 THEN Computer
WHEN n.n = 2 THEN Fridge
END
END) AS [21-Jun-13],
MAX(CASE WHEN tbl.Date = '20130620'
THEN CASE WHEN n.n = 1 THEN Computer
WHEN n.n = 2 THEN Fridge
END
END) AS [20-Jun-13],
MAX(CASE WHEN tbl.Date = '20130619'
THEN CASE WHEN n.n = 1 THEN Computer
WHEN n.n = 2 THEN Fridge
END
END) AS [19-Jun-13]
FROM tbl
CROSS JOIN (SELECT n = 1 UNION ALL SELECT 2) AS n


This is the pattern of a combined pivot and unpivot (without using
the PIVOT and UNPIVOT keywords in SQL Server that I recommend staying
away from.)

As you may note the code is repetitive and the column names are fixed.
This is because a SELECT statement returns a table, and a table is
supposed to mode a unique entity with distinct attributes. Thus dynamic
naming does not fit into this pattern.

If you always want three days, you can insert the data into a temp
table, and then use sp_rename to rename the columns. If you have a variable
number of days, you will need to first read the available dates and
then compose dynamic SQL. There is all reason to investigate if you
can do this more easily client-side.
--
Erland Sommarskog, Stockholm, ***@sommarskog.se
Manoj Kumar
2013-08-11 13:02:21 UTC
Permalink
Post by Erland Sommarskog
Post by Manoj Kumar
Is it possible to convert columns into rows.
Converting this
Date Computer Fridge
21-Jun-13 2 3
20-Jun-13 3 4
19-Jun-13 7 9
into
Items 21-Jun-13 20-Jun-13 19-Jun-13
Computer 2 3 7
Fridge 3 4 9
SELECT MAX(CASE WHEN tbl.Date = '20130621'
THEN CASE WHEN n.n = 1 THEN Computer
WHEN n.n = 2 THEN Fridge
END
END) AS [21-Jun-13],
MAX(CASE WHEN tbl.Date = '20130620'
THEN CASE WHEN n.n = 1 THEN Computer
WHEN n.n = 2 THEN Fridge
END
END) AS [20-Jun-13],
MAX(CASE WHEN tbl.Date = '20130619'
THEN CASE WHEN n.n = 1 THEN Computer
WHEN n.n = 2 THEN Fridge
END
END) AS [19-Jun-13]
FROM tbl
CROSS JOIN (SELECT n = 1 UNION ALL SELECT 2) AS n
This is the pattern of a combined pivot and unpivot (without using
the PIVOT and UNPIVOT keywords in SQL Server that I recommend staying
away from.)
As you may note the code is repetitive and the column names are fixed.
This is because a SELECT statement returns a table, and a table is
supposed to mode a unique entity with distinct attributes. Thus dynamic
naming does not fit into this pattern.
If you always want three days, you can insert the data into a temp
table, and then use sp_rename to rename the columns. If you have a variable
number of days, you will need to first read the available dates and
then compose dynamic SQL. There is all reason to investigate if you
can do this more easily client-side.
--
Thanks.
That should solve my problem.

The Dates are added daily but the items is always fixed. I want to take only last 10 days records and show table in the above format.
Erland Sommarskog
2013-08-11 14:08:29 UTC
Permalink
Post by Manoj Kumar
The Dates are added daily but the items is always fixed. I want to take
only last 10 days records and show table in the above format.
Good. Then you can use my solution, but using column names like [1], [2]
etc, inserting the data into a temp table and then rename the columns.

See also http://www.sommarskog.se/dynamic_sql.html#columnalias
--
Erland Sommarskog, Stockholm, ***@sommarskog.se
Manoj Kumar
2013-08-11 14:14:30 UTC
Permalink
Post by Erland Sommarskog
Post by Manoj Kumar
The Dates are added daily but the items is always fixed. I want to take
only last 10 days records and show table in the above format.
Good. Then you can use my solution, but using column names like [1], [2]
etc, inserting the data into a temp table and then rename the columns.
See also http://www.sommarskog.se/dynamic_sql.html#columnalias
--
Once again thanks.

Continue reading on narkive:
Loading...