我正在使用分析,我想得到每天的计数,并且它将在当天返回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天的日期的查询,但我无法获得每天的计数()。
示例:
我有一个包含以下列的货件表:
id = some 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 |
+--------------+
答案 0 :(得分:0)
此示例从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
答案 1 :(得分:0)
尝试这个:您应该GROUP BY
天并根据条件计算值,如下所示:
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);
您可以查看每个状态的计数,因此您需要从选择
中的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,
json =
'[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1005", "type": "DeleteMe" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" }
]
}
]';
objArray = JSON.parse(json);
objArray.reject! {|obj|
obj['batters']['batter'].detect {|batter|
batter['type'] == 'DeleteMe'
}
};
result = objArray.to_json;