Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Transpose cloumns into rows and rows into columns Expand / Collapse
Author
Message
Posted Monday, February 15, 2010 8:19 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:43 AM
Points: 1,938, Visits: 1,162
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)
Post #865556
Posted Monday, February 15, 2010 11:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 8:11 PM
Points: 6,604, Visits: 8,910
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
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
Post #865653
Posted Monday, February 15, 2010 12:19 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Sunday, September 7, 2014 11:27 PM
Points: 7,164, Visits: 13,257
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
Post #865666
Posted Tuesday, February 16, 2010 1:00 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:43 AM
Points: 1,938, Visits: 1,162
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)
Post #865881
Posted Tuesday, February 16, 2010 5:33 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:48 PM
Points: 11,194, Visits: 11,169
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
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #866010
Posted Tuesday, February 16, 2010 7:56 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:43 AM
Points: 1,938, Visits: 1,162
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)
Post #866131
Posted Wednesday, February 17, 2010 5:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:48 PM
Points: 11,194, Visits: 11,169
Thanks very much!



Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #866907
Posted Friday, July 11, 2014 5:13 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 18, 2014 2:13 PM
Points: 1, Visits: 34
http://sqlmag.com/t-sql/how-sql-server-clr-and-c-can-solve-common-problem
Post #1591825
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse