已更新。NULL與Union
當前查詢的樣子:
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,用戶ID,EndServiceDate,CaseDetailsId,CaseStatusId - 案例整體名單
DebtorDetails:CaseDetailsId,DebtorDetailsId - 債務人細節
DebtorPhone:DebtorDetailsId - 債務人電話號碼列表。
欄3:總體病例數,其中CaseStatusId <> 2和<> 6
Column4:總體病例數,其中CaseStatusId <> 2和<> 6和存在於DebtorPhone(在這裏我們得到了手機的情況下,這樣的數數債務人)
Column5:開放病例數(其中CaseStatusId = 1)整體
Column6:開放病例數(其中CaseStatusId = 1)和DebtorPhone存在(所以我們有電話號碼的債務人)
你想行的數據顯示爲列? –
我希望顯示結果,如 用戶1 50 75;用戶2 46 78; etc – glaeran