更新。
当前查询如下所示:
SELECT MAX([Ztel]) as [Ztel] , MAX([Beztel]) as [Beztel], MAX([Otwarte]) as [Otwarte], MAX([Otwartez]) as [Otwartez], [Windykator]
FROM (
SELECT COUNT(C.CaseDetailsId) AS [Ztel], NULL AS [Beztel], NULL AS [Otwarte], NULL as [Otwartez], U.FirstName
+ ' ' + U.LastName AS [Windykator]
FROM CaseDetails AS C
JOIN DebtorDetails AS D
ON C.CaseDetailsId = D.CaseDetailsId
JOIN Users AS U
ON C.UserId = U.UserId
WHERE D.DebtorDetailsId IN (SELECT DebtorDetailsId
FROM DebtorPhone
WHERE (IsValid = 'True')
OR (IsDefault = 'True'))
AND C.CaseStatusId <> 2
AND C.CaseStatusId <> 6
GROUP BY
U.FirstName, U.LastName
UNION ALL
SELECT NULL, COUNT(CaseDetailsId), NULL, NULL, U.FirstName + ' ' + U.LastName AS [Windykator]
FROM CaseDetails AS C
JOIN Users AS U
ON C.UserId = U.UserId
WHERE C.CaseStatusId <> 2
AND C.CaseStatusId <> 6
GROUP BY
U.FirstName, U.LastName
UNION ALL
SELECT NULL, NULL, COUNT(CaseDetailsId), NULL, U.FirstName + ' ' + U.LastName AS [Windykator]
FROM CaseDetails AS C
JOIN Users AS U
ON C.UserId = U.UserId
WHERE C.CaseStatusId = 1
GROUP BY
U.FirstName, U.LastName
UNION ALL
SELECT NULL, NULL, NULL, COUNT(C.CaseDetailsId), U.FirstName
+ ' ' + U.LastName AS [Windykator]
FROM CaseDetails AS C
JOIN DebtorDetails AS D
ON C.CaseDetailsId = D.CaseDetailsId
JOIN Users AS U
ON C.UserId = U.UserId
WHERE D.DebtorDetailsId IN (SELECT DebtorDetailsId
FROM DebtorPhone
WHERE (IsValid = 'True')
OR (IsDefault = 'True'))
AND C.CaseStatusId = 1
GROUP BY
U.FirstName, U.LastName
) AS x
GROUP BY [Windykator]
工作正常,但他们希望我在数据集中添加 3 个额外的列。取自 CaseDetailsView 列:ClientNameShort、SetId、EndServiceDate。想法?或者我应该做另一个子查询?
预期结果: http: //oi43.tinypic.com/2vdi5vk.jpg
要使用的表:
CaseDetailsView:SetId、ClientNameShort、UserId、EndServiceDate、CaseDetailsId、CaseStatusId - 案例的总体列表
DebtorDetails: CaseDetailsId、DebtorDetailsId - 债务人的详细信息
DebtorPhone: DebtorDetailsId - 债务人的电话号码列表。
列 3:CaseStatusId <> 2 和 <> 6
的总体案例数 Column4:CaseStatusId <> 2 和 <> 6 且存在于 DebtorPhone 中的总体案例数(因此我们获得债务人电话号码的案例数)
Column5:未结案件的数量(其中 CaseStatusId = 1)总体
第 6 列:未结案件的数量(其中 CaseStatusId = 1)并存在于 DebtorPhone 中(因此我们有债务人的电话号码)