SQL表也包含0个值行

时间:2018-03-08 16:52:06

标签: sql

我有几个表,tblUser和tblTableUsage,用于跟踪谁在哪个表/类上保存了数据。

tblUser: UserID, UserName, Department
tblTableUsage: UserID, ClassName, DateUsed


DECLARE @ThisDate DATETIME
SET @ThisDate = '2018-03-05 00:00:00.000'

SELECT 
  DISTINCT(tu.ClassName)
FROM [tblTableUsage] tu
INNER JOIN tblUser u on tu.[UserID] = u.[UserID]
WHERE DATEADD(Day,0,datediff(day, 0, tu.SaveDate)) = @ThisDate
AND u.Department = 'DD'
GROUP BY tu.ClassName, DATEADD(DAY, 0, datediff(Day, 0, tu.SaveDate))
ORDER BY ClassName

这为我提供了“DD'”所使用的所有ClassName项目的唯一列表。部门为' 3/5/2018'

SELECT 
  u.UserName,  
  tu.ClassName, 
  COUNT(tu.[UserID]) as 'Items Worked On',  
  DATEADD(DAY, 0, datediff(Day, 0, tu.SaveDate)) as 'Date'
FROM [tblTableUsage] tu
INNER JOIN [tblUsers] u on tu.[UserID] = u.UserID
WHERE DATEADD(Day,0,datediff(day, 0, tu.SaveDate)) = @ThisDate and u.Department = 'DD'
GROUP BY UserName,  tu.ClassName, DATEADD(DAY, 0, datediff(Day, 0, tu.SaveDate))
ORDER BY DATEADD(DAY, 0, datediff(Day, 0, tu.SaveDate)),ClassName, u.UserName

这为我提供了所有用户,使用的类和次数。但是我有ClassName条目,只有1个用户使用过它。

USERNAME        CLASSNAME       ITEMS WORKED ON         DATE
User A                   CLASS 1                 3                              3/5/2018
User B                   CLASS 1                 12                           3/5/2018
User C                   CLASS 2                 12                           3/5/2018
User A                   CLASS 3                 2                              3/5/2018
User B                   CLASS 3                 12                           3/5/2018
User C                   CLASS 3                 3                              3/5/2018

我想看到的是项目工作的条目= 0。

User A                   CLASS 1                 3                              3/5/2018
User B                   CLASS 1                 12                           3/5/2018
User C                   CLASS 1                 0                              3/5/2018                              <--THIS ONE
User A                   CLASS 2                 0                              3/5/2018                              <--THIS ONE
User B                   CLASS 2                 0                              3/5/2018                              <--THIS ONE
User C                   CLASS 2                 12                           3/5/2018
User A                   CLASS 3                 2                              3/5/2018
User B                   CLASS 3                 12                           3/5/2018
User C                   CLASS 3                 3                              3/5/2018

有关如何获得这个的任何想法?谢谢!

我希望根据这些数据构建报告,如果存在0个数据行条目则会更容易,所以我不必手动添加它们...

2 个答案:

答案 0 :(得分:1)

使用cross join生成所有用户和类,然后加入并聚合:

SELECT u.UserName, c.ClassName, 
       COUNT(tu.[UserID]) as items_worked_on  
       CAST(tu.SaveDate as Date) as dte
FROM (SELECT u.*
      FROM tblusers u
      WHERE  u.Department = 'DD'
     ) u CROSS JOIN
     (SELECT DISTINCT tu.ClassName 
      FROM tblTableUsage tu
      WHERE CAST(tu.SaveDate as DATE) = @ThisDate
     ) c LEFT JOIN
     tblTableUsage tu
     ON CAST(tu.SaveDate as DATE) = @ThisDate AND tu.UserId = u.UserId and
        tu.ClassName = c.ClassName
INNER JOIN [tblUsers] u on tu.[UserID] = u.UserID
WHERE DATEADD(Day,0,datediff(day, 0, tu.SaveDate)) = @ThisDate and u.Department = 'DD'
GROUP BY u.UserName, cu.ClassName, CAST(tu.SaveDate as Date)
ORDER BY dte, ClassName, u.UserName;

答案 1 :(得分:0)

声明@ThisDate datetime

设置@ThisDate ='2018-03-05 00:00:00.000'

选择tmpU.UserName,tmpTU.ClassName,ISNULL(COUNT(tu.ClassName),0)作为'工作项目'

            SELECT u.UserName, u.Department , u.UserID

            FROM tblUsers u

            WHERE  u.Department = 'Due Diligence'

)tmpU

CROSS JOIN

            SELECT DISTINCT tu.ClassName

            FROM tblTableUsage tu

            WHERE CAST(tu.SaveDate as DATE) = @ThisDate

            AND tu.[User] IN

            (

                            SELECT u.UserID

                            FROM tblUsers u

                            WHERE u.Department = 'Due Diligence'

            )

)tmpTU

LEFT JOIN tblTableUsage tu ON DATEADD(Day,0,datediff(day,0,tu.SaveDate))= @ThisDate AND tu。[User] = tmpU.UserId and tu.ClassName = tmpTU.ClassName

GROUP BY tmpU.UserName,tmpTU.ClassName

ORDER BY tmpTU.ClassName,tmpU.UserName