Another query similar to the other where I want to SUM CASE but only count where unique;
Here's the code:
This returns 5 columns of data (time_stamp, Time, All, UK, US), however the SUM CASES will simply return a count where the conditions are met in the WHERE clause, where as I want to only SUM based on the unique user in that grouped hour. Ergo All should be the sum of UK and US for that grouped hour.
It's another subquery but again where I'm using the COUNT (DISTINCT in this instance) it's throwing me.
Any pointers much appreciated.
Cheers, Paul.
Here's the code:
Code:
SELECT TOP (100) PERCENT dbo.vw_ctrx_archive_session_perf.time_stamp, CONVERT(varchar(2), DATEADD(day, 0, dbo.vw_ctrx_archive_session_perf.time_stamp), 105)
+ '-' + CONVERT(varchar(2), DATEADD(hh, 0, dbo.vw_ctrx_archive_session_perf.time_stamp), 108) + ':00' AS Time, COUNT(DISTINCT dbo.vw_ctrx_archive_session_perf.[user])
AS [All], SUM(CASE WHEN dbo.vw_ctrx_archive_session_perf.dept_path LIKE 'All\XenApp Farms\GSK_XA65\SomeServers\UK%' THEN 1 ELSE 0 END) AS UK,
SUM(CASE WHEN dbo.vw_ctrx_archive_session_perf.dept_path LIKE 'All\XenApp Farms\GSK_XA65\SomeServers\US%' THEN 1 ELSE 0 END) AS US
FROM dbo.vw_ctrx_archive_session_perf LEFT OUTER JOIN
dbo.vw_ctrx_archive_published_app_event ON dbo.vw_ctrx_archive_session_perf.sessid = dbo.vw_ctrx_archive_published_app_event.sessid
WHERE (dbo.vw_ctrx_archive_session_perf.dept_path LIKE 'All\XenApp Farms\SomeServers%') AND (dbo.vw_ctrx_archive_session_perf.time_stamp BETWEEN
GETDATE() - 3 AND GETDATE() - 0.25)
GROUP BY dbo.vw_ctrx_archive_session_perf.time_stamp, CONVERT(varchar(2), DATEADD(hh, 0, dbo.vw_ctrx_archive_session_perf.time_stamp), 108)
ORDER BY dbo.vw_ctrx_archive_session_perf.time_stamp
This returns 5 columns of data (time_stamp, Time, All, UK, US), however the SUM CASES will simply return a count where the conditions are met in the WHERE clause, where as I want to only SUM based on the unique user in that grouped hour. Ergo All should be the sum of UK and US for that grouped hour.
It's another subquery but again where I'm using the COUNT (DISTINCT in this instance) it's throwing me.
Any pointers much appreciated.
Cheers, Paul.
Last edited: