Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

SQL query having pivot and cross join giving duplicate values

$
0
0

Hi,

I am getting duplicate values when I execute the following sql query. Where am I going wrong? 

Pl guide...

BEGIN
;With Table1
AS
(
SELECT EmpId,EmpName, MonthE as [MonthEmol], YearE as [YearEmol], [Basic] as Basic, [HA] as HA, [DA] as DA, [SA] as SA, [Gross] as Gross
from
(select a.EmpId,b.EmpName, a.MonthE, a.YearE, a.HeadName, a.HeadAmt from Employee_Earning a left join Employee_Master b
on a.EmpId= b.EmpId ) as PivotData
pivot
(
SUM(HeadAmt) for HeadName in
(Basic, HA, DA, SA,Gross)
) as Pivoting
),

Table2
AS
(
SELECT EmpId,EmpName, MonthD as [MonthEmol], YearD as [YearEmol], [UTS] as UTS, [GF S] as GF_S,
[GF Advance] as GF_Advance, [Advance] as Advance,
[C Allowance] as C_Allowance,
[C Charges] as C_Charges, [W Charges] as W_Charges,[L Deductions] as L_Deductions,[Total] as Total
from
(select a.EmpId,b.EmpName, a.MonthD, a.YearD, a.HeadName, a.HeadAmt from Employee_Deduction a left join Employee_Master b
on a.EmpId=b.EmpId) as PivotData
pivot
(
SUM(HeadAmt) for HeadName in
(UTS, [GF S], [GF Advance], Advance, [C Allowance], [C Charges], [W Charges], [L Deductions], Total)
) as Pivoting2
)

Select Table1.* ,Table2.*, (Table1.Gross - Table2.Total) as NetPayable
from Table1 cross join Table2
ORDER BY Table1.EmpId,Table1.EmpName

END

Pl help...


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>