MySQL 跨日期范围求和值
我有一个表,每行包含一个日期和一些任意的数值。我需要对特定但动态的日期间隔的该值求和。
SELECT VERSION();
5.0.51a-24+lenny5
CREATE TABLE IF NOT EXISTS `work_entries` (
`entry_id` int(10) unsigned NOT NULL auto_increment,
`employee_id` tinyint(3) unsigned NOT NULL,
`work_date` date NOT NULL,
`hour_count` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`entry_id`)
);
INSERT INTO `work_entries` (`entry_id`, `employee_id`, `work_date`, `hour_count`) VALUES
(1, 1, '2011-04-25', 2),
(2, 1, '2011-04-26', 3),
(3, 1, '2011-04-27', 1),
(4, 2, '2011-04-25', 2),
(5, 2, '2011-04-27', 4),
(6, 1, '2011-05-08', 2),
(7, 2, '2011-05-06', 8),
(8, 2, '2011-05-08', 9),
(9, 2, '2011-05-09', 1),
(10, 1, '2011-05-29', 3),
(11, 1, '2011-05-30', 1),
(12, 2, '2011-05-30', 2),
(13, 1, '2011-06-02', 2),
(14, 1, '2011-06-04', 3),
(15, 1, '2011-06-14', 1),
(16, 2, '2011-06-14', 2),
(17, 2, '2011-06-17', 4),
(18, 1, '2011-06-18', 2),
(19, 2, '2011-06-19', 8),
(20, 2, '2011-06-26', 9),
(21, 2, '2011-07-01', 1),
(22, 1, '2011-07-03', 3),
(23, 1, '2011-07-03', 1),
(24, 2, '2011-07-16', 2);
以下查询返回上述数据集的正确输出,并且应该说明我正在尝试执行的操作,但是,我需要根据 work_date
生成年份和月份值。日子(16、15)永远不会改变。具体来说,此查询将生成两行,一行用于指定的时间间隔,另一行用于其余时间,其中每个周期需要一行(对于具有值的所有月份,从 N 月 16 日到 N+1 月 15 日)。
SELECT
SUM(hour_count) AS res
FROM `work_entries`
GROUP BY work_date
BETWEEN '2011-04-16' AND '2011-05-15';
-- Outputs
res
----
44
32
-- Should give
res
----
32
14
28
2
另一个可以正常工作但时间间隔错误(第 01-31 天)的示例:
SELECT
SUM(hour_count) AS res
FROM `work_entries`
GROUP BY MONTH(work_date);
-- Outputs
res
---
12
26
31
7
此外,如果有一种方法可以同时输出起始日期和截止日期,我也希望如此,但这不是很好重要的。
I have a table with each row containing a date and some arbitrary, numeric value. I need to sum this value for a specific but dynamic date interval.
SELECT VERSION();
5.0.51a-24+lenny5
CREATE TABLE IF NOT EXISTS `work_entries` (
`entry_id` int(10) unsigned NOT NULL auto_increment,
`employee_id` tinyint(3) unsigned NOT NULL,
`work_date` date NOT NULL,
`hour_count` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`entry_id`)
);
INSERT INTO `work_entries` (`entry_id`, `employee_id`, `work_date`, `hour_count`) VALUES
(1, 1, '2011-04-25', 2),
(2, 1, '2011-04-26', 3),
(3, 1, '2011-04-27', 1),
(4, 2, '2011-04-25', 2),
(5, 2, '2011-04-27', 4),
(6, 1, '2011-05-08', 2),
(7, 2, '2011-05-06', 8),
(8, 2, '2011-05-08', 9),
(9, 2, '2011-05-09', 1),
(10, 1, '2011-05-29', 3),
(11, 1, '2011-05-30', 1),
(12, 2, '2011-05-30', 2),
(13, 1, '2011-06-02', 2),
(14, 1, '2011-06-04', 3),
(15, 1, '2011-06-14', 1),
(16, 2, '2011-06-14', 2),
(17, 2, '2011-06-17', 4),
(18, 1, '2011-06-18', 2),
(19, 2, '2011-06-19', 8),
(20, 2, '2011-06-26', 9),
(21, 2, '2011-07-01', 1),
(22, 1, '2011-07-03', 3),
(23, 1, '2011-07-03', 1),
(24, 2, '2011-07-16', 2);
The following query returns the correct output for the above data set and should illustrate what I'm trying to do, however, I need to generate the year and month values based on work_date
. The days (16, 15) never change. Specifically, this query will produce two rows, one for the specified interval and one for the rest, where I need one row for each period (16th of month N to 15th of month N+1 for all months with values).
SELECT
SUM(hour_count) AS res
FROM `work_entries`
GROUP BY work_date
BETWEEN '2011-04-16' AND '2011-05-15';
-- Outputs
res
----
44
32
-- Should give
res
----
32
14
28
2
An alternative example that works correctly but on the wrong interval (days 01-31):
SELECT
SUM(hour_count) AS res
FROM `work_entries`
GROUP BY MONTH(work_date);
-- Outputs
res
---
12
26
31
7
Additionally, if there is a way to output the from- and to- dates at the same time I'd like that as well, but that's not very important.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
迷人的:
lovely:
您可以使用另一个表来存储所有期间范围。
并与其建立连接
然后您可以在需要时添加 where 子句。
You could use another table where you store all period ranges.
and make a join with it
You can then put a where clause when you need it.
有点hackish,但
仔细想想,上面的方法根本行不通。它只会返回两行,具体取决于 work_date 字段是否落在生成的开始/结束时间段内或之外。
基本上,您所需的查询需要一个更像“财政月”而不是日历月的组间隔。
Somewhat hackish, but
Thinking about it, the above wouldn't work at all. It'd only ever return two rows, based on if the work_date field falls inside the generated start/end periods, or outside.
Basically your desired query requires a group interval that's more of a "fiscal month" than a calendar month.