Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 pivot / unpivot Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, February 19, 2014 1:45 AM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, May 13, 2014 6:45 AM Points: 1, Visits: 24
 hi...pls help...i have a table with data as : emp_code paydate basic hra med splall ....a001 31-01-14 10000 2000 100 1000a002 31-01-14 20000 2000 100 1000a003 31-01-14 15000 2000 100 1000a004 31-01-14 10000 2000 100 1000a005 31-01-14 10000 2000 100 1000a006 31-01-14 10000 2000 100 1000a001 31-12-13 10000 2000 100 1000a002 31-12-13 20000 2000 100 1000a003 31-12-13 15000 2000 100 1000a004 31-12-13 10000 2000 100 1000a005 31-12-13 10000 2000 100 1000a006 31-12-13 10000 2000 100 1000.................................................................AND NOW I NEED OUTPUT IN GIVEN FORMAT : 31-01-14 31-12-13BASIC 75000 75000HRA 12000 12000MED 600 600SPLALL 6000 6000NOTE : SUM OF BASIC OF ALL EMP IN A MONTH 31-01-14: 75000
Post #1542885
 Posted Wednesday, February 19, 2014 2:54 AM This worked for the OP
 SSCrazy Eights Group: General Forum Members Last Login: Thursday, December 1, 2016 10:11 AM Points: 8,587, Visits: 18,753
 Hi and welcome to ssc. There's an article for newcomers which you may find useful; http://www.sqlservercentral.com/articles/Best+Practices/61537/If you take a little time to prepare a sample data script, forum users will be encouraged to participate in your thread.This time I’ve prepared your sample data for you;`-- Sample data setupSET DATEFORMAT DMYDROP TABLE #SampleCREATE TABLE #Sample (emp_code VARCHAR(4), paydate DATE, [basic] INT, hra INT, med INT, splall INT)INSERT INTO #Sample VALUES('a001', '31-01-14', 10000, 2000, 100, 1000),('a002', '31-01-14', 20000, 2000, 100, 1000),('a003', '31-01-14', 15000, 2000, 100, 1000),('a004', '31-01-14', 10000, 2000, 100, 1000),('a005', '31-01-14', 10000, 2000, 100, 1000),('a006', '31-01-14', 10000, 2000, 100, 1000),('a001', '31-12-13', 10000, 2000, 100, 1000),('a002', '31-12-13', 20000, 2000, 100, 1000),('a003', '31-12-13', 15000, 2000, 100, 1000),('a004', '31-12-13', 10000, 2000, 100, 1000),('a005', '31-12-13', 10000, 2000, 100, 1000),('a006', '31-12-13', 10000, 2000, 100, 1000)SELECT * FROM #Sample; -- check everything is ok (Stare & Compare)`Here’s a solution which employs both CROSS APPLY VALUES (a handy unpivot alternative championed by Dwain Camps) and CROSSTAB (championed by MVP Jeff Moden);`-- SolutionSELECT Category, [31-01-14] = SUM(CASE WHEN paydate = '31-01-14' THEN Amount END), [31-12-13] = SUM(CASE WHEN paydate = '31-12-13' THEN Amount END)FROM #SampleCROSS APPLY ( VALUES ('basic', [basic]), ('hra', hra), ('med', med), ('splall', splall)) x (Category, Amount) GROUP BY Category`Test it rigorously. If there are any issues, post back, extending your sample data set if necessary to help explain your findings. “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1542903

 Permissions

 Copyright © 2002-2016 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.