更新
当前查询如下:
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&lt;&gt;的总体案例数2和&lt; 2&gt; 6
第4列:CaseStatusId&lt;&gt;的总体案例数2和&lt; 2&gt; 6,存在于DebtorPhone中(因此我们获得电话号码给债务人的案件数量)
第5列:Open案例数(CaseStatusId = 1)总数
第6列:公开案例的数量(CaseStatusId = 1)并存在于DebtorPhone中(因此我们有债务人的电话号码)
答案 0 :(得分:2)
如果您只想在一行中显示所有内容,可以试试这个
SELECT MAX([Ztel]), MAX([Beztel]), MAX([Windykator])
FROM (
.....
) AS x
<强>更新强>
根据您的评论,我认为您正在寻找的是
SELECT MAX([Ztel]), MAX([Beztel]), [Windykator]
FROM (
SELECT COUNT(C.CaseDetailsId) AS [Ztel], NULL AS [Beztel], 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), 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
) AS x
GROUP BY [Windykator]