[英]Finding maximum revenue day
I have a table revenue with following fields 我有一个带有以下字段的表收入
+----+-----------+-------------+---------------+
| id | member_id | amount_paid | datetime_paid |
+----+-----------+-------------+---------------+
I want to run a query to find the top 10 maximum revenue days. 我想运行查询以查找前10个最大收入天数。 What would be the fastest way to achieve this?
实现这一目标的最快方法是什么? The table has a lot of data.
该表有很多数据。
Regards, 问候,
Try the basic query which does it: 尝试执行此操作的基本查询:
SELECT DATE(`datetime_paid`) AS `Max Revenue`
FROM `revenue`
ORDER BY `amount_paid` DESC
GROUP BY DATE(`datetime_paid`)
LIMIT 10;
This shows you the Top 10 Max paid days. 这将向您显示前10个最高带薪天数。 :)
:)
Assuming your datetime_paid
column is of some date or time type ( see MySQL docu ), the following should work. 假设您的
datetime_paid
列具有某种日期或时间类型( 请参见MySQL docu ),则以下内容应该可以工作。
SELECT SUM( `amount_paid` ) AS `amount`, DATE( `datetime_paid` ) as `date`
FROM yourTable
GROUP BY `date`
ORDER BY `amount` DESC
LIMIT 10
First you need to group the results by the day the payment was made. 首先,您需要按付款日期对结果进行分组。 This can be done by selecting
DATE(datetime_paid) as payday
, then grouping by payday
. 这可以通过选择
DATE(datetime_paid) as payday
,然后按payday
分组来完成。 To get the total, select SUM(amount_paid) as total
. 要获得总计,请选择
SUM(amount_paid) as total
。 Finally, to get the top 10, order by total desc
and limit 10
. 最后,要获得前10名,请按
total desc
和limit 10
排序。
Your final query should look like: 您的最终查询应如下所示:
SELECT DATE(`datetime_paid`) AS `payday`, SUM(`amount_paid`) AS `total`
FROM `table_name_here`
GROUP BY `payday`
ORDER BY `total` DESC
LIMIT 10
You may need to change that ORDER BY
line to ORDER BY SUM(amount_paid) DESC
, I can't remember if field aliases are allowed in ordering. 您可能需要将该
ORDER BY
行更改为ORDER BY SUM(amount_paid) DESC
,我不记得是否在排序中允许使用字段别名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.