I am trying to identify from a table of clients, only those who have handled a particular type of case (kf1.WorkSource2 = 4). This is in addition to more standard cases (there are some who never have kf1.WorkSource2 = 4 so the COUNT would be 0), so I want to ignore those where the COUNT is 0.
The script below produces a list of ALL including the COUNT = 0. I've tried the GROUP by / Having route (ending the sub-query further after the HAVING) but this doesn't work.
I can export the result to Excel & use a sort to identify the 0 lines for deletion, but I'd like to be able to automate and schedule this so need to resolve the SQL issue.
SELECT ts.client, ts.clientPostCode,
(SELECT COUNT(kf1.FileId)
FROM Reports1.dbo.KeyFields kf1
WHERE kf1.WorkSource2 = 4
AND ts.clientId = kf1.client2) as Private_Cases
-- GROUP BY ts.clientId
-- HAVING COUNT(kf1.FileId) > 0) as Private_Cases
FROM v2data.dbo.tclient ts
WHERE clientStatus = 'ACTIVE'
ORDER BY ts.clientName
Help would be appreciated.
Paul
The script below produces a list of ALL including the COUNT = 0. I've tried the GROUP by / Having route (ending the sub-query further after the HAVING) but this doesn't work.
I can export the result to Excel & use a sort to identify the 0 lines for deletion, but I'd like to be able to automate and schedule this so need to resolve the SQL issue.
SELECT ts.client, ts.clientPostCode,
(SELECT COUNT(kf1.FileId)
FROM Reports1.dbo.KeyFields kf1
WHERE kf1.WorkSource2 = 4
AND ts.clientId = kf1.client2) as Private_Cases
-- GROUP BY ts.clientId
-- HAVING COUNT(kf1.FileId) > 0) as Private_Cases
FROM v2data.dbo.tclient ts
WHERE clientStatus = 'ACTIVE'
ORDER BY ts.clientName
Help would be appreciated.
Paul