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]=@Year)
        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
4

3 回答 3

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
于 2013-05-29T11:49:18.237 回答
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]=@Year)
GROUP BY s.SubjectName,l.UserID, e.UserName ,TotalDays  -- I am not sure why you have this group by

我猜问题的一部分是你加入的方式。您正在使用内部联接,这将要求每个表中都存在行,但用户正在被UserInfo表过滤。因此,我建议进行以下更改。

将您的 JOIN 更改为 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]=@Year
        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
于 2013-05-29T12:33:34.843 回答
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 
               l.year=@Year
                 GROUP BY s.SubjectName,l.UserID, e.UserName ,TotalDays
            )ps
            pivot
            (
                SUM(Total)
                FOR SubjectName IN ([Marks1],[Marks2],[Marks3])
            ) AS pvt           

        END
于 2013-05-30T09:27:10.397 回答