2013-09-26 46 views
1

已更新。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存在(所以我們有電話號碼的債務人)

+0

你想行的數據顯示爲列? –

+0

我希望顯示結果,如 用戶1 50 75;用戶2 46 78; etc – glaeran

回答

2

如果你只想在一行中顯示所有內容,你可以試試這個

SELECT MAX([Ztel]), MAX([Beztel]), MAX([Windykator]) 
FROM (
     ..... 
    ) AS x 

UPDATE:

基礎上您的意見,我想你正在尋找的是這個

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] 
+0

只顯示1行。點是我需要顯示幾行,因爲結果包含乘法用戶和數字 – glaeran

+0

更新完美。謝謝! – glaeran

+0

已更新的第一篇文章。看看空閒時間請。 – glaeran