![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
I would like to know if it's possible to compose a TSQL statement that will convert columns of information into rows of information. * |
|
and want the result to look like this: ProductID * * * * *Item |
#5
| |||
| |||
|
|
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 Ratchevhttp://www.SQLStudio.com |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
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. and want the result to look like this: 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-Da.../dp/1593271905 All you are getting here is DANGEROUS kludges. |
#8
| |||
| |||
|
|
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! |
#9
| |||
| |||
|
|
Tony Rogerson <tonyrogerson (AT) torver (DOT) net> wrote in message news:uznsekKSKHA.4704 (AT) TK2MSFTNGP02 (DOT) phx.gbl 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.) You owe your life to open minded people! And hopefully such open-minded people won't perpetuate myths... -- Regards Michael Cole |
#10
| |||
| |||
|
|
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 Ratchevhttp://www.SQLStudio.com |
![]() |
| Thread Tools | |
| Display Modes | |
| |