我有一个我正在写的查询,它几乎是完美的,除了我似乎无法控制的一个错误。
SELECT Clients.client_id,
Clients.last_name + ', ' + Clients.first_name AS Client_Name,
Query3.team_id,
Query3.team_name,
Query2.CSW_Name,
Query1.Date_of_Service AS Last_Service_by_CSW,
Query.Tx_Start_Date AS Tx_Start_Date,
Query.Tx_End_Date AS Tx_End_Date
FROM Clients
LEFT OUTER JOIN
(SELECT TxPlus.client_id,
Max(TxPlus.start_date) AS Tx_Start_Date,
Max(TxPlus.end_date) AS Tx_End_Date
FROM TxPlus
GROUP BY TxPlus.client_id) Query ON Query.client_id = Clients.client_id
LEFT OUTER JOIN
(SELECT EmployeeClients.client_id,
Employees.last_name + ', ' + Employees.first_name AS CSW_Name
FROM EmployeeClients
INNER JOIN Employees ON EmployeeClients.emp_id = Employees.emp_id
WHERE EmployeeClients.case_manager = 1) Query2 ON Clients.client_id = Query2.client_id
LEFT OUTER JOIN
(SELECT ClientVisit.client_id,
Max(ClientVisit.rev_timeout) AS Date_of_Service
FROM ClientVisit
INNER JOIN EmployeeClients ON ClientVisit.emp_id = EmployeeClients.emp_id
GROUP BY ClientVisit.client_id,
EmployeeClients.case_manager,
ClientVisit.visittype_id
HAVING EmployeeClients.case_manager = 1
AND ClientVisit.visittype_id = 9) Query1 ON Clients.client_id = Query1.client_id
LEFT OUTER JOIN
(SELECT TeamClient.client_id,
Team.team_id,
Team.team_name
FROM TeamClient
INNER JOIN Team ON TeamClient.team_id = Team.team_id
WHERE TeamClient.primary_flag = 1) Query3 ON Clients.client_id = Query3.client_id
WHERE Clients.client_status LIKE 'Active'
目标是基本上提取客户记录,并显示他们的治疗计划,他们的主要团队任务,他们的个案经理以及案例管理员为他们提供的大多数社区支持服务的日期。
现在,根据编写的代码,我获得了所有正确的信息,除了最近的社区支持日期。这将返回由ANYONE完成的最新服务,而不是Case Manager。
我确定这是一个非常简单的问题,但它正在推动我的发展。
你们都是宝贵的资源,我提前感谢你们。
答案 0 :(得分:0)
嗯,不知道数据是什么样的,我不能100%肯定这是答案,但我对它有很好的感觉。我相信这是返回“社区支持日期”的查询。 (它没有像我希望的那样清晰地识别出来,但它看起来像我这样。)
(SELECT ClientVisit.client_id,
Max(ClientVisit.rev_timeout) AS Date_of_Service
FROM ClientVisit
INNER JOIN EmployeeClients ON ClientVisit.emp_id = EmployeeClients.emp_id
GROUP BY ClientVisit.client_id,
EmployeeClients.case_manager,
ClientVisit.visittype_id
HAVING EmployeeClients.case_manager = 1
AND ClientVisit.visittype_id = 9) Query1 ON Clients.client_id = Query1.client_id
所以你正在做的是在这两个表中创建数据的子集,由emp_id
加入,并按客户端,案例管理器和访问类型分组。然后,您删除任何不是案例管理员或您想要的访问类型的组。但到目前为止,您已经选择了Max(ClientVisit.rev_timeout) AS Date_of_Service
。我相信如果您将HAVING
子句更改为WHERE
子句(并在分组之前进行过滤),您将获得所需的结果。
编辑:
(SELECT ClientVisit.client_id,
Max(ClientVisit.rev_timeout) AS Date_of_Service
FROM ClientVisit
INNER JOIN EmployeeClients ON ClientVisit.emp_id = EmployeeClients.emp_id
WHERE EmployeeClients.case_manager = 1
AND ClientVisit.visittype_id = 9
GROUP BY ClientVisit.client_id) Query1 ON Clients.client_id = Query1.client_id