Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Monthly totals listed more than once - need only once

$
0
0

Hi,

I have this query that adds the 'current amount' column and sums it by month:

--Query Changed to include new codes

;WITH cte AS 
(
select * from (SELECT 
distinct pehPErcontrol ,
DATENAME(MONTH, pehpaydate) AS  [UltiMonth],
rtrim(eepNameLast) + 
',  ' + rtrim(eepNameFirst) + 
' ' + coalesce(substring(eepNameMiddle,1,1) + '.', '') as Name, 
eepNameLast AS [Last Name],
IsNull(eepNameSuffix,'') AS [Suffix],
eepNameFirst AS [First Name],
IsNull(eepNameMiddle,'') AS [Middle Name],
pehCurAmt AS [Current Amount], 
pehCurHrs AS [Current Hours], 
pehCoID AS [Company ID], 
pehEEID AS [EE ID], 
pehEmpNo AS [Emp No], 
pehLocation AS [Location], 
pehJobCode AS [Job Code], 
pehOrgLvl1 AS [Org Level 1],    
pehOrgLvl2 AS [Org Level 2], 
pehOrgLvl3 AS [Org Level 3], 
pehOrgLvl4 AS [Org Level 4], 
pehPayGroup AS [Pay Group], 
pehProject AS [Project], 
pehShfShiftAmt AS [Shift Amount],
pehearncode AS [Earn Code],
pehIsVoided AS [IS Voided],
pehIsVoidingRecord AS [Voiding Record],
pehIsOvertime AS [Is Overtime],
pehpaydate AS [Pay Date]
FROM EmpPers JOIN pearhist ph ON ph.pehEEID = eepEEID 
right outer join  WSISQL4.DASHBOARD.DBO.[OVERTIME_BUDGET_2013] 
	ON [orglevel] = pehOrgLvl2) t 
right outer join  
	WSISQL4.DASHBOARD.DBO.[OVERTIME_BUDGET_2013] ob 
		on t.[UltiMonth] = ob.[month] and orglevel = [org level 2]
where [Pay Date] >= '2013-01-01' 
AND [EARN CODE] IN ('0002', '0022','0025','0066','0104','A102','N002','N104', '002D', '0071','0096','0104','0121', '0123', '0151', '0200', '0201', '0202', '0205', 'A102', 'CREW', 'N002', 'N104', 'TXWP' )
AND [IS Voided] <> 'Y'
AND [Voiding Record] <> 'Y' 
AND [Is Overtime] = 'Y' 
AND  [org level 2] like '%ZPR'
AND [ULTIMONTH] = 'JANUARY'
--ORDER BY pehpaydate
--AND [NAME] = 'Moskowitz,  Joshua' 
)
SELECT *
	,SUM([Current Amount]) 
		OVER (PARTITION BY [Emp No],[UltiMonth]) AS [Monthly Amount]
FROM cte

My problem now, is that I get the same total for the month multiple times (in the Monthly Amount column).

Here's a pic:

This is close to what I need (its going in a dashboard). So, instead of

Monthly Amount
190.03
190.03
240.28
240.28
164.69
164.69

I need it to be Unique :

Monthly Amount

190.03

240.28

164.69


qeqw


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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