2013-07-18 25 views
0

嗨,我有一個出現在「未設置」兩次成爲拉動查詢:從列中選擇數據,其中空值和字符串不存在

enter image description here

現在一些檢查後,我知道這是因爲currentstage列具有NULLS和表中存儲的字符串'Not Set'。因此,下面會產生3個'Not set'字符串和195個NULLS被強制爲'Not Set'。 但是我真正想看到的是198 x'Not Sets'在我的#TempCircCount中。這怎麼能做到呢?

我的失敗代碼是在這裏:

IF OBJECT_ID('tempdb..#TempCircCount') is not null 
DROP TABLE #TempCircCount 
SELECT 
    ISNULL(cirRep.CurrentStage, 'Not Set') AS CurrentStage, 
    COUNT(ISNULL(cirRep.CurrentStage, 'Not Set')) AS Circuits 
INTO #TempCircCount 
FROM 
    [QuoteBase].[dbo].[CircuitReports] cirRep 
RIGHT JOIN 
    Quotebase.dbo.Circuits cir ON cir.[PW Number] = CirRep.[PWNumber] 
WHERE 
    Cir.Status='New Circuit Order' 
GROUP BY CurrentStage 
ORDER BY CurrentStage 

SELECT 
    ISNULL(CurrentStage, 'Not Set') AS [CurrentStage], 
    Circuits AS Circuits 
FROM #TempCircCount 
GROUP BY CurrentStage, Circuits 
ORDER BY CurrentStage 

回答

3

我相信,只要改變

GROUP BY CurrentStage 

GROUP BY ISNULL(cirRep.CurrentStage, 'Not Set') 

會工作。

GROUP BY從您的一個表格字段(即cirRep.CurrentStage)使用CurrentStage而不是select中的字段。 SQL Server不允許按select中的字段進行分組。

我也推薦而不是正是因爲這個原因,爲輸出字段使用了與已有字段相同的名稱。

+0

謝謝你的建議,表示感謝,解決方案解決了我的問題。 – MoiD101

相關問題