mysql - 如何在MYSQL中获取每个月的最大日期
问题描述
如何获得每个月的最大日期?我不知道我做错了什么,但数据是根据我的结果重复的。
这是桌子。
history table
|ID | from_range | to_range | price | main_id
1 2019-09-01 00:00:00 2019-09-15 00:00:00 5 1
2 2019-09-16 00:00:00 2019-09-30 00:00:00 10 1
3 2019-10-01 00:00:00 2019-10-15 00:00:00 15 1
4 2019-10-16 00:00:00 2019-10-19 00:00:00 20 1
5 2019-10-20 00:00:00 0000-00-00 00:00:00 25 1
current_usage table
id main_id total_usage date_created
1 1 10 2019-09-01
2 1 10 2019-09-02
3 1 10 2019-09-03
4 1 10 2019-09-04
5 1 10 2019-09-05
6 1 10 2019-09-06
7 1 20 2019-09-07
8 1 5 2019-09-08
9 1 5 2019-09-09
10 1 10 2019-09-10
11 1 10 2019-09-15
12 1 10 2019-09-16
13 1 10 2019-09-17
14 1 10 2019-09-18
15 1 10 2019-09-19
16 1 10 2019-09-20
17 1 20 2019-09-25
18 1 5 2019-09-26
19 1 5 2019-09-27
20 1 10 2019-09-30
21 1 10 2019-10-01
22 1 10 2019-10-02
23 1 10 2019-10-03
24 1 10 2019-10-10
25 1 10 2019-10-11
26 1 10 2019-10-12
27 1 10 2019-10-18
28 1 20 2019-10-20
29 1 5 2019-10-21
30 1 5 2019-10-22
31 1 10 2019-10-23
所以在我的history_table中,我每个月都得到了date_range,这取决于用户每天更改成本的次数。
所以基本上
输出应该是这样的
id price range_date total_usage
1 5 2019-09-01 - 2019-09-15 SUM(total_usage) based on the date range
2 10 2019-09-16 - 2019-09-30 SUM(total_usage) based on the date range
3 15 2019-10-01 - 2019-10-15 SUM(total_usage) based on the date range
4 20 2019-10-16 - 2019-09-19 SUM(total_usage) based on the date range
5 25 2019-10-20 - 2019-10-31 SUM(total_usage) based on the date range
之后我打算每个月总结一下。基于那 5 行的输出。
这是我当前的查询,如果他们只有一个价格历史记录,我能够做到这一点,如果只有一个价格历史记录,那是一个单独的查询,但是对于多个价格历史记录,我真的很难获得范围从每个月和他们的总和。
SELECT price.price,
price.from _range AS first_range,
price.to_range AS second_range,
(current_usage.total_usage),
current_usage.date_created
FROM history AS price
INNER JOIN current_usage AS current_usage
ON price.main_id = current_usage .main_id
INNER JOIN(SELECT MAX(to_range) AS maxdate
FROM history
GROUP BY YEAR(to_range), MONTH(to_range)) x ON price.to_range= maxdate
任何帮助将不胜感激,我真的需要一些帮助。
解决方案
如果我理解正确,您只需要基于表的连接date_created
来位于current_usage
表和表之间 :from_range
to_range
history
SELECT
p.id,
p.price,
DATE(p.from_range) AS first_range,
DATE(IF(p.to_range = '0000-00-00 00:00:00', LAST_DAY(p.from_range), p.to_range)) AS second_range,
SUM(c.total_usage) AS total_usage
FROM history AS p
INNER JOIN current_usage AS c
ON c.main_id = p.main_id
AND c.date_created BETWEEN DATE(p.from_range) AND DATE(IF(p.to_range = '0000-00-00 00:00:00', LAST_DAY(p.from_range), p.to_range))
GROUP BY p.id
我假设表id
中的列history
是您的主键。因此,SELECT 子句中表中的其余非聚合列history
在功能上是相关的,因此无需在GROUP BY
推荐阅读
- shopify - 新注册的用户在 Shopify 中被重定向
- css - rake assets:precompile > Sass::SyntaxError: Invalid CSS after "padding": expected ";", was ":20px;"
- c# - 如何在一次调用 Azure 密钥保管库中获取所有机密
- java - GIF 图像在我的应用程序中不起作用
- microservices - 刷新没有队列的 Spring Cloud Config
- c# - 将 SelectedItem 传递给 DataTemplate 中 UserControl 的 ViewModel
- python - 在 txt.file 中使用 3 列的 edgelist
- node.js - 从 NodeJS 到 App 引擎的 Google Cloud 构建
- laravel - 从数据库传递模板时变量不呈现
- vb.net - 有没有程序可以输出黑色像素周边的 X 和 Y 坐标?