繁体   English   中英

SQL Server:两个表联接可显示每天没有数据的所有行

[英]SQL Server: Two table join show all rows where there's no data per day

我正在尝试创建一个报表查询,以显示给定日期范围,所有名称,日期以及是否有数据。 我真的很接近,但是我对如何在数据中获得最终的零值一无所知。 所以这就是我想要的:

 Name   ID     Mth Day Count
 Auburn  7261   10  14  0
 Auburn  7261   10  15  0
 Auburn  7261   10  16  0
 Auburn  7261   10  17  0
 Auburn  7261   10  18  0
 Concord 7262   10  14  2
 Concord 7262   10  15  0
 Concord 7262   10  15  0
 Concord 7262   10  17  1
 Katey   7263   10  14  0
 Katey   7263   10  15  0
 Katey   7263   10  16  0
 Katey   7263   10  17  0
 Katey   7263   10  18  0

相反,我得到:

 Name   ID     Mth Day Count
 Auburn  7261   10  14  0
 Auburn  7261   10  15  0
 Auburn  7261   10  16  0
 Auburn  7261   10  17  0
 Auburn  7261   10  18  0
 Concord 7262   10  14  2
 Concord 7262   10  17  1
 Katey   7263   10  14  0
 Katey   7263   10  15  0
 Katey   7263   10  16  0
 Katey   7263   10  17  0
 Katey   7263   10  18  0

这是我的查询:

 SELECT PUE.Name as [Name], pue.EventID,
 MONTH(dates.Date) AS Month, DAY(dates.Date) AS Day,
 puc.idcount AS PicturesTaken
 from (
 select name, eventid from Events where 
 TourID = 444 and EventID > 7256 and EventID < 7323
 ) PUE
 outer apply (
SELECT
    Date = DateAdd( Day, n1.number * 10 + n0.number, '2014-10-14' )
FROM
    (SELECT 1 AS number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) as n0
CROSS JOIN
    (SELECT 1 AS number UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) as n1
WHERE   DateAdd( Day, n1.number * 10 + n0.number, '2014-10-14' ) BETWEEN '2014-10-14' AND '2014-10-18'
 ) dates
 left outer join (
 select eventid, convert(date,picturetakendate) as picdate, count(consumerdataid) as idcount from ConsumerData
 where EventID > 7256 and EventID < 7323 
 group by eventid, convert(date,picturetakendate)
 ) puc
 on pue.EventID = puc.EventID 
 where puc.picdate = dates.Date or puc.idcount is NULL
 order by pue.eventid, MONTH(dates.Date), DAY(dates.Date)

我知道为什么名称没有显示零,这是外部联接表的结果:

 7262   2014-10-14  2
 7262   2014-10-17  1
 7265   2014-10-14  2
 7266   2014-10-14  2

因此,在where子句中它不为null或不匹配日期是有意义的。 但是,如果我取出where子句,则每个日期的每个计数都会得到一行,即:

 Concord    7262    10  14  2
 Concord    7262    10  14  1
 Concord    7262    10  15  1
 Concord    7262    10  15  2
 Concord    7262    10  16  2
 Concord    7262    10  16  1
 Concord    7262    10  17  1
 Concord    7262    10  17  2
 Concord    7262    10  18  2
 Concord    7262    10  18  1

我是如此亲密,我知道我只是缺少一些简单的东西,但是每次尝试修复它实际上都会使结果变得更糟,所以我在这里寻求帮助。 我认为我只需要在外部联接中修复查询以某种方式显示每个日期,如果我可以引用日期,则应用日期会更容易或更改位置。 但是我一直不知道怎么做。

在SELECT子句中添加ISNULL条件

 SELECT PUE.Name as [Name], pue.EventID,
 MONTH(dates.Date) AS Month, DAY(dates.Date) AS Day,
 ISNULL(puc.idcount,0) AS PicturesTaken

将where子句转换为ON

on pue.EventID = puc.EventID 
AND puc.picdate = dates.Date 

代替

where puc.picdate = dates.Date or puc.idcount is NULL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM