如何按月统计mysql的三列?
该表有 3 列过滤器(filter1、filter2、filter3)和一列日期,您需要拉出每个过滤器并按月计算它们的数量 有一个过滤器的工作请求,但如何按月计数正确拉出 3 个过滤器?
SELECT *
FROM ( SELECT filter1, COUNT() AS 'Количество'
FROM mosedo.doc_rows_og
WHERE filter1 IS NOT NULL
GROUP BY filter1
) AS a LEFT JOIN (
SELECT filter1, COUNT() AS 'January '
FROM mosedo.doc_rows_og
WHERE data_reg LIKE '%-01-%'
AND filter1 IS NOT NULL
GROUP BY filter1
) as b USING(filter1) LEFT JOIN (
SELECT filter1, COUNT(*) AS 'February '
FROM mosedo.doc_rows_og
WHERE data_reg LIKE '%-02-%'
AND filter1 IS NOT NULL
GROUP BY filter1
) as c using(filter1)
结果:
DDL 和DML
db<>fiddle 此处< /em>
CREATE TABLE `doc_rows_og` (
`number_doc` varchar(250) NOT NULL,
`data_reg` longtext,
`komy` longtext,
`ot_kogo` longtext,
`soderjanie` longtext,
`number_sogol` longtext,
`number_otv` longtext,
`srok_sam` longtext,
`srok_isp` longtext,
`fio_isp` varchar(250) NOT NULL,
`status_` longtext,
`porych` longtext,
`vid` longtext,
`istochink` longtext,
`ot_kuda` longtext,
`nomer` longtext,
`status` longtext,
`control` longtext,
`name_object` longtext,
`kto` longtext,
`rezultat` longtext,
`filter1` longtext,
`filter2` longtext,
`filter3` longtext,
`dostovernost` longtext,
`sujet` longtext,
`opisanie` longtext,
`prim` longtext,
`type_object` longtext,
`type` longtext,
PRIMARY KEY (`number_doc`,`fio_isp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `mosedo`.`doc_rows_og` (`number_doc`, `data_reg`, `komy`, `ot_kogo`, `soderjanie`, `number_sogol`, `number_otv`, `srok_sam`, `srok_isp`, `fio_isp`, `status_`, `porych`, `filter1`, `filter2`, `filter3`) VALUES ('1', '2022-01-25', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'filterA', 'filterB', 'filterC');
INSERT INTO `mosedo`.`doc_rows_og` (`number_doc`, `data_reg`, `komy`, `ot_kogo`, `soderjanie`, `number_sogol`, `number_otv`, `srok_sam`, `srok_isp`, `fio_isp`, `status_`, `porych`, `filter2`, `filter3`) VALUES ('2', '2022-02-25', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'filterC', 'filterA');
INSERT INTO `mosedo`.`doc_rows_og` (`number_doc`, `data_reg`, `komy`, `ot_kogo`, `soderjanie`, `number_sogol`, `number_otv`, `srok_sam`, `srok_isp`, `fio_isp`, `status_`, `porych`, `filter1`, `filter2`, `filter3`) VALUES ('3', '2022-01-25', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'filterB', 'filterA', '');
INSERT INTO `mosedo`.`doc_rows_og` (`number_doc`, `data_reg`, `komy`, `ot_kogo`, `soderjanie`, `number_sogol`, `number_otv`, `srok_sam`, `srok_isp`, `fio_isp`, `status_`, `porych`, `filter1`, `filter2`, `filter3`) VALUES ('4', '2022-02-25', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'filterB', 'filterC', 'filterA');
- 第一列 - 过滤器A、过滤器B、过滤器C;
- 第二列 - 对第一列中的每个值进行计数,其中
data_reg like '%-01-%'
; - 第三列 - 对第一列中的每个值进行计数,其中
data_reg like '%-02-%'
The table has 3 columns of filters(filter1, filter2, filter3) and a column with a date, you need to pull out each filter and calculate their number by month
There is a working request for one filter, but how to pull out 3 filters correctly with a monthly count?
SELECT *
FROM ( SELECT filter1, COUNT() AS 'Количество'
FROM mosedo.doc_rows_og
WHERE filter1 IS NOT NULL
GROUP BY filter1
) AS a LEFT JOIN (
SELECT filter1, COUNT() AS 'January '
FROM mosedo.doc_rows_og
WHERE data_reg LIKE '%-01-%'
AND filter1 IS NOT NULL
GROUP BY filter1
) as b USING(filter1) LEFT JOIN (
SELECT filter1, COUNT(*) AS 'February '
FROM mosedo.doc_rows_og
WHERE data_reg LIKE '%-02-%'
AND filter1 IS NOT NULL
GROUP BY filter1
) as c using(filter1)
Results:
DDL & DML
db<>fiddle here
CREATE TABLE `doc_rows_og` (
`number_doc` varchar(250) NOT NULL,
`data_reg` longtext,
`komy` longtext,
`ot_kogo` longtext,
`soderjanie` longtext,
`number_sogol` longtext,
`number_otv` longtext,
`srok_sam` longtext,
`srok_isp` longtext,
`fio_isp` varchar(250) NOT NULL,
`status_` longtext,
`porych` longtext,
`vid` longtext,
`istochink` longtext,
`ot_kuda` longtext,
`nomer` longtext,
`status` longtext,
`control` longtext,
`name_object` longtext,
`kto` longtext,
`rezultat` longtext,
`filter1` longtext,
`filter2` longtext,
`filter3` longtext,
`dostovernost` longtext,
`sujet` longtext,
`opisanie` longtext,
`prim` longtext,
`type_object` longtext,
`type` longtext,
PRIMARY KEY (`number_doc`,`fio_isp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `mosedo`.`doc_rows_og` (`number_doc`, `data_reg`, `komy`, `ot_kogo`, `soderjanie`, `number_sogol`, `number_otv`, `srok_sam`, `srok_isp`, `fio_isp`, `status_`, `porych`, `filter1`, `filter2`, `filter3`) VALUES ('1', '2022-01-25', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'filterA', 'filterB', 'filterC');
INSERT INTO `mosedo`.`doc_rows_og` (`number_doc`, `data_reg`, `komy`, `ot_kogo`, `soderjanie`, `number_sogol`, `number_otv`, `srok_sam`, `srok_isp`, `fio_isp`, `status_`, `porych`, `filter2`, `filter3`) VALUES ('2', '2022-02-25', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'filterC', 'filterA');
INSERT INTO `mosedo`.`doc_rows_og` (`number_doc`, `data_reg`, `komy`, `ot_kogo`, `soderjanie`, `number_sogol`, `number_otv`, `srok_sam`, `srok_isp`, `fio_isp`, `status_`, `porych`, `filter1`, `filter2`, `filter3`) VALUES ('3', '2022-01-25', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'filterB', 'filterA', '');
INSERT INTO `mosedo`.`doc_rows_og` (`number_doc`, `data_reg`, `komy`, `ot_kogo`, `soderjanie`, `number_sogol`, `number_otv`, `srok_sam`, `srok_isp`, `fio_isp`, `status_`, `porych`, `filter1`, `filter2`, `filter3`) VALUES ('4', '2022-02-25', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'filterB', 'filterC', 'filterA');
- first column - filterA,filterB,filterC;
- second column - count where
data_reg like '%-01-%'
for each value from the first column; - third column - count where
data_reg like '%-02-%'
for each value from the first column
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试将所有filter1、filter2 和filter3 值合并到单个列中,则需要UNION ALL 而不是JOIN。然后使用条件 SUM() 按月计算行数。
请注意,该示例使用 STR_TO_DATE() 因为
data_reg
列将日期存储为字符串。理想情况下,您应该将该列的数据类型更改为 DATE,以便您可以更好地利用可用的 日期函数。结果:
db<>fiddle 此处
If you're trying to merge all of the filter1,filter2 and filter3 values into a single column, you need a UNION ALL instead of a JOIN. Then use a conditional SUM() to count the number of rows by month.
Note, the example uses STR_TO_DATE() because the
data_reg
column stores dates as strings. Ideally you should change the data type of that column to DATE, so you can take better advantage of the available date functions.Results:
db<>fiddle here