Mysql 使用groupBy为max查询创建最大限制
我有这种桌子Mysql 使用groupBy为max查询创建最大限制,mysql,sql,Mysql,Sql,我有这种桌子 +----+------------+-------+------------+ | id | company_id | price | periods | +----+------------+-------+------------+ | 1 | A1 | 500 | 2016-07-12 | | 2 | A2 | 540 | 2018-01-21 | | 3 | A1 | 440 | 2017-01-19
+----+------------+-------+------------+
| id | company_id | price | periods |
+----+------------+-------+------------+
| 1 | A1 | 500 | 2016-07-12 |
| 2 | A2 | 540 | 2018-01-21 |
| 3 | A1 | 440 | 2017-01-19 |
| 4 | A2 | 330 | 2016-01-12 |
| 5 | A3 | 333 | 2018-01-22 |
+----+------------+-------+------------+
首先,我只想选择最长期间,并使用此查询按公司id对其进行分组
SELECT salesreport.* FROM salesreport
INNER JOIN (SELECT company_id, MAX(periods) AS max_periods FROM salesreport WHERE periods < 2016-01-01 GROUP BY company_id) AS latest_report
ON salesreport.company_id = latest_report.company_id AND salesreport.periods = latest_report.max_periods;
但是现在我想对我想要分组的最大周期做一个限制,比如说我想要周期你刚才没有在日期值上加上一个qoute。这是基于userid:accounter的sqlfiddle演示的 更正:MAXperiods<'2017-01-01'
旧版:MAXperiods<2017-01-01您刚刚错过了在日期值上添加单个qoute。这是基于userid:accounter的sqlfiddle演示的 更正:MAXperiods<'2017-01-01' 旧版本:MAXperiods<2017-01-01使用子查询和相关方法
select * from salesreport s
where periods = (select max(periods) from salesreport
where company_id = s.company_id and periods <= '2017-01-01')
使用相关方法的子查询
select * from salesreport s
where periods = (select max(periods) from salesreport
where company_id = s.company_id and periods <= '2017-01-01')
试试这个,只在周期的地方获取最大数据试试这个,只在周期的地方获取最大数据你不需要比较最大日期,你只需要比较日期周期。同时删除HAVING子句
请试试这个,它应该适合您。您不需要比较最大日期,只需要比较日期周期。同时删除HAVING子句
请试试这个,它应该对你有用。请告诉我们你尝试过的结果。它返回为空,一点也没有……你看到你的工作查询有错误吗?您投入的资金少于2016年,但它显示了2017年和2018年的行数。请告诉我们该表格是如何定义的?periods列是字符串还是日期?在第一次查询中选择periods<2016-01-01的位置。请重新检查数据它是日期类型,即使我更改了<2018-01-01的期间,它也会返回空的。请向我们显示您尝试过的结果。它返回空的,一点也没有…您是否看到您的工作查询也有问题?您投入的资金少于2016年,但它显示了2017年和2018年的行数。请告诉我们该表格是如何定义的?periods列是字符串还是日期?在第一次查询中选择periods<2016-01-01的位置。请重新检查数据它是日期类型,即使我更改了<2018-01-01期间的位置,它也将返回空Tooto它不工作,如何使用这些代码获取2016期间的销售报告?不工作,如何使用这些代码获取2016年期间的销售报告?能否告诉我如何将其与我正在处理的整个groupBy情况结合起来?@LaurensiusTony。。。在这里,我添加了不在当前查询中的句点过滤器。因此,我也用不同的方法这样做,这样你就可以找到丢失的东西。能告诉我如何将它与我工作的整个groupBy情况结合起来吗?@LaurensiusTony。。。在这里,我添加了不在当前查询中的句点过滤器。因此,我也用不同的方法这样做,这样你就可以找到丢失的东西。哇,就是这样,整个错误只是因为一个引语谢谢你这么多人,它就像放一个where一样简单哇,就是这样,整个错误只是因为一个引语谢谢你这么多人,它就像放一个where一样简单
SELECT salesreport.* FROM salesreport
INNER JOIN (SELECT company_id, MAX(periods) AS max_periods FROM salesreport GROUP BY company_id HAVING MAX(periods) < 2017-01-01) AS latest_report
ON salesreport.company_id = latest_report.company_id AND salesreport.periods = latest_report.max_periods;
SELECT salesreport.* FROM salesreport
INNER JOIN (SELECT company_id, MAX(periods) AS max_periods FROM salesreport WHERE periods < 2017-01-01 GROUP BY company_id ) AS latest_report
ON salesreport.company_id = latest_report.company_id AND salesreport.periods = latest_report.max_periods;
select * from salesreport s
where periods = (select max(periods) from salesreport
where company_id = s.company_id and periods <= '2017-01-01')
SELECT salesreport.* FROM salesreport
INNER JOIN
(
SELECT company_id, MAX(periods) AS max_periods
FROM salesreport where periods<'2017-01-01'
GROUP BY company_id
) AS latest_report
ON salesreport.company_id = latest_report.company_id
AND salesreport.periods = latest_report.max_periods;
SELECT salesreport.* FROM salesreport INNER JOIN (SELECT company_id, MAX(periods) AS max_periods FROM salesreport WHERE periods < '2017-01-01' GROUP BY company_id) AS latest_report ON salesreport.company_id = latest_report.company_id AND salesreport.periods = latest_report.max_periods