使用多个枢轴添加零代替null

时间:2018-08-03 12:29:39

标签: sql sql-server-2008 sql-server-2012 pivot-table isnull

我已经使用两个数据透视表开发了一个查询,现在我想添加一个零,其中每个单元格的值返回为null。有办法吗?

我当前正在处理的代码如下。

  ;WITH ToPivot AS ( SELECT time_tracker.date, Users.FirstName + ' ' + Users.LastName AS username, (CASE WHEN ((datepart(hour, chk_in)) >= 12 OR (datepart(hour, chk_out)) < 16) THEN 0.5 ELSE 1 END) AS late, TypeOfLeaves.leave_type, Userleavetyp.no_of_days FROM Users
        INNER JOIN time_tracker ON Users.ID = time_tracker.fk_userid
        INNER JOIN Userleavetyp ON Users.ID = Userleavetyp.fk_user 
        INNER JOIN TypeOfLeaves ON Userleavetyp.fk_tol = TypeOfLeaves.ID 
        WHERE (Users.FK_Status = 1)), 
    LateTotals AS (SELECT T.username, 
    Total = SUM(late),T.leave_type,T.no_of_days FROM ToPivot AS T
     WHERE T.date BETWEEN '2018-" + monthvalue + "-01' AND '2018-" + monthvalue + "-31' GROUP BY T.username,T.leave_type,T.no_of_days)SELECT distinct(pv2.username) as original ,PV2.*,L.Total FROM ToPivot AS P PIVOT (SUM(late) 
         FOR date IN (\"2018-" + monthvalue
         + "-01\", \"2018-" + 
        monthvalue + "-02\", \"2018-" +
         monthvalue + "-03\", \"2018-" +
         monthvalue + "-04\", \"2018-" +
         monthvalue + "-05\", \"2018-" +
         monthvalue + "-06\", \"2018-" + 
        monthvalue + "-07\", \"2018-" +
         monthvalue + "-08\", \"2018-" +
         monthvalue + "-09\", \"2018-" +
         monthvalue + "-10\", \"2018-" + 
        monthvalue + "-11\", \"2018-" +
         monthvalue + "-12\", \"2018-" +
         monthvalue + "-13\", \"2018-" + 
        monthvalue + "-14\", \"2018-" +
         monthvalue + "-15\", \"2018-" +
         monthvalue + "-16\", \"2018-" + 
        monthvalue + "-17\", \"2018-" + 
        monthvalue + "-18\", \"2018-" + 
        monthvalue + "-19\", \"2018-" +
         monthvalue + "-20\", \"2018-" +
         monthvalue + "-21\", \"2018-" + 
        monthvalue + "-22\", \"2018-" + 
        monthvalue + "-23\", \"2018-" + 
        monthvalue + "-24\", \"2018-" + 
        monthvalue + "-25\", \"2018-" +
         monthvalue + "-26\", \"2018-" +
         monthvalue + "-27\", \"2018-" + 
        monthvalue + "-28\", \"2018-" + 
        monthvalue + "-29\", \"2018-" + monthvalue + "-30\", \"2018-" + monthvalue + "-31\"))
         AS pv1 PIVOT (SUM(no_of_days) FOR leave_type IN ([Casual Leave], [Paid Leave], [Complimentary Leave])) AS pv2 LEFT JOIN LateTotals AS L ON L.username = pv2.username

我的输出如下:

https://i.stack.imgur.com/TxFUJ.png

1 个答案:

答案 0 :(得分:0)

在选择列时仅使用isull:

;WITH ToPivot AS ( SELECT time_tracker.date, Users.FirstName + ' ' + Users.LastName AS username, (CASE WHEN ((datepart(hour, chk_in)) >= 12 OR (datepart(hour, chk_out)) < 16) THEN 0.5 ELSE 1 END) AS late, TypeOfLeaves.leave_type, Userleavetyp.no_of_days FROM Users
        INNER JOIN time_tracker ON Users.ID = time_tracker.fk_userid
        INNER JOIN Userleavetyp ON Users.ID = Userleavetyp.fk_user 
        INNER JOIN TypeOfLeaves ON Userleavetyp.fk_tol = TypeOfLeaves.ID 
        WHERE (Users.FK_Status = 1)), 
    LateTotals AS (SELECT T.username, 
    Total = SUM(late),T.leave_type,T.no_of_days FROM ToPivot AS T
     WHERE T.date BETWEEN '2018-" + monthvalue + "-01' AND '2018-" + monthvalue + "-31' GROUP BY T.username,T.leave_type,T.no_of_days)SELECT distinct(pv2.username) as original ,PV2.*,L.Total FROM ToPivot AS P PIVOT (SUM(late) 
         FOR date IN (\"2018-" + monthvalue
         + "-01\", \"2018-" + 
        monthvalue + "-02\", \"2018-" +
         monthvalue + "-03\", \"2018-" +
         monthvalue + "-04\", \"2018-" +
         monthvalue + "-05\", \"2018-" +
         monthvalue + "-06\", \"2018-" + 
        monthvalue + "-07\", \"2018-" +
         monthvalue + "-08\", \"2018-" +
         monthvalue + "-09\", \"2018-" +
         monthvalue + "-10\", \"2018-" + 
        monthvalue + "-11\", \"2018-" +
         monthvalue + "-12\", \"2018-" +
         monthvalue + "-13\", \"2018-" + 
        monthvalue + "-14\", \"2018-" +
         monthvalue + "-15\", \"2018-" +
         monthvalue + "-16\", \"2018-" + 
        monthvalue + "-17\", \"2018-" + 
        monthvalue + "-18\", \"2018-" + 
        monthvalue + "-19\", \"2018-" +
         monthvalue + "-20\", \"2018-" +
         monthvalue + "-21\", \"2018-" + 
        monthvalue + "-22\", \"2018-" + 
        monthvalue + "-23\", \"2018-" + 
        monthvalue + "-24\", \"2018-" + 
        monthvalue + "-25\", \"2018-" +
         monthvalue + "-26\", \"2018-" +
         monthvalue + "-27\", \"2018-" + 
        monthvalue + "-28\", \"2018-" + 
        monthvalue + "-29\", \"2018-" + monthvalue + "-30\", \"2018-" + monthvalue + "-31\"))
         AS pv1 PIVOT (SUM(no_of_days) FOR leave_type IN ([Casual Leave], [Paid Leave], [Complimentary Leave])) AS pv2 LEFT JOIN LateTotals AS L ON L.username = pv2.username