2013-05-29 70 views
1

該存儲過程結果存儲在總共查詢的基礎上,如果總共沒有任何記錄 那麼結果就要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 
+0

您可以發佈爲每個表和所期望的結果一些數據?將'INNER JOIN'改爲'LEFT JOIN'可能就足夠了。 – Taryn

+0

LEFT JOIN嘗試過,但沒有得到任何結果 –

+0

我的建議是發佈一些樣本數據和期望的結果。或者更好的方式是用工作數據模型創建一個[sql小提琴](http://sqlfiddle.com)。 – Taryn

回答

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, 
    case when l.Result = 'Pass' 
    then l.TotalMarks 
    else 0 end AS Total 
     from UserInfo e 
LEFT JOIN ViewLeave l ON e.UserID = l.UserID 
    LEFT JOIN SubjectType s ON s.SubjectID = l.SubjectID  

       Where e.UserID = @UserD AND 
       [email protected] 
       GROUP BY s.SubjectName,l.UserID, e.UserName ,TotalDays 
      )ps 
      pivot 
      (
       SUM(Total) 
       FOR SubjectName IN ([Marks1],[Marks2],[Marks3]) 
      ) AS pvt   

     END 
1

嘗試這一個 -

CREATE PROCEDURE dbo.Procedure_Summary 

    @UserID INT , 
    @Year INT  

AS BEGIN 

    SELECT 
      UserID 
     , UserName 
     , [English] = [Marks1] 
     , [Maths] = [Marks2] 
     , [Science] = [Marks3] 
    FROM 
    ( 
     SELECT 
       l.UserID 
      , e.UserName 
      , s.SubjectName 
      , Total = ISNULL(TotalMarks, 0) 
     FROM dbo.ViewLeave l 
     LEFT JOIN dbo.SubjectType s ON s.SubjectID = l.SubjectID 
     LEFT JOIN dbo.UserInfo e ON e.UserID = l.UserID 
     WHERE l.Result = 'Pass' 
      AND e.UserID = @UserID 
      AND l.[year] = @Year 
     GROUP BY 
       s.SubjectName 
      , l.UserID 
      , e.UserName 
      , TotalDays 
    ) ps 
    PIVOT 
    (
     SUM(Total) 
     FOR SubjectName In ([Marks1],[Marks2],[Marks3]) 
    ) pvt   

END 
+0

總共沒有得到0 –

+0

請提供您的表格的一些樣本數據。 – Devart

+0

檢查更新後的問題.. –

1

這是很難完全回答這個問題,因爲表的細節並不是很好。

你可以嘗試運行與要傳遞給看看是否有任何數據被返回的用戶你的內部查詢:

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 -- I am not sure why you have this group by 

我猜測的問題的一部分是要加入的方式。您正在使用內部聯接,這將要求每個表中都存在行,但用戶正在被UserInfo表過濾。所以我會提出以下更改。

更改您的連接來LEFT JOIN的,使第一表連接是UserInfo表,這樣的代碼將是:

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 UserInfo e 
     LEFT JOIN ViewLeave l 
      ON e.UserID = l.UserID 
      AND l.Result = 'Pass' 
      AND l.[year][email protected] 
     LEFT JOIN SubjectType s 
      ON s.SubjectID = l.SubjectID 
     WHERE e.UserID = @UserID 
    )ps 
    pivot 
    (
     SUM(Total) 
     FOR SubjectName IN ([Marks1],[Marks2],[Marks3]) 
    ) AS pvt   

END 
+0

通過檢查案件解決問題..看到答案 –

相關問題