Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Aggrating data

Author  Topic 

Menorel
Starting Member

15 Posts

Posted - 2013-07-15 : 09:12:02
In the data table there is the possibility of each date having multiple time entries for a user. as well as multiple dates for the user. I am attempting to aggragte this down to a single record but I am still getting one record for each date.
Is there something I am missing about aggragation that is not allowing it to populate to one record.

Date range used would be for Between 7/7/2013 and 7/13/2013

Temp Table and Query:

CREATE TABLE #tbl_WORKEDPayRoll(
EmployeeID int,
ExternalID int,
TempDept int,
ActivityType nchar(6),
Worked decimal(10,2),
Is_Paid bit,
Is_Holiday bit)

INSERT INTO #tbl_WORKEDPayRoll
(EmployeeID, ExternalID, TempDept, ActivityType, Worked, Is_Paid, Is_Holiday)
SELECT EmployeeID, ExternalID, TempDept, ActivityType, SUM(Worked) AS Worked, Is_Paid, CASE WHEN Is_Holiday IS NULL
THEN 0 ELSE Is_Holiday END AS Is_Holiday
FROM vw_PayRollDataWithHoliday
GROUP BY EmployeeID, ExternalID, StartDate, TempDept, ActivityType, Is_Paid, CASE WHEN Is_Holiday IS NULL THEN 0 ELSE Is_Holiday END
HAVING (StartDate BETWEEN @StartDate AND @EndDate) AND (ExternalID = @ExternalID) AND (ActivityType = N'WORKED')

SELECT * FROM #tbl_WORKEDPayRoll



Sample data from Select statement:

EmployeeID ExternalID TempDept ActivityType Worked Is_Paid Is_Holiday
520691 3662 249983 WORKED 5.98 1 0
520691 3662 249983 WORKED 7.93 1 0
520691 3662 249983 WORKED 8.11 1 0
520691 3662 249983 WORKED 3.49 1 0

I would like it to populate down to:

EmployeeID ExternalID TempDept ActivityType Worked Is_Paid Is_Holiday
520691 3662 249983 WORKED 25.51 1 0

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-07-15 : 09:56:00
[code]SELECT EmployeeID,
ExternalID,
TempDept,
ActivityType,
SUM(Worked) AS Worked,
Is_Paid,
ISNULL(Is_Holiday, 0) AS Is_Holiday
FROM dbo.vw_PayRollDataWithHoliday
WHERE StartDate BETWEEN @StartDate AND @EndDate
AND ExternalID = @ExternalID
AND ActivityType = N'WORKED'
GROUP BY EmployeeID,
ExternalID,
TempDept,
ActivityType,
Is_Paid,
ISNULL(Is_Holiday, 0);[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Menorel
Starting Member

15 Posts

Posted - 2013-07-15 : 11:45:41
Worked like a charm.
Go to Top of Page
   

- Advertisement -