Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Transpose cloumns into rows and rows into columns


Transpose cloumns into rows and rows into columns

Author
Message
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2015 Visits: 1189
my result set is like this.But i want to pivot the result.

I used both the cobination of pivot and unpivot but i got the result but is not worked for me.

state October November December January Febuary
state1 721 1631 1035 821 752
state2 1321 2015 1997 101 515
state3 1309 1360 908 736 665
state4 272 1716 1847 1804 897
state5 130 366 394 452 240
state6 231 2992 2468 2553 1693
state7 612 1637 1419 941 339
state8 1023 941 1772 1641 902

for the above result set Actually i want the result set like this where there is transpose function in sql server.

state state1 state2 state3 state4 state5 state6 state7 state8
October 721 1321 1309 272 130 231 612 1023
November 1631 2015 1360 1716 366 2992 1637 941
December 1035 1997 908 1847 394 2468 1419 1772
January 821 1013 736 1804 452 2553 941 1641
Febuary 752 515 665 897 240 1693 339 902

Malleswarareddy
I.T.Analyst
MCITP(70-451)
WayneS
WayneS
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6251 Visits: 10403
You might want to read these articles:
http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7007 Visits: 13559
I'm not aware of any transpose function in SQL Server.
So you'd need to UNPIVOT your data to get three columns (state, month and value). Then use one of the links Wayne pointed you at to get the data pivoted again in a transposed for.



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2015 Visits: 1189
Hi i got my result .but i did not got my result directly with combination of pivot and unpivot result.first i unpivot the result into temp
then pivot the temp table.

finally got succeeded.

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10350 Visits: 11350

USE tempdb;

DECLARE @Data
TABLE (
state CHAR(6) NOT NULL,
october INT NOT NULL,
november INT NOT NULL,
december INT NOT NULL,
january INT NOT NULL,
february INT NOT NULL
);

INSERT @Data
VALUES ('state1', 0721, 1631, 1035, 0821, 0752),
('state2', 1321, 2015, 1997, 0101, 0515),
('state3', 1309, 1360, 0908, 0736, 0665),
('state4', 0272, 1716, 1847, 1804, 0897),
('state5', 0130, 0366, 0394, 0452, 0240),
('state6', 0231, 2992, 2468, 2553, 1693),
('state7', 0612, 1637, 1419, 0941, 0339),
('state8', 1023, 0941, 1772, 1641, 0902);

SELECT P.month AS state,
P.state1,
P.state2,
P.state3,
P.state4,
P.state5,
P.state6,
P.state7,
P.state8
FROM @Data D
UNPIVOT (value FOR month IN (october, november, december, january, february)) U
PIVOT (MAX(U.value) FOR U.state IN (state1, state2, state3, state4, state5, state6, state7, state8)) P
ORDER BY
CASE P.month
WHEN 'october' THEN 1
WHEN 'november' THEN 2
WHEN 'december' THEN 3
WHEN 'january' THEN 4
WHEN 'february' THEN 5
ELSE NULL
END;



For a general Transpose function (implemented as a CLR procedure) see http://www.sqlmag.com/articles/index.cfm?articleid=102631. A subscription is required.

Paul



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2015 Visits: 1189
Hi pual ,

i got suceeded with pivot and unpivot.But i First transpose this to unpivot then pivot the table.I take two steps for this.

But you have done this in single steps.thanks for reply.

your code is pretty ,simple and excellent.

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10350 Visits: 11350
Thanks very much!



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Stun-Dexim
Stun-Dexim
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 53
http://sqlmag.com/t-sql/how-sql-server-clr-and-c-can-solve-common-problem
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search