如何找到丢失的小时并将数据显示为0
该查询每天每天都有数量的数量,但留下了没有计数的任何天和小时。如何填写缺失的日期和在计数中选择0的日期之间的数小时?
SELECT CAST(RecordTime AS date) AS Date, DATENAME(dw, RecordTime) AS [Day of the week], DATEPART(hour, RecordTime) AS [Hour of the day], COUNT(*) AS [Hourly Count]
FROM Counts
WHERE (RecordTime >= CONVERT(DATETIME, '2022-04-01 00:00:00', 102)) AND (RecordTime < CONVERT(DATETIME, '2022-05-01 00:00:00', 102)) AND (MachineNum = 11) AND (Cavity = 1)
GROUP BY CAST(RecordTime AS date), DATEPART(hour, RecordTime), DATENAME(dw, RecordTime)
ORDER BY Date, [Hour of the day]
This query pulls counts for every hour of every day but leaves off any days and hours that don't have counts. How can I fill in the missing dates and hours between the dates selected with 0 for the counts?
SELECT CAST(RecordTime AS date) AS Date, DATENAME(dw, RecordTime) AS [Day of the week], DATEPART(hour, RecordTime) AS [Hour of the day], COUNT(*) AS [Hourly Count]
FROM Counts
WHERE (RecordTime >= CONVERT(DATETIME, '2022-04-01 00:00:00', 102)) AND (RecordTime < CONVERT(DATETIME, '2022-05-01 00:00:00', 102)) AND (MachineNum = 11) AND (Cavity = 1)
GROUP BY CAST(RecordTime AS date), DATEPART(hour, RecordTime), DATENAME(dw, RecordTime)
ORDER BY Date, [Hour of the day]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要有一个参考表,该参考表存储所有日期和各个小时。这将是一个日历表,并添加一个小时。
步骤1:
创建餐桌小时。
步骤2:
创建一个将具有所需的日期和小时字段的表。
注意:使用计数表来得出日期和小时字段是一个差的解决方案,因为它可能是交易表,并且具有大量记录。而是使用日历表。
最终步骤:
使用步骤2 中创建的表作为主表(或用作子查询),
左JOIN
COUNTS
根据日期使用它和小时。以下查询应为您提供所需的输出。
SQL小提琴:在这里尝试
You need to have a reference table which stores all the dates and respective hour. It will be kind of a Calendar table with hour added to it.
Step 1:
Create table hours.
Step 2:
Create a table which will have the required date and hour field.
Note: Using Counts table to derive the date and hour field is a poor solution, As it might be a transactional table and have huge number of records. Instead use a Calendar table.
Final Step:
Use the table created in Step 2 as the main table(or use as subquery) and
left join
Counts
with it based on Date and hour.Below query should give you the desired output.
SQL Fiddle: Try it here
正如Larnu建议的那样,您需要生成一个全天+小时组合的完整数据集,以便将其左JON加入。通过我的计算,您需要30天 * 24小时= 720行。如果您还没有数字表或日历表或a 序列生成函数,您可以使用递归CTES生成此功能,如下所示:
输出:
”更轻松。这是一个简单的示例,仅包含1,000行作为您期望的最大日期,并使用递归CTE-最初有多种填充数字表的方法,并且在那里的性能并不重要。
现在,查询将所有日期都在范围内获取:
然后,您可以将其用作核心数据集,以便像上面的示例一样。
As Larnu suggested, you need to generate a full dataset with all day+hour combinations in the range, in order to left join to. By my calculation you need 30 days * 24 hours = 720 rows. If you don't already have a numbers table, or a calendar table, or a sequence generating function, you can generate this using recursive CTEs as follows:
Output:
Now, we just need to left outer join that against your existing table:
If you have a numbers table, the generation of dates is a little easier. This is a simple example and only contains 1,000 rows as the largest range of dates you expect, and uses a recursive CTE - there are multiple ways to populate a numbers table initially and the performance there is not important.
Now the query to get all the dates in the range:
You can then use that as the core dataset to left join to just like in the above examples.