該存儲過程結果存儲在總共查詢的基礎上,如果總共沒有任何記錄 那麼結果就要0,如何管理空值。數據透視表在存儲過程中使用視圖管理空值
CREATE PROCEDURE dbo.Procedure_Summary
@UserID INT ,
@Year INT
AS
BEGIN
/* Procedure body */
SELECT UserID,
UserName ,
[Marks1] as 'English',
[Marks2] as 'Maths',
[Marks3] as 'Science'
FROM
(
SELECT l.UserID,
e.UserName ,
s.SubjectName,
TotalMarks AS Total
FROM ViewLeave l
INNER JOIN SubjectType s ON s.SubjectID = l.SubjectID
INNER JOIN UserInfo e ON e.UserID = l.UserID
WHERE ((l.Result = 'Pass')
AND (e.UserID = @UserID)
AND l.[year][email protected])
GROUP BY s.SubjectName,l.UserID, e.UserName ,TotalDays
)ps
pivot
(
SUM(Total)
FOR SubjectName IN ([Marks1],[Marks2],[Marks3])
) AS pvt
END
當前結果和預期的結果是因爲 -
Accoding to current stored procedure result are as ,
UserID UserName English Maths Science
105 ABC 25 0 36
106 XYZ 26 88 88
But i want if all three subject marks are 0 means total are zero and then show
as , also show non zero record
UserID UserName English Maths Science
109 QWE 0 0 0
您可以發佈爲每個表和所期望的結果一些數據?將'INNER JOIN'改爲'LEFT JOIN'可能就足夠了。 – Taryn
LEFT JOIN嘗試過,但沒有得到任何結果 –
我的建議是發佈一些樣本數據和期望的結果。或者更好的方式是用工作數據模型創建一個[sql小提琴](http://sqlfiddle.com)。 – Taryn