[英]Select data for last 10 days
I want to implement SQL query for Bar chart which shows volumes per day for last 10 days. 我想对条形图实施SQL查询,该条形图显示最近10天每天的交易量。 For example I have this table structure:
例如,我有这个表结构:
CREATE TABLE `payment_transactions` (
`id` int(11) NOT NULL,
`amount` int(11) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
);
I need to get this example result (grouped per day): 我需要得到以下示例结果(每天分组):
Date | Amount| Number of transactions per day |
11-11-2018 | 30 | 3 |
11-12-2018 | 230 | 13 |
How I can implement this using MariaDB? 我如何使用MariaDB来实现呢?
You just need to chop off the time portion from date and group by this: 您只需要从日期中切除时间部分并按此分组:
SELECT DATE(created_at) AS Date, SUM(amount) AS Total_Amount, COUNT(*) AS Num_Transactions
FROM t
GROUP BY DATE(created_at)
To restrict the results to last 10 days use the following where clause: 要将结果限制为持续10天,请使用以下where子句:
-- 10 days including today
WHERE created_at >= CURRENT_DATE - INTERVAL 9 DAY
AND created_at < CURRENT_DATE + INTERVAL 1 DAY
-- 10 days excluding today
WHERE created_at >= CURRENT_DATE - INTERVAL 10 DAY
AND created_at < CURRENT_DATE
-- 10 days from current time
WHERE created_at > CURRENT_TIMESTAMP - INTERVAL 10 DAY
AND created_at <= CURRENT_TIMESTAMP
You can try below 您可以在下面尝试
select date(created_at) as cdate, sum(amount) as amount,count(id) as
nooftransaction
from payment_transactions
where date(created_at)>=date(now()- interval 10 day) and date(created_at)<date(now())
group by date(created_at)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.