我正在尝试获取一个记录集,其中包含过去24小时内每小时的销售量,并用零填充空余时间。
我的示例数据集如下所示,并包含两个测试行:
id | saleID | amount | created
---|-------------------|-------------|-------------
1 | 6032b317-1533... | 20000 | 2019-01-10 23:56:40
2 | 43556fg6-5344... | 60000 | 2019-01-11 18:06:32
例如,当前时间是18:10,所以我要查找的结果是:
| hourOrderID | saleHour | saleHourTotal |
| ----------- | -------- | ------------- |
| 1 | 18:00 | 1 |
| 2 | 17:00 | 0 |
| 3 | 16:00 | 0 |
| 4 | 15:00 | 0 |
| 5 | 14:00 | 0 |
| 6 | 13:00 | 0 |
| 7 | 12:00 | 0 |
| 8 | 11:00 | 0 |
| 9 | 10:00 | 0 |
| 10 | 9:00 | 0 |
| 11 | 8:00 | 0 |
| 12 | 7:00 | 0 |
| 13 | 6:00 | 0 |
| 14 | 5:00 | 0 |
| 15 | 4:00 | 0 |
| 16 | 3:00 | 0 |
| 17 | 2:00 | 0 |
| 18 | 1:00 | 0 |
| 19 | 0:00 | 0 |
| 20 | 23:00 | 1 |
| 21 | 22:00 | 0 |
| 22 | 21:00 | 0 |
| 23 | 20:00 | 0 |
| 24 | 19:00 | 0 |
| 25 | 18:00 | 0 |
请注意;今天18:00-19:00之间有1笔交易,昨天18:00-19:00之间有0笔交易。这是我的问题。
我实际上得到的结果是:
| hourOrderID | saleHour | saleHourTotal |
| ----------- | -------- | ------------- |
| 1 | 18:00 | 1 |
| 2 | 17:00 | 0 |
| 3 | 16:00 | 0 |
| 4 | 15:00 | 0 |
| 5 | 14:00 | 0 |
| 6 | 13:00 | 0 |
| 7 | 12:00 | 0 |
| 8 | 11:00 | 0 |
| 9 | 10:00 | 0 |
| 10 | 9:00 | 0 |
| 11 | 8:00 | 0 |
| 12 | 7:00 | 0 |
| 13 | 6:00 | 0 |
| 14 | 5:00 | 0 |
| 15 | 4:00 | 0 |
| 16 | 3:00 | 0 |
| 17 | 2:00 | 0 |
| 18 | 1:00 | 0 |
| 19 | 0:00 | 0 |
| 20 | 23:00 | 1 |
| 21 | 22:00 | 0 |
| 22 | 21:00 | 0 |
| 23 | 20:00 | 0 |
| 24 | 19:00 | 0 |
| 25 | 18:00 | 1 | <----- this isn't yesterday, it's today
我一直在处理的查询是:
SELECT
aux.id AS hourOrderID,
CONCAT(aux.dh, ':00') AS saleHour,
COALESCE(COUNT(ets.saleID), 0) AS saleHourTotal
FROM eventTicketSales AS ets
RIGHT JOIN (
SELECT 1 AS id, HOUR(UTC_TIMESTAMP()) AS dh UNION
SELECT 2 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR)) AS dh UNION
SELECT 3 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 2 HOUR)) AS dh UNION
SELECT 4 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 3 HOUR)) AS dh UNION
SELECT 5 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 4 HOUR)) AS dh UNION
SELECT 6 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 5 HOUR)) AS dh UNION
SELECT 7 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 6 HOUR)) AS dh UNION
SELECT 8 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 7 HOUR)) AS dh UNION
SELECT 9 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 8 HOUR)) AS dh UNION
SELECT 10 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 9 HOUR)) AS dh UNION
SELECT 11 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 10 HOUR)) AS dh UNION
SELECT 12 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 11 HOUR)) AS dh UNION
SELECT 13 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 12 HOUR)) AS dh UNION
SELECT 14 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 13 HOUR)) AS dh UNION
SELECT 15 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 14 HOUR)) AS dh UNION
SELECT 16 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 15 HOUR)) AS dh UNION
SELECT 17 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 16 HOUR)) AS dh UNION
SELECT 18 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 17 HOUR)) AS dh UNION
SELECT 19 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 18 HOUR)) AS dh UNION
SELECT 20 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 19 HOUR)) AS dh UNION
SELECT 21 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 20 HOUR)) AS dh UNION
SELECT 22 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 21 HOUR)) AS dh UNION
SELECT 23 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 22 HOUR)) AS dh UNION
SELECT 24 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 23 HOUR)) AS dh UNION
SELECT 25 AS id, HOUR(DATE_SUB(UTC_TIMESTAMP(), INTERVAL 24 HOUR)) AS dh
) AS aux ON HOUR(ets.created) = aux.dh
AND ets.created > DATE_SUB(UTC_TIMESTAMP, INTERVAL 24 HOUR)
GROUP BY aux.id, aux.dh
ORDER BY hourOrderID
我想要这样,所以集合中的最后一个小时是前一天,但是我的大脑决定它不能再走了,需要协助。
我尝试在第25小时添加另一个UNION
,并尝试将时间范围延长到25小时,但结果与我想要的相去甚远:
AND ets.created > DATE_SUB(UTC_TIMESTAMP, INTERVAL 25 HOUR
请提出可以进行哪些更改以使其按预期工作。
答案 0 :(得分:1)
我在注释部分采纳了@Strawberry的建议,并决定执行此应用程序级别,这现在很有意义,因为它更易于阅读。我不后悔尝试使用SQL版本,因为我学到了一点。
我在下面发布了我使用的解决方案,该解决方案在PHP中适用于需要类似功能的任何人。如果有人有比这更有效的解决方案,请分享评论。
查询:
SELECT
CONCAT(DATE_FORMAT(created, '%Y-%m-%d %H'), ':00:00') AS saleHour,
SUM(amount) AS totalSales
FROM eventTicketSales
WHERE created BETWEEN DATE_SUB(UTC_TIMESTAMP(), INTERVAL 24 HOUR) AND UTC_TIMESTAMP
GROUP BY HOUR(created);
结果:
saleHour totalSales
2019-01-11 17:00:00 10000
2019-01-10 23:00:00 20000
存储在$salesIn24Hours
逻辑:
$hours = 24 + 1;
for ($i=0; $i<$hours; $i++) {
$date = date('Y-m-d H', strtotime('-'.$i.' HOUR')).':00:00';
$key = array_search($date, array_column($salesIn24Hours, 'saleHour'));
if ($key !== FALSE) {
echo $date . ' ' . $salesIn24Hours[$key]['totalSales'] . '<br>';
} else {
echo $date . ' 0' . '<br>';
}
}
结果:
2019-01-11 21:00:00 0
2019-01-11 20:00:00 0
2019-01-11 19:00:00 0
2019-01-11 18:00:00 0
2019-01-11 17:00:00 10000
2019-01-11 16:00:00 0
2019-01-11 15:00:00 0
2019-01-11 14:00:00 0
2019-01-11 13:00:00 0
2019-01-11 12:00:00 0
2019-01-11 11:00:00 0
2019-01-11 10:00:00 0
2019-01-11 09:00:00 0
2019-01-11 08:00:00 0
2019-01-11 07:00:00 0
2019-01-11 06:00:00 0
2019-01-11 05:00:00 0
2019-01-11 04:00:00 0
2019-01-11 03:00:00 0
2019-01-11 02:00:00 0
2019-01-11 01:00:00 0
2019-01-11 00:00:00 0
2019-01-10 23:00:00 20000
2019-01-10 22:00:00 0
2019-01-10 21:00:00 0
答案 1 :(得分:1)
(出于语法目的,我假设您正在使用MySQL。)
为了从单个SELECT
语句中产生25行数据,您需要一个具有25行的表(真实或伪造),这正是内部SELECT
所做的。在显示实际构造内部表的更简单方法之前,我建议向其添加更多列(假设当前时间是12月31日下午6点):
| hourOrderID | saleHour | startTime | endTime |
| ----------- | -------- | ---------------- | ---------------- |
| 1 | 18:00 | 2019-01-10 18:00 | 2019-01-10 19:00 |
| 2 | 17:00 | 2019-01-10 17:00 | 2019-01-10 18:00 |
| 3 | 16:00 | 2019-01-10 16:00 | 2019-01-10 17:00 |
| 4 | 15:00 | 2019-01-10 15:00 | 2019-01-10 16:00 |
| 5 | 14:00 | 2019-01-10 14:00 | 2019-01-10 15:00 |
| 6 | 13:00 | 2019-01-10 13:00 | 2019-01-10 14:00 |
| 7 | 12:00 | 2019-01-10 12:00 | 2019-01-10 13:00 |
| 8 | 11:00 | 2019-01-10 11:00 | 2019-01-10 12:00 |
| 9 | 10:00 | 2019-01-10 10:00 | 2019-01-10 11:00 |
| 10 | 9:00 | 2019-01-10 09:00 | 2019-01-10 10:00 |
| 11 | 8:00 | 2019-01-10 08:00 | 2019-01-10 09:00 |
| 12 | 7:00 | 2019-01-10 07:00 | 2019-01-10 08:00 |
| 13 | 6:00 | 2019-01-10 06:00 | 2019-01-10 07:00 |
| 14 | 5:00 | 2019-01-10 05:00 | 2019-01-10 06:00 |
| 15 | 4:00 | 2019-01-10 04:00 | 2019-01-10 05:00 |
| 16 | 3:00 | 2019-01-10 03:00 | 2019-01-10 04:00 |
| 17 | 2:00 | 2019-01-10 02:00 | 2019-01-10 03:00 |
| 18 | 1:00 | 2019-01-10 01:00 | 2019-01-10 02:00 |
| 19 | 0:00 | 2019-01-10 00:00 | 2019-01-10 01:00 |
| 20 | 23:00 | 2019-01-09 23:00 | 2019-01-10 00:00 |
| 21 | 22:00 | 2019-01-09 22:00 | 2019-01-09 23:00 |
| 22 | 21:00 | 2019-01-09 21:00 | 2019-01-09 22:00 |
| 23 | 20:00 | 2019-01-09 20:00 | 2019-01-09 21:00 |
| 24 | 19:00 | 2019-01-09 19:00 | 2019-01-09 20:00 |
| 25 | 18:00 | 2019-01-09 18:00 | 2019-01-09 19:00 |
然后您的查询将非常简单:
SELECT
aux.hourOrderID,
aux.saleHour,
COALESCE(COUNT(ets.saleID), 0) AS saleHourTotal
FROM madeUpTable AS aux LEFT JOIN eventTicketSales AS ets
ON aux.startTime <= ets.created AND ets.created < aux.endTime
GROUP BY aux.hourOrderID
ORDER BY hourOrderID
事实证明,您可以使用通用表表达式(https://dev.mysql.com/doc/refman/8.0/en/with.html)来做到这一点:
WITH RECURSIVE aux AS
(
SELECT 1 AS hourOrderId,
HOUR(UTC_TIMESTAMP()) AS saleHour,
DATE_ADD(
DATE_FORMAT(UTC_TIMESTAMP(), "%Y-%m-%d %H:00:00"),
INTERVAL 0 HOUR
) AS startDate,
DATE_ADD(
DATE_FORMAT(UTC_TIMESTAMP(), "%Y-%m-%d %H:00:00"),
INTERVAL 1 HOUR
) AS endDate
UNION ALL
SELECT n + 1,
HOUR(DATE_SUB(startDate, INTERVAL 1 HOUR)),
DATE_SUB(startDate, INTERVAL 1 HOUR),
DATE_SUB(startDate, INTERVAL 1 HOUR)
FROM aux
WHERE n < 25
)
SELECT
aux.hourOrderID,
aux.saleHour,
COALESCE(COUNT(ets.saleID), 0) AS saleHourTotal
FROM madeUpTable AS aux LEFT JOIN eventTicketSales AS ets
ON aux.startTime <= ets.created AND ets.created < aux.endTime
GROUP BY aux.hourOrderID
ORDER BY hourOrderID
我个人试图限制这种技术在涉及其他人的代码上的使用,因为这不是大多数人在野外看到的东西,但是对于SQL来说,这对我来说更容易理解,因为查询本身很简单。恕我直言,其中最复杂的部分实际上是将当前时间四舍五入到最近的小时,同时保留日期部分。