Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Total by Group.

$
0
0
Dear All,I am trying to bring the group total .below my query,[code="sql"]WITH Prep AS(SELECT preffered_name as Department ,email_address1 as subdepartment,friday,saturday,government_num as dutyhours,ci.person_num, ci.full_name, ci.event_name, CAST(DATEADD(hour,1,dbo.UtcToLocal(ci.creation_date)) AS DATE) AS event_date,DATEname(weekday,dbo.UtcToLocal(co.creation_date)) AS Day, CAST(dbo.UtcToLocal(ci.creation_date) AS date) AS in_date, CAST(dbo.UtcToLocal(ci.creation_date) AS time) AS in_time, CAST(dbo.UtcToLocal(co.creation_date) AS date) AS out_date, CAST(dbo.UtcToLocal(co.creation_date) AS time) AS out_time, DATEDIFF(minute, ci.creation_date, co.creation_date) AS minutes FROM TEST ci OUTER APPLY (SELECT TOP 1 creation_date FROM TEST t WHERE t.person_num = ci.person_num AND t.creation_date >= ci.creation_date AND t.event_name = 'CLOCK_OUT' ORDER BY t.creation_date) co WHERE ci.event_name = 'CLOCK_IN' AND preffered_name='finance' ), CalcPart AS(SELECT department,subdepartment,dutyhours,person_num, full_name, event_date, in_time, out_time, minutes,day,friday,saturday, ROW_NUMBER() OVER (PARTITION BY event_date, person_num ORDER BY in_time) AS daypart FROM Prep) SELECT subdepartment,--full_name, SUM(minutes) / 60.0 AS HoursWorked, dutyhours,Day,friday,saturday, SUM(minutes) / 60.0 AS HoursWorked, case when ( [friday] = 1 and [day] = 'Friday' ) or ( [saturday] = 1 and [day] = 'Saturday' ) then SUM(minutes) / 60.0 else sum(( [minutes] /60.0)) - [Dutyhours] end as OT FROM CalcPart GROUP BY subdepartment,dutyhours,day,friday,saturday[/code]CURRENT RESULTS [img]http://i67.tinypic.com/hrygyc.jpg[/img]EXPECTED OUTPUT[img]http://i65.tinypic.com/2njisqo.jpg[/img]

Viewing all articles
Browse latest Browse all 3145

Trending Articles