简体   繁体   English

在SQL查询中按日期,日间隔分组

[英]Group by date, day interval in SQL query

Say I have a table similar to the following (simplified): 假设我有一个类似于以下(简化)的表:

| Name  | Date     | Hours |
| Bob   | 10/1/13  |  5    |
| John  | 10/1/13  |  8    |
| Bob   | 10/2/13  |  6    |
| Ashley| 10/2/13  |  4    |
...
| Bob   | 10/17/13 |  4    |
| John  | 10/17/13 |  6    |
| John  | 10/18/13 |  3    |
...

Given a starting date (say 10/1/13), how can I construct a query to group Name, SUM(Hours) by 14-day intervals? 给定一个开始日期(例如10/1/13),如何构建一个查询以将名称,SUM(小时)分组为间隔14天? So that the result would be something like: 所以结果会是这样的:

| Name  | Period              | SUM(Hours) |
| Bob   | 10/1/13 - 10/14/13  |  11        |
| John  | 10/1/13 - 10/14/13  |  8         |
| Ashley| 10/1/13 - 10/14/13  |  4         |
| Bob   | 10/15/13 - 10/29/13 |  4         |
| John  | 10/15/13 - 10/29/13 |  9         |

I have tried suggestions listed in posts, such as this: Group by date range on weeks/months interval But they usually assume you want actual weeks. 我已尝试过帖子中列出的建议,例如: 按周/日间隔范围分组但是他们通常假设您想​​要实际的周数。 Since these are just 14-day intervals, they are not necessarily aligned with the weeks of the year. 由于这些只是间隔14天,因此不一定与一年中的几周一致。

Any suggestion or guidance appreciated! 任何建议或指导表示赞赏!

Edit: This is querying a NexusDB server, so it uses the SQL:2003 standard. 编辑:这是查询NexusDB服务器,因此它使用SQL:2003标准。

May be something like this could work? 这可能是这样的吗? (Assuming it's MySQL) (假设它是MySQL)

UPDATE : tested in SQL Fiddle (thanks @pm-77-1): http://sqlfiddle.com/#!2/3d7af/2 更新 :在SQL Fiddle中测试(感谢@ pm-77-1): http ://sqlfiddle.com/#!2/3d7af / 2

The word of caution : due to the fact that we are doing date arithmetic here, this query might become heavy if it will be run on a large table. 谨慎之处 :由于我们在这里进行日期算术,如果它将在大型表上运行,则此查询可能会变得很重。

SELECT Name, 
CONCAT(DATE_ADD(<your-starting-date>, INTERVAL period*14 day),
" - ",
DATE_ADD(<your-starting-date>, INTERVAL period*14+13 day)) as Period,
Hours FROM 
(SELECT Name, 
FLOOR(DATEDIFF(Date,<your-starting-date>)/14) AS period, 
SUM(Hours) as Hours 
FROM <yourtable> GROUP BY period, name) p;

UPDATE for NexusDB. NexusDB的更新。 I have found out some piece of info for DateDiff replacement in NexusDB: 我在NexusDB中找到了DateDiff替换的一些信息:

http://www.nexusdb.com/support/index.php?q=node/10091 http://www.nexusdb.com/support/index.php?q=node/10091

Taking that into account, you have two choices: either to add that function into your DB, then you don't have to modify the query, or to replace DATEDIFF with that definition: 考虑到这一点,您有两种选择:要么将该功能添加到数据库中,那么您不必修改查询,或者用该定义替换DATEDIFF:

SELECT Name, 
CONCAT(DATE_ADD(<your-starting-date>, INTERVAL period*14 day),
" - ",
DATE_ADD(<your-starting-date>, INTERVAL period*14+13 day)) as Period,
Hours FROM 
(SELECT Name, 
FLOOR(cast((cast(Date as float ) - cast(<your-starting-date> as float)) as integer)/14) as period,
SUM(Hours) as Hours 
FROM <yourtable> GROUP BY period, name) p;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM