試圖在我的報告中顯示NULL
爲zero
。在SQL Server中顯示爲0
ISNULL(course, ' ')
- 這個工程並顯示空細胞作爲空白ISNULL(count(course), '')
- 這不起作用
我試圖ISNothing
使用SQL Server 12,顯示計數爲零,它沒有。
有什麼建議嗎?
感謝
試圖在我的報告中顯示NULL
爲zero
。在SQL Server中顯示爲0
ISNULL(course, ' ')
- 這個工程並顯示空細胞作爲空白ISNULL(count(course), '')
- 這不起作用我試圖ISNothing
使用SQL Server 12,顯示計數爲零,它沒有。
有什麼建議嗎?
感謝
你的代碼是......
SELECT college,
department,
COUNT(department),
MONTH([date]),
CASE
WHEN DATEPART(MONTH, [date]) = 1 THEN
ISNULL(COUNT(department),
' ')
END AS 'January',
CASE
WHEN DATEPART(MONTH, [date]) = 2 THEN
ISNULL(COUNT(department),
' ')
END AS 'February'
FROM rpt_school
GROUP BY college,
department,
MONTH([date]);
如果你的目的是讓學校的名單,各高校內部門,記錄各高校內各部門的數量,的記錄January
或February
每個學院內各部門的數量,那麼我建議以下...
若y如有任何問題或意見,請隨時發表評論。
附錄
要獲得每個college
內college
S,department
個清單,並與由month
各高校內各部門相關date
小號計數,請嘗試以下...
SELECT collegeDepartments.college AS college,
collegeDepartments.department AS department,
COUNT(CASE
WHEN monthNumber = 1 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'January',
COUNT(CASE
WHEN monthNumber = 2 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'February',
COUNT(CASE
WHEN monthNumber = 3 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'March',
COUNT(CASE
WHEN monthNumber = 4 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'April',
COUNT(CASE
WHEN monthNumber = 5 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'May',
COUNT(CASE
WHEN monthNumber = 6 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'June',
COUNT(CASE
WHEN monthNumber = 7 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'July',
COUNT(CASE
WHEN monthNumber = 8 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'August',
COUNT(CASE
WHEN monthNumber = 9 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'September',
COUNT(CASE
WHEN monthNumber = 10 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'October',
COUNT(CASE
WHEN monthNumber = 11 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'November',
COUNT(CASE
WHEN monthNumber = 12 THEN
COALESCE(countOfMonthNumber, 0)
ELSE
0
END
) AS 'December'
FROM (SELECT college AS college,
department AS department,
monthNumber AS monthNumber
FROM (SELECT college AS college,
department AS department
FROM rpt_school
GROUP BY college,
department
) AS collegeDepartments,
(SELECT 1 AS monthNumber
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12
) AS monthNumbers
) AS collegeDepartmentsMonthNumbers
LEFT JOIN (SELECT college AS college,
department AS department,
DATEPART(MONTH, rpt_school.[date]) AS monthNumber,
COUNT(*) AS countOfMonthNumber
FROM rpt_school
GROUP BY college,
department,
monthNumber
) AS countOfMonthPartFinder ON collegeDepartmentsMonthNumbers.college = countOfMonthPartFinder.college
AND collegeDepartmentsMonthNumbers.department = countOfMonthPartFinder.department
AND collegeDepartmentsMonthNumbers.monthNumber = countOfMonthPartFinder.monthNumber
ORDER BY college,
department,
collegeDepartmentsMonthNumbers.monthNumber;
此聲明從以下子查詢開始...
SELECT college AS college,
department AS department
FROM rpt_school
GROUP BY college,
department
此子查詢從rpt_school
編譯college
和department
的唯一組合列表。現在執行此分組而不是稍後會消除大量加入,並應使總體說明更有效。
一個CROSS JOIN
然後在此子查詢的結果和下面的子查詢的執行...
SELECT 1 AS monthNumber
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12
此子查詢創建每月數的列表。執行的CROSS JOIN
具有將每個月號碼的列表附加到每個唯一組合college
和department
的效果。這CROSS JOIN
編集的字段,然後通過選擇每個字段FO數據集返回到主聲明,按照下面的...
SELECT college AS college,
department AS department,
monthNumber AS monthNumber
FROM (SELECT college AS college,
department AS department
FROM rpt_school
GROUP BY college,
department
) AS collegeDepartments,
(SELECT 1 AS monthNumber
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12
) AS monthNumbers
請注意,一個表CROSS JOIN
兩者之間的語法是tblTable1, tblTable2
。
的CROSS JOIN
ED數據集然後LEFT JOIN
編到發現的dates
每個college
內屬於特定month
每個department
計數子查詢,按照以下...
JOIN (SELECT college AS college,
department AS department,
DATEPART(MONTH, rpt_school.[date]) AS monthNumber,
COUNT(*) AS countOfMonthNumber
FROM rpt_school
GROUP BY college,
department,
monthNumber
) AS countOfMonthPartFinder ON collegeDepartmentsMonthNumbers.college = countOfMonthPartFinder.college
AND collegeDepartmentsMonthNumbers.department = countOfMonthPartFinder.department
AND collegeDepartmentsMonthNumbers.monthNumber = countOfMonthPartFinder.monthNumber
的college
和然後選擇我們現在最終加入的數據集中的department
和一系列CASE
語句,它們將返回當前月份記錄的計數或適當的零。
也可以這樣做:
select case when course is null then '0' else course end
它接受甚至文本。
因爲如果它只是將空值更改爲0,您得到一個錯誤,轉換失敗.....數據類型爲int。我只是想,如果你的課程領域是varchar數據類型 –
是的,數據類型是varchar –
那麼你可以使用單引號'0',以避免錯誤。 –
在樣本數據,我們有兩個空值,其計顯示爲2
Declare @Course TABLE (Course VARCHAR(50))
Insert INTO @Course
SELECT 'ABC' Union all
SELECT NUll Union all
SELECT 'ABC' Union all
SELECT NUll Union all
SELECT 'dfg'
SELECT Course,COUNT(ISNULL(NULLIF(Course, NULL) ,'0'))AS CourseCount
FROM @Course
Group by Course
輸出
Rno |Course |CourseCount
------------------------
1 |NULL |2
2 |ABC |2
3 |dfg |1
我想我有一個新問題。 –
我每個月都有一行導致報告運行很長。例如 –
如何將三個行合併爲一個? 學院,系,一月,二月,三月 藝術-------音樂------- 1 \t ------- 0 ------- 0 \t Art- ------音樂------- 0 ------- 2 ------- 0 藝術-------音樂------- 0 - ------ 0 ------- 3 使用此代碼:toonice提供了嗎? –
請包括整個查詢。 –
爲了提高您的問題的質量,請向我們展示您正在使用的整個查詢並優先考慮表格的結構以及一些示例數據。另外,請根據您的樣本數據向我們展示所需的輸出。 – toonice
如果你沒有得到count(column)的任何結果,很可能是因爲你的查詢如下所示:'SELECT COUNT(column)FROM mytable WHERE somecolumn = somecondition GROUP BY somecolumn;'並且沒有記錄somecolumn =一些條件。在這種情況下,您的計數_is_ 0,但您根本不會得到結果集(這是關於WHERE和GROUP BY與COUNT一起工作的方式)。一種解決方案是將整個查詢放入isnull語句中。例如'SELECT ISNULL((SELECT COUNT(column)FROM myTable ...),0)' – ZLK