Discussion:
Converting columns to rows **** NEED HELP ****
(too old to reply)
Sydney
2009-10-08 22:12:22 UTC
Permalink
Hello,

I would like to know if it's possible to compose a TSQL statement that
will convert columns of information into rows of information. For
example:

I have the following table of info:

ProductID Descr Qty
--------------------------------------------
001 Mouse 10
002 Cable 20

and want the result to look like this:

ProductID Item
-----------------------------------
001 Mouse
001 10
002 Cable
002 20
-----------------------------------

I know it's strange to have this type of output but I have a need to
do something like this.
in TSQL. Please show how this is accomplished.

Any help I can get from this group is greatly appreciated.


-Sydney
Erland Sommarskog
2009-10-08 22:15:53 UTC
Permalink
Post by Sydney
I would like to know if it's possible to compose a TSQL statement that
will convert columns of information into rows of information. For
ProductID Descr Qty
--------------------------------------------
001 Mouse 10
002 Cable 20
ProductID Item
-----------------------------------
001 Mouse
001 10
002 Cable
002 20
-----------------------------------
I know it's strange to have this type of output but I have a need to
do something like this.
in TSQL. Please show how this is accomplished.
If you are on SQL 2005, look up UNPIVOT in Books Online. And, no, I'm
not going to show you an example; I'm just about to sign off for tonight.
--
Erland Sommarskog, SQL Server MVP, ***@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Plamen Ratchev
2009-10-08 22:24:09 UTC
Permalink
This should do it (make sure to cast the 'qty' column to the same data type/size as 'descr'):

SELECT productid, value
FROM (SELECT productid, descr, CAST(qty AS VARCHAR(20)) AS qty
FROM Info) AS I
UNPIVOT
(value FOR col IN (descr, qty)) AS U;
--
Plamen Ratchev
http://www.SQLStudio.com
Sydney
2009-10-09 02:03:56 UTC
Permalink
Post by Plamen Ratchev
SELECT productid, value
FROM (SELECT productid, descr, CAST(qty AS VARCHAR(20)) AS qty
       FROM Info) AS I
UNPIVOT
(value FOR col IN (descr, qty)) AS U;
--
Plamen Ratchevhttp://www.SQLStudio.com
Plamen,

How would you do it in SQL2K? Sorry I forgot to mention in my
original post.


Celko,

Yes, I did realize the "Item" column in my sample contains two
different data types. It was only an example
not a real world data.

Thanks



-Sydney
Plamen Ratchev
2009-10-09 02:21:02 UTC
Permalink
Here is SQL Server 2000 version:

SELECT productid,
CASE n WHEN 1 THEN descr
WHEN 2 THEN qty
END AS value
FROM (SELECT productid, descr, CAST(qty AS VARCHAR(20)) AS qty
FROM Info) AS I
CROSS JOIN (SELECT 1 UNION SELECT 2) AS Nums(n);
--
Plamen Ratchev
http://www.SQLStudio.com
Sydney
2009-10-12 17:49:17 UTC
Permalink
Post by Plamen Ratchev
SELECT productid,
        CASE n WHEN 1 THEN descr
               WHEN 2 THEN qty
        END AS value
FROM (SELECT productid, descr, CAST(qty AS VARCHAR(20)) AS qty
       FROM Info) AS I
CROSS JOIN (SELECT 1 UNION SELECT 2) AS Nums(n);
--
Plamen Ratchevhttp://www.SQLStudio.com
Plamen,

Thank you for your sample!

I have modified your code to work with what I needed to do in a real
database table. It's working
beautifully. I tried to study your TSQL and it looks like trick is
the CROSS JOIN and the UNION.
I do some have experience in TSQL but not at a level I want to be in
yet. Can you explain how the
"n" (as in CASE n ....) and how Nums(n) work? I am going to do some
research as well.

Thanks.

Sydney
Plamen Ratchev
2009-10-12 18:36:44 UTC
Permalink
You use a cross join with table with two rows (in this case it was a simple derived table with two numbers in each row)
to produce twice the number of rows in the original table (since the cross join will generate the combination of all
possible row matches). Then the CASE expression is used to pick the correct value corresponding to the row you want. In
this case for the first row it picks descriptions and for the second quantity.
--
Plamen Ratchev
http://www.SQLStudio.com
--CELKO--
2009-10-09 00:56:36 UTC
Permalink
I would like to know if it's possible to compose a TSQL statement that will convert columns of information into rows of information.  <<
This is best done with a report writer.
ProductID          Item
-----------------------------------
001                   Mouse
001                   10
002                   Cable
002                   20
-----------------------------------
<<

