Hi and welcome to ssc.
There's an article for newcomers which you may find useful; http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]
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 setup
SET DATEFORMAT DMY
DROP TABLE #Sample
CREATE 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 [/url](a handy unpivot alternative championed by Dwain Camps) and CROSSTAB[/url] (championed by MVP Jeff Moden);
-- Solution
SELECT
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 #Sample
CROSS 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.
For 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 Moden