2017-04-25 59 views
1

我想創建一個報告,該報告給出了某個LabourID(L_ID)每月的事務總數。但我似乎無法得到我想要得到的東西。SQL Server 2014-代碼中的錯誤

我的代碼如下:

SET NOCOUNT ON 

Declare @S AS DateTime = ISNULL(@StartDate,DateAdd(d,-60,GETDATE())) 
IF OBJECT_ID('tempdb..#Count') IS NOT NULL DROP TABLE #Count; 

SELECT Year(TranxDate) AS YY 
     ,Month(TranxDate) AS MM 
     ,TT.L_ID 
     ,count(IR.T_TransactionId) TranxCount 

INTO #Count 

FROM  IR 
INNER JOIN TT ON IR.T_TransactionId = TT.T_TransactionId 

WHERE [TranxDate] >= @StartDate 
    AND [Some condition] 
    AND [Some condition] 
    AND [Some condition] 
    AND TT.L_ID IN ('502','701','18','22','702','503') 

GROUP BY Year(TranxDate), Month(TranxDate), TT.L_ID 


SELECT YY 
     ,MM 
     ,(SELECT TranxCount FROM #Count WHERE L_ID = '701')ID701 
     ,(SELECT TranxCount FROM #Count WHERE L_ID = '502')ID502 
     ,(SELECT TranxCount FROM #Count WHERE L_ID = '18')ID18 
     ,(SELECT TranxCount FROM #Count WHERE L_ID = '22')ID22 
     ,(SELECT TranxCount FROM #Count WHERE L_ID = '702')ID702 
     ,(SELECT TranxCount FROM #Count WHERE L_ID = '503')ID503 

FROM #Count 

GROUP BY YY, MM, L_ID 

ORDER BY YY, MM 

我從這個代碼得到一條錯誤消息:

子查詢返回多個值。當 子查詢遵循=,!=,<,< =,>,> =或當子查詢用作 表達式時,這是不允許的。是

的不是Temptable #COUNT的結果如下:有

Year Month L_ID TranxCount 
2016 9  18  4141 
2016 9  22  637 
2016 9  502  7782 
2016 9  503  531 
2016 9  701  7529 
2016 9  702  1577 
2016 10  18  4611 
2016 10  22  1007 
2016 10  502  9763 
2016 10  503  507 
2016 10  701  10093 
2016 10  702  2176 
2016 11  18  5421 
2016 11  22  1471 
2016 11  502  11505 
2016 11  503  613 
2016 11  701  12530 
2016 11  702  2569 

我想有結果如下:

YEAR Month ID701 ID502 ID18 ID22 ID702 ID503 
2016 9  7529 7782 4141 637  1577 531 
2016 10  10093 9763 4611 1007 2176 507 
2016 11  12530 11505 5421 1471 2569 613 
2016 12  ... 
2017 1 
... 
... 
.. 
. 

我在做什麼錯在我的代碼?

編輯:

按@Sean建議,我添加以下代碼:

SELECT YY 
     ,MM 
     , MAX(Case when L_ID = '701' then TranxCount end) as ID701 
     , MAX(Case when L_ID = '502' then TranxCount end) as ID502 
     , MAX(Case when L_ID = '18' then TranxCount end) as ID18 
     , MAX(Case when L_ID = '22' then TranxCount end) as ID22 
     , MAX(Case when L_ID = '702' then TranxCount end) as ID702 
     , MAX(Case when L_ID = '503' then TranxCount end) as ID503 
FROM #Count 
GROUP BY YY, MM, L_ID 
ORDER BY YY, MM 

但我得到以下結果:

YY  MM ID701 ID502 ID18 ID22 ID702 ID503 
2017 1 NULL NULL 2793 NULL NULL NULL 
2017 1 NULL NULL NULL 1407 NULL NULL 
2017 1 NULL 7763 NULL NULL NULL NULL 
2017 1 NULL NULL NULL NULL NULL 608 
2017 1 7919 NULL NULL NULL NULL NULL 
2017 1 NULL NULL NULL NULL 832 NULL 
2017 2 NULL NULL 2874 NULL NULL NULL 
2017 2 NULL NULL NULL 1694 NULL NULL 
2017 2 NULL 7369 NULL NULL NULL NULL 
2017 2 NULL NULL NULL NULL NULL 489 
2017 2 8306 NULL NULL NULL NULL NULL 
2017 2 NULL NULL NULL NULL 778 NULL 
2017 3 NULL NULL 3809 NULL NULL NULL 
2017 3 NULL NULL NULL 2003 NULL NULL 
2017 3 NULL 10368 NULL NULL NULL NULL 

至少有進展。有沒有辦法刪除空記錄,讓他們反而像我想要他們?

編輯2:

作爲每@Sean建議,添加以下代碼,然後由一個臨時表:

SELECT YY 
     ,MM 
     , MAX(Case when L_ID = '701' then TranxCount end) as ID701 
     , MAX(Case when L_ID = '502' then TranxCount end) as ID502 
     , MAX(Case when L_ID = '18' then TranxCount end) as ID18 
     , MAX(Case when L_ID = '22' then TranxCount end) as ID22 
     , MAX(Case when L_ID = '702' then TranxCount end) as ID702 
     , MAX(Case when L_ID = '503' then TranxCount end) as ID503 
INTO #TotCount 
FROM #Count 
GROUP BY YY, MM, L_ID 
ORDER BY YY, MM 

SELECT YY 
    ,MM 
    ,SUM(ID701) 
    ,SUM(ID502) 
    ,SUM(ID18) 
    ,SUM(ID22) 
    ,SUM(ID702) 
    ,SUM(ID503) 

FROM #TotCount 

GROUP BY YY, MM, L_ID 

ORDER BY YY, MM 

我得到的錯誤消息:

無效列L_ID

+0

善良我希望那些不是你真正的表名。呸。錯誤很明顯。至少有一個子查詢返回多於一行。 –

回答

1

我解決了這個問題,方法是創建另一個臨時表,然後從第二個臨時表中取出一個SUM值,該值取出空值。
我也刪除了L_ID的分組。

所以我的代碼如下:

SELECT YY 
     ,MM 
     , MAX(Case when L_ID = '701' then TranxCount end) as ID701 
     , MAX(Case when L_ID = '502' then TranxCount end) as ID502 
     , MAX(Case when L_ID = '18' then TranxCount end) as ID18 
     , MAX(Case when L_ID = '22' then TranxCount end) as ID22 
     , MAX(Case when L_ID = '702' then TranxCount end) as ID702 
     , MAX(Case when L_ID = '503' then TranxCount end) as ID503 
INTO #TotCount 
FROM #Count 
GROUP BY YY, MM, L_ID 
ORDER BY YY, MM 

SELECT YY 
    ,MM 
    ,SUM(ID701) 
    ,SUM(ID502) 
    ,SUM(ID18) 
    ,SUM(ID22) 
    ,SUM(ID702) 
    ,SUM(ID503) 

FROM #TotCount 

GROUP BY YY, MM--, L_ID 

ORDER BY YY, MM 

此代碼的工作,給了我希望的結果。

謝謝@Sean試圖幫助。我很感激。

+0

只需從第一組中刪除L_ID,而不需要相同數據的其他副本。 –

2

您可以使用條件聚合來實現此目的。

SELECT YY 
     ,MM 
     , MAX(Case when L_ID = '701' then TranxCount end) as ID701 
     , MAX(Case when L_ID = '502' then TranxCunt end) as ID502 
     , MAX(Case when L_ID = '18' then TranxCount end) as ID18 
     , MAX(Case when L_ID = '22' then TranxCount end) as ID22 
     , MAX(Case when L_ID = '702' then TranxCount end) as ID702 
     , MAX(Case when L_ID = '503' then TranxCount end) as ID503 
FROM #Count 
GROUP BY YY, MM, L_ID 
ORDER BY YY, MM 
+0

謝謝@Sean。但你的代碼給了我空值(請參閱我的問題編輯)。我將如何刪除Null值並將其顯示爲我想要的值? – user1777929

+0

從中取出L_ID。 –