Did you notice that the vague "item_<nothing in particular>" has data
of two different types and meanings??

If you don't know anything about RDBMS, then get a copy of the
simplest intro book I know --
http://www.amazon.com/Manga-Guide-Databases-Mana-Takahashi/dp/1593271905

All you are getting here is DANGEROUS kludges.
Tony Rogerson
2009-10-09 06:24:11 UTC
Permalink
This is down to your lack of real industrial experience.

Its akin to the group of people who persisted that the Earth was flat - they
swore blind it was flat (aka you and your kludge and normal form rants);
unfortunate for them, there where people who had open minds and actually
found the Earth was round - it was those folk that made it to the new lands
(America).

You owe your life to open minded people!

Just in the same way posters on here are thankful there are open minded
people on here that give them workable solutions to their real business
problems.

--ROGGIE--
Post by --CELKO--
Post by Sydney
I would like to know if it's possible to compose a TSQL statement that
will convert columns of information into rows of information. <<
This is best done with a report writer.
ProductID Item
-----------------------------------
001 Mouse
001 10
002 Cable
002 20
-----------------------------------
<<
Did you notice that the vague "item_<nothing in particular>" has data
of two different types and meanings??
If you don't know anything about RDBMS, then get a copy of the
simplest intro book I know --
http://www.amazon.com/Manga-Guide-Databases-Mana-Takahashi/dp/1593271905
All you are getting here is DANGEROUS kludges.
Michael Cole
2009-10-09 10:17:55 UTC
Permalink
Post by Tony Rogerson
This is down to your lack of real industrial experience.
Its akin to the group of people who persisted that the Earth was flat
- they swore blind it was flat (aka you and your kludge and normal
form rants); unfortunate for them, there where people who had open
minds and actually found the Earth was round - it was those folk that
made it to the new lands (America).
Ah, minor point here. AFAIAA, no Greek of around 300BCE ever made it to
America.

(In other words, the idea that Columbus proved the Earth was round is a
myth - it was known that it was a spheroid over 2000 years before his
journey. Look up Aristotle and Erastosthenes.)
Post by Tony Rogerson
You owe your life to open minded people!
And hopefully such open-minded people won't perpetuate myths...
--
Regards

Michael Cole
Tony Rogerson
2009-10-09 11:33:47 UTC
Permalink
Thanks Michael, but I'm sure people will get my meaning entirely.

Tony.
Post by Michael Cole
Post by Tony Rogerson
This is down to your lack of real industrial experience.
Its akin to the group of people who persisted that the Earth was flat
- they swore blind it was flat (aka you and your kludge and normal
form rants); unfortunate for them, there where people who had open
minds and actually found the Earth was round - it was those folk that
made it to the new lands (America).
Ah, minor point here. AFAIAA, no Greek of around 300BCE ever made it to
America.
(In other words, the idea that Columbus proved the Earth was round is a
myth - it was known that it was a spheroid over 2000 years before his
journey. Look up Aristotle and Erastosthenes.)
Post by Tony Rogerson
You owe your life to open minded people!
And hopefully such open-minded people won't perpetuate myths...
--
Regards
Michael Cole
HASAN AYDIN
2012-05-17 07:11:07 UTC
Permalink
--we can write generic solution for any query.

--sample table

SELECT * INTO #comp
FROM ( SELECT '001' ProductID, 'Mouse' Descr, 10 Qty
UNION all
SELECT '002' ProductID, 'Cable' Descr, 20 Qty
) x

--selected record to #tmp
SELECT * INTO #tmp FROM #comp -- WHERE car_id=1

--generic solution
DECLARE @sql VARCHAR(max)
SET @sql=''
SELECT @sql=@sql + 'SELECT ProductID,'''+Name+''' as ColumnName,cast(['+Name+'] as varchar(500)) as Value FROM #tmp union all '
FROM tempdb.sys.columns s
where object_id=object_id('tempdb..#tmp') AND s.NAME IN ('Descr','Qty')

SET @sql = LEFT(@sql,LEN(@sql)-9)

SET @sql = 'select distinct ProductID,Value from (' + @sql + ') x'

EXEC( @sql)

DROP TABLE #tmp,#comp
Post by Plamen Ratchev
SELECT productid, value
FROM (SELECT productid, descr, CAST(qty AS VARCHAR(20)) AS qty
FROM Info) AS I
UNPIVOT
(value FOR col IN (descr, qty)) AS U;
--
Plamen Ratchev
http://www.SQLStudio.com
Post by Plamen Ratchev
SELECT productid,
CASE n WHEN 1 THEN descr
WHEN 2 THEN qty
END AS value
FROM (SELECT productid, descr, CAST(qty AS VARCHAR(20)) AS qty
FROM Info) AS I
CROSS JOIN (SELECT 1 UNION SELECT 2) AS Nums(n);
--
Plamen Ratchev
http://www.SQLStudio.com
Post by Tony Rogerson
This is down to your lack of real industrial experience.
Its akin to the group of people who persisted that the Earth was flat - they
swore blind it was flat (aka you and your kludge and normal form rants);
unfortunate for them, there where people who had open minds and actually
found the Earth was round - it was those folk that made it to the new lands
(America).
You owe your life to open minded people!
Just in the same way posters on here are thankful there are open minded
people on here that give them workable solutions to their real business
problems.
--ROGGIE--
Post by Michael Cole
Ah, minor point here. AFAIAA, no Greek of around 300BCE ever made it to
America.
(In other words, the idea that Columbus proved the Earth was round is a
myth - it was known that it was a spheroid over 2000 years before his
journey. Look up Aristotle and Erastosthenes.)
And hopefully such open-minded people will not perpetuate myths...
--
Regards
Michael Cole
Thanks Michael, but I am sure people will get my meaning entirely.
Tony.
Post by Sydney
Hello,
I would like to know if it is possible to compose a TSQL statement that
will convert columns of information into rows of information. For
ProductID Descr Qty
--------------------------------------------
001 Mouse 10
002 Cable 20
ProductID Item
-----------------------------------
001 Mouse
001 10
002 Cable
002 20
-----------------------------------
I know it is strange to have this type of output but I have a need to
do something like this.
in TSQL. Please show how this is accomplished.
Any help I can get from this group is greatly appreciated.
-Sydney
If you are on SQL 2005, look up UNPIVOT in Books Online. And, no, I am
not going to show you an example; I am just about to sign off for tonight.
--
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
ill convert columns of information into rows of information. =A0<<
This is best done with a report writer.
ProductID =A0 =A0 =A0 =A0 =A0Item
-----------------------------------
001 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Mouse
001 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 10
002 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Cable
002 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 20
-----------------------------------
<<
Did you notice that the vague "item_<nothing in particular>" has data
of two different types and meanings??
If you do not know anything about RDBMS, then get a copy of the
simplest intro book I know --
http://www.amazon.com/Manga-Guide-Databases-Mana-Takahashi/dp/1593271905
All you are getting here is DANGEROUS kludges.
Post by Sydney
Plamen,
How would you do it in SQL2K? Sorry I forgot to mention in my
original post.
Celko,
Yes, I did realize the "Item" column in my sample contains two
different data types. It was only an example
not a real world data.
Thanks
-Sydney
Post by Plamen Ratchev
You use a cross join with table with two rows (in this case it was a simple derived table with two numbers in each row)
to produce twice the number of rows in the original table (since the cross join will generate the combination of all
possible row matches). Then the CASE expression is used to pick the correct value corresponding to the row you want. In
this case for the first row it picks descriptions and for the second quantity.
--
Plamen Ratchev
http://www.SQLStudio.com
Post by Sydney
Plamen,
Thank you for your sample!
I have modified your code to work with what I needed to do in a real
database table. it is working
beautifully. I tried to study your TSQL and it looks like trick is
the CROSS JOIN and the UNION.
I do some have experience in TSQL but not at a level I want to be in
yet. Can you explain how the
"n" (as in CASE n ....) and how Nums(n) work? I am going to do some
research as well.
Thanks.
Sydney
rpresser
2012-05-18 16:22:47 UTC
Permalink
Post by HASAN AYDIN
--we can write generic solution for any query.
Now that, if you like, is a dangerous kludge. My compliments.

Continue reading on narkive:
Loading...