如何获取每天的计数()
Posted
技术标签:
【中文标题】如何获取每天的计数()【英文标题】:How to get the count() for each day 【发布时间】:2018-01-26 04:21:51 【问题描述】:我正在使用分析,我想获取每天的计数,并且它会在没有计数的情况下返回 0。我正在使用 mysql。
这是我的查询:
SELECT DAY(a.Date)
FROM (
SELECT LAST_DAY(CURDATE()) - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS DATE
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
) a
WHERE a.Date BETWEEN (CURDATE() - INTERVAL 31 DAY) AND CURDATE() GROUP BY a.Date;
我得到了获取 31 天天数的查询,但我无法获取每一天的 count()。
例子:
我有一个包含以下列的装运表:
id = 一些 id
status = '已发送'、'已交付'、'已完成'
date_dispatched = 某个日期
我想统计 31 天内所有处于已发送状态的数据。
预期输出示例:
+-----+--------+
| Day | Count |
+-----+--------+
| 25 | 0 |
| 26 | 0 |
| 27 | 0 |
| 28 | 0 |
| 29 | 12 |
| 30 | 24 |
| 31 | 34 |
| 1 | 24 |
| 2 | 67 |
| 3 | 0 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
| 8 | 0 |
| 9 | 0 |
| 10 | 0 |
| 11 | 12 |
| 12 | 44 |
| 13 | 67 |
| 14 | 0 |
| 15 | 0 |
| 16 | 0 |
| 17 | 0 |
| 18 | 0 |
| 19 | 0 |
| 20 | 0 |
| 21 | 0 |
| 22 | 0 |
| 23 | 0 |
| 24 | 0 |
+--------------+
【问题讨论】:
样本数据和预期输出会有所帮助。 你的表结构和预期输出是什么? 【参考方案1】:试试这个:你应该GROUP BY
days并根据以下条件计算值:
SELECT DAY(date_dispatched) Days,
COUNT(1) AS DispatchedCount
--Place the column here if you have any particular like `dispatched_number`
--If we have separate rows for every transaction then `COUNT(1) will work fine`
FROM shipment
WHERE status = 'Dispatched'
AND date_dispatched BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()
GROUP BY DAY(date_dispatched)
ORDER by DAY(date_dispatched);
您可以看到每个状态的计数,因此您需要从 where 删除 status = 'Dispatched'
并在选择中使用下面的 CASE
SUM(CASE WHEN status = 'Dispatched' THEN 1 ELSE 0 END) AS DispatchedCount,
SUM(CASE WHEN status = 'Delivered' THEN 1 ELSE 0 END) AS DeliveredCount,
SUM(CASE WHEN status = 'Completed' THEN 1 ELSE 0 END) AS CompletedCount,
【讨论】:
【参考方案2】:这个例子从2015-01-01
到2015-01-31
select lstOfDays, date_dispatched,case when Dispatched_Count is null then 0 else Dispatched_Count end as Dispatched_Count from
(SELECT ADDDATE('2015-01-01', INTERVAL @i:=@i+1 DAY) AS lstOfDays
FROM (
SELECT a.a
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
) a
JOIN (SELECT @i := -1) r1
WHERE
@i < DATEDIFF('2015-01-31', '2015-01-01')) as a
LEFT JOIN
(select date_dispatched, Status, count(1) as Dispatched_Count from
shipment_table where Status = 'Dispatched' GROUP BY date_dispatched) as b
on a.lstOfDays = b.date_dispatched ORDER BY lstofDays
你必须输入StartDate
和EndDate
这将返回如下输出:
lstofDays date_dispatched Dispatched_Count
2015-01-01 0
2015-01-02 0
2015-01-03 0
2015-01-04 0
2015-01-05 0
2015-01-06 0
2015-01-07 2015-01-07 1
2015-01-08 0
2015-01-09 2015-01-09 2
2015-01-10 0
2015-01-11 0
2015-01-12 0
2015-01-13 0
2015-01-14 0
2015-01-15 0
2015-01-16 0
2015-01-17 0
2015-01-18 0
2015-01-19 0
2015-01-20 0
2015-01-21 0
2015-01-22 0
2015-01-23 0
2015-01-24 0
2015-01-25 0
2015-01-26 0
2015-01-27 0
2015-01-28 0
2015-01-29 0
2015-01-30 0
2015-01-31 0
您需要添加 year
和 month
否则它将包含您的所有 date_dispatched 字段。
或者这会从Date Today - 31 days
自动生成
set @edate = DATE_FORMAT(CURDATE(), "%Y-%m-%d");
set @sdate = DATE_ADD(DATE_FORMAT(CURDATE(), "%Y-%m-%d"),INTERVAL -31 DAY);
select case when date_dispatched is null then Date_format(date(lstOfDays),'%d') else Date_format(date(date_dispatched),'%d') end date_dispatched,
case when Dispatched_Count is null then 0 else Dispatched_Count end as Dispatched_Count from
(SELECT ADDDATE(@sdate, INTERVAL @i:=@i+1 DAY) AS lstOfDays
FROM (
SELECT a.a
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
) a
JOIN (SELECT @i := -1) r1
WHERE
@i < DATEDIFF(@edate,@sdate)) as a
LEFT JOIN
(select date_dispatched, Status, count(1) as Dispatched_Count from
shipment_table where Status = 'Dispatched' GROUP BY date_dispatched) as b
on a.lstOfDays = b.date_dispatched ORDER BY lstofDays
结果:
26 0
27 0
28 0
29 0
30 0
31 0
01 0
02 0
03 0
04 0
05 0
06 0
07 0
08 0
09 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 1
18 0
19 2
20 0
21 0
22 0
23 0
24 0
25 0
26 0
【讨论】:
以上是关于如何获取每天的计数()的主要内容,如果未能解决你的问题,请参考以下文章