[英]mysql inner join two tables with month matching
I have a two tables in MySQL and both tables in same database.我在 MySQL 中有两个表,并且两个表都在同一个数据库中。 table name data
表名数据
| service | count | date |
--------------------------------------
| bugss | 375 | 2022-01-01 05:00:00.00000
| fromsite | 5 | 2022-02-01 05:00:00.00000
| kbocetra | 100 | 2022-01-05 07:00:00.00000
tried for data.table试过 data.table
SELECT SUM(`count`) AS Alertcount,
DATE_FORMAT(`date`, '%M') AS Month,
FROM data
GROUP BY DATE_FORMAT(`date`, '%Y-%m')
output: output:
January | 475
February | 5
another table name pd另一个表名pd
| group | minutes | projdate |
--------------------------------
gcp | 145 | 2022-01-01 05:00:00.00000
azure | 10 | 2022-02-01 05:00:00.00000
aws | 80 | 2022-01-05 07:00:00.00000
i tried below command for separate tables, for pd table as below..which gives output as我在下面的命令中尝试了单独的表,如下所示的 pd 表..它给出了 output 作为
SELECT SUM(`minutes`) AS Hours,
DATE_FORMAT(`group `, '%M') AS Month
FROM pd
GROUP BY DATE_FORMAT(`group`, '%Y-%m')
output: output:
January | 225
February | 10
and im expected the ouput like below, and total count would be as output of two tables count/minutes ie, 475/225 and 5/10.我预计输出如下所示,总计数将是两个表计数/分钟的 output,即 475/225 和 5/10。 please help, i red about inner statement, but didn't worked.
请帮忙,我对内部陈述很生气,但没有用。
Month![]() |
total![]() |
---|---|
January![]() |
0.78 ![]() |
February![]() |
2 ![]() |
Run the following command and see the results.运行以下命令并查看结果。
SELECT
a.`Month`,
a.`Hours` / b.`Alertcount` as 'total'
FROM
(
SELECT
SUM( `minutes` ) AS Hours,
DATE_FORMAT( `group `, '%M' ) AS 'Month'
FROM
pd
GROUP BY
DATE_FORMAT( `group`, '%Y-%m' )
) a
INNER JOIN (
SELECT
SUM( `count` ) AS Alertcount,
DATE_FORMAT( `date`, '%M' ) AS 'Month'
FROM
DATA
GROUP BY
DATE_FORMAT( `date`, '%Y-%m' )
) b ON a.`Month` = b.`Month`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.