Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.77/5 (3 votes)
I had a Table Employee master in my SQL Database

Employeemaster Table
SQL
Empid	EmpName
	
1	Emp1
2	Emp6
3	Emp4
4	Emp4



Another Table Employeesalarydetails
SQL
Empid	Component	Amount
		
2	Basic	10000
2	Hra	1000
2	TA	750
1	Basic	20000
1	Hra	1000
3	Basic	6700
3	Hra	1000
4	Basic	5000
4	Hra	1000



Now I want to Create a query in a way that result will be in form of will be in the form of
SQL
Empid	Empanme	 Basic	Hra	TA
1	Emp1	20000	1000	0
2	Emp6	10000	1000	750
3	Emp4	6700	1000	0
4	Emp4	5000	1000	0


I had tried all the possible methods in the Cross Query .Can anyone suggest me a idea
Posted
Updated 22-Sep-14 0:04am
v2

Below is a sample reference for dynamic columns (ie. u dont need to hard code 'Component' value in query)

Sample Data #tempA,#tempB

SQL
create Table #tempA (Empid bigint,EmpName VARCHAR(50))
INSERT INTO #tempA (Empid ,EmpName )VALUES(1,'Emp1')
INSERT INTO #tempA (Empid ,EmpName )VALUES(2,'Emp6')
INSERT INTO #tempA (Empid ,EmpName )VALUES(3,'Emp4')
INSERT INTO #tempA (Empid ,EmpName )VALUES(4,'Emp4')

create Table #tempB (id bigint ,Empid bigint,Component VARCHAR(50),Amount bigint)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(1,2,'Basic',10000)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(2,2,'Hra',1000)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(3,2,'TA',750)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(4,1,'Basic',20000)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(5,1,'Hra',1000)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(6,3,'Basic',6700)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(7,3,'Hra',1000)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(8,4,'Basic',5000)
INSERT INTO #tempB (id,Empid ,Component,Amount )VALUES(9,4,'Hra',1000)

Combine Two Tables into #tempc

SQL
SELECT id,#tempB.Empid,EmpName,Component,Amount INTO #tempc
FROM #tempB
INNER JOIN #tempA ON #tempA.Empid=#tempB.Empid

Declare variables

SQL
DECLARE @cols NVARCHAR (MAX),
@selCols NVARCHAR (MAX),
@query  AS VARCHAR(MAX);


Fetch columns for dynamic query like [Basic],[Hra],[TA]

SQL
SELECT  @cols = COALESCE (@cols + ',[' + Component + ']', '[' + Component + ']') 
FROM    #tempB
group by Component


Fetch columns for Aliasing in dynamic query like [Basic] AS Basic,[Hra] AS Hra,[TA] AS TA
SQL
SELECT  @selCols = COALESCE (@selCols + ',[' + Component + ']', '[' + Component + ']') + ' AS '+ Component
FROM    #tempB
group by Component


Fetch columns for SUM in dynamic query like SUM(isnull(Basic,0)) AS Basic,SUM(isnull(Hra,0)) AS Hra,SUM(isnull(TA,0)) AS TA
SQL
SET @selCols=REPLACE(@selCols,'[','SUM(isnull(')
SET @selCols=REPLACE(@selCols,']',',0))')


Final Query
SQL
SET @query='SELECT Empid,EmpName,'+ @selCols+
		' FROM #tempc 
		PIVOT(
				SUM(Amount) 
				FOR Component 
				IN('+@cols+')		
				
		)AS empPivot
		group by Empid,EmpName'
execute(@query)		


good luck ;-)
 
Share this answer
 
You should use the Pivot methods in SQL Server.
Read my article[^] about how to do so.
 
Share this answer
 
Hi,

Check this...

Simple Way To Use Pivot In SQL Query[^]

... for EmployeeSalaryDetails table.

I will like to give you one more chance to try by your own to get required output.

Hope this will help you.


Cheers.
 
Share this answer
 
Try Live test, http://www.sqlfiddle.com/#!3/ad5e6/1[^] or
SQL
WITH Dtl
AS
(
	SELECT *
		FROM Employeesalarydetails
		PIVOT(
			SUM(Amount) 
				FOR Component 
				IN([Basic],[Hra],[TA])		-- pivot for [Basic],[Hra],[TA]
		)AS DtlPivot
)
SELECT Emp.*, 
	COALESCE(Dtl.[Basic], 0) AS [Basic],	-- assigning names for [Basic],[Hra],[TA]
	COALESCE(Dtl.[Hra], 0) AS [Hra],
	COALESCE(Dtl.[TA], 0) AS [TA]
	FROM Employeemaster AS Emp
	LEFT JOIN Dtl ON Emp.EmpId = Dtl.EmpId
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900