I modified a view I had built to include a Grand total but would like to know if there is a better way?
Thanks
John Fuhrman
Code:
With cte_TodaysRecords(EmployeeFullName, BoxNumber, FileNumber, TrackingDate)
As (
SELECT
EmployeeFullName = Case
When e.EmployeeMI = '' Then e.EmployeeLN + ', ' + e.EmployeeFN
When e.EmployeeMI Is Null Then e.EmployeeLN + ', ' + e.EmployeeFN
When e.EmployeeMI <> '' Then e.EmployeeLN + ', ' + e.EmployeeFN + ' ' + e.EmployeeMI
End,
a.BoxNumber As [Tracking Number],
a.FileNumber As [File Number],
a.TrackingDate As [Tracking Date and Time]
FROM dbo.tblTrackingTable a
INNER JOIN dbo.tblEmployee e ON a.EmployeeID = e.EmployeeID
WHERE (a.TrackingDate
BETWEEN DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 0)
AND DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 1)))
AND (a.EmployeeID IS NOT NULL)
AND (a.FileNumber <> '')
AND (a.BoxNumber <> '')
AND (a.FileNumber <> '.BOX.END.')
AND (a.TrackingDate IS NOT NULL)
),
cte_Totals([Employee Full Name], [Total Tracking Numbers], [Total Folders Scanned])
As (
Select
Cast(EmployeeFullName As VarChar(35)) As [Employee Full Name],
Count(Distinct BoxNumber) As [Total Tracking Numbers],
Count(FileNumber) As [Total Folders Scanned]
From cte_TodaysRecords
Group By EmployeeFullName
)
Select
T.[Employee Full Name],
T.[Total Tracking Numbers],
T.[Total Folders Scanned],
Case When [Employee Full Name] = (Select Top 1 [Employee Full Name] from cte_Totals
Order By [Employee Full Name] Desc)
Then (Select Sum([Total Folders Scanned]) From cte_Totals)
Else 0
End As 'Grand Total - Folders'
From cte_Totals T
Thanks
John Fuhrman