如何按时间间隔选择/排序日期?
例如:
如果我们的表记录如下:
25/06/2009
28/12/2009
19/02/2010
16/04/2011
20/05/2012
我想根据从当前日期开始的 6 个月间隔来拆分/选择此日期。 结果应该是这样的: 从现在起 0-6 个月:第一个记录 7-12 个月后:第二记录 ...
如果您使这个简单,我们将非常感激,因为我将其变得非常愚蠢和复杂,例如:
declare variable like t1=curdate()+6
t2=curdate()+12
...
然后选择适合 curdate() 和 t1 之间的记录,然后是 t1 和 t2 等。
谢谢,
r。
For ex:
If we have in table records like:
25/06/2009
28/12/2009
19/02/2010
16/04/2011
20/05/2012
I want to split/select this dates according to 6 month intervals starting from current date.
result should be like:
0-6 month from now: first record
7-12 month from now: second record
...
It will be much apreciated if you make this simple as I made it very stupid and complicated like:
declare variable like t1=curdate()+6
t2=curdate()+12
...
then selected records to fit between curdate() and t1, then t1 and t2 etc.
Thanks,
r.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更正:如果反过来,需要使用模数,而不是整数除法 - 抱歉...
特定 12 月 31 日以来的月数,并且 mod 是模数除法(输出除法后的余数)
如果 MonthCount 是一个计算值,用于计算自 例如,SQL Server,您可以使用 DateDiff 函数
(在 SQL Server 中,百分号是模运算符),
这会将所有记录分组到存储桶中,每个存储桶包含六个月的数据
CORRECTION: Had it backwards, Need to use Modulus, not integer division - sorry...
If MonthCount is a calculated value which counts the number of months since a specific Dec 31, and mod is modulus division (output the remainder after dividing)
In SQL Server, for example, you could use the DateDiff Function
( in SQL Server the percent sign is the modulus operator )
This will group all the record into buckets which each contain six months of data
关键思想是通过给出“学期”的表达式进行分组和排序。
the key idea is grouping and ordering by the expression that gives you the "semester".
这个问题确实让我困惑,因为我实际上无法想出一个简单的解决方案。 该死。
我能管理的最好的方法是对以下内容进行绝对的混蛋,您创建一个临时表,将“期间”插入其中,连接回原始表,然后将其分组。
假设您的内容表具有以下内容
并且您正在尝试对六个月内的所有计数器进行求和
我真的希望有人能够想出更好的解决方案我的大脑显然已经融化了。
This question really baffled me, cos I couldn't actually come up with a simple solution for it. Damn.
Best I could manage was an absolute bastardization of the following where you create a Temp Table, insert the "Periods" into it, join back to your original table, and group off that.
Assume your content table has the following
And you're trying to sum all the
counter
's in six month periodsI really hope someone can come up with a better solution my brain has obviously melted.
不完全是您想要完成的任务,但您可以使用 DATEDIFF 函数来区分每个记录的范围:
这将创建一个子查询,其中的表达式表示您想要的日期范围组。 然后它会按此日期范围组对子查询进行分组,然后您可以对结果执行任何您想要的操作。
编辑:我根据您的示例修改了示例。
Not quite what you're attempting to accomplish, but you could use the DATEDIFF function to distinguish the ranging of each record:
This would create a sub query with an expression that expresses the date ranging group you want. It would then group the sub-query by this date ranging group and you can then do whatever you want with the results.
Edit: I modified the example based on your example.
如果您使用的是 SQL Server:
如果您使用的是 MySQL:
If you're using SQL Server:
If you're using MySQL: