Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi AllFirst time poster long time lurker.QuestionI have a table structure that I am outputting from SQL in this formatPRODUCT Siz1 Siz2 Siz3Test 1 2 3And I want to output it asPRODUCT QTYTEST Siz1 1TEST Siz2 2TEST Siz3 3Any ideas as to how I can go about that?Thanks in advance.Craig
Kristen
Test
22859 Posts
Posted - 2005-08-31 : 05:14:11
Hi craiglkb, Welcome to SQL Team Posters!Just one row to "transpose"?Or do you want to SUM/Total up all the rows - i.e. make a "crosstab"?Kristen
craiglkb
Starting Member
2 Posts
Posted - 2005-08-31 : 06:05:10
Hi Kristen, thanks for the WelcomeThe table I want to transpose is about 2000 seperate lines - this number will vary over time too.ThanksCraig
Kristen
Test
22859 Posts
Posted - 2005-08-31 : 06:35:45
Something like this maybe?
SELECT PRODUCT, 'Siz1', Siz1 AS [Qty]FROM MyTableUNION ALLSELECT PRODUCT, 'Siz2', Siz2 AS [Qty]FROM MyTableSELECT PRODUCT, 'Siz3', Siz3 AS [Qty]FROM MyTable
Kristen
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2005-08-31 : 06:58:33
I think Using Union will display the records in proper orderSuppose if the table has following recordsPRODUCT Siz1 Siz2 Siz3Test 1 2 3Testing 4 5 6Union All will display Test s1 1Testing s1 4Test s2 2Testing s2 5Test s3 3Testing s3 6whereas Union will displayTest s1 1Test s2 2Test s3 3Testing s1 4Testing s2 5Testing s3 6Am I correct?MadhivananFailing to plan is Planning to fail
Kristen
Test
22859 Posts
Posted - 2005-08-31 : 08:36:34
Yup, I reckon you're right.UNION will remove duplicates - I assumed that wasn't desireable! (and if there are NO duplicates possible then UNION ALL is better because it avoids the Sort which SQL Server will do to enable it to try to find the non-existent Dups!)Kristen
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2005-08-31 : 08:41:53
Thanks KristenIf Display order is not a problem then yours is bestMadhivananFailing to plan is Planning to fail
Kristen
Test
22859 Posts
Posted - 2005-08-31 : 08:47:34
"Display order"For any repeatable display order it needs an ORDER BY clause, of courseKristen