如何按时间间隔选择/排序日期?

发布于 2024-07-21 22:02:50 字数 420 浏览 2 评论 0原文

例如:

如果我们的表记录如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

李不 2024-07-28 22:02:50

更正:如果反过来,需要使用模数,而不是整数除法 - 抱歉...

特定 12 月 31 日以来的月数,并且 mod 是模数除法(输出除法后的余数)

Select [Column list here] 
From Table
Group By Case When MonthCount Mod 12 < 6 
         Then 0 Else 1 End  

如果 MonthCount 是一个计算值,用于计算自 例如,SQL Server,您可以使用 DateDiff 函数

Select [Column list here] 
From Table
Group By Case When DateDiff(month, myDateColumn, curdate) % 12 < 6 
         Then 0 Else 1 End

(在 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)

Select [Column list here] 
From Table
Group By Case When MonthCount Mod 12 < 6 
         Then 0 Else 1 End  

In SQL Server, for example, you could use the DateDiff Function

Select [Column list here] 
From Table
Group By Case When DateDiff(month, myDateColumn, curdate) % 12 < 6 
         Then 0 Else 1 End

( 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

权谋诡计 2024-07-28 22:02:50
SELECT (DATEDIFF(MONTH, thedate, GETDATE()) / 6) AS semester,
       SUM(receipt)
FROM thetable
GROUP BY semester
ORDER BY semester

关键思想是通过给出“学期”的表达式进行分组和排序。

SELECT (DATEDIFF(MONTH, thedate, GETDATE()) / 6) AS semester,
       SUM(receipt)
FROM thetable
GROUP BY semester
ORDER BY semester

the key idea is grouping and ordering by the expression that gives you the "semester".

慵挽 2024-07-28 22:02:50

这个问题确实让我困惑,因为我实际上无法想出一个简单的解决方案。 该死。

我能管理的最好的方法是对以下内容进行绝对的混蛋,您创建一个临时表,将“期间”插入其中,连接回原始表,然后将其分组。

假设您的内容表具有以下内容

ID int
Date DateTime
Counter int

并且您正在尝试对六个月内的所有计数器进行求和

DECLARE @min_date datetime
select @min_date = min(date) from test

DECLARE @max_date datetime
select @max_date = max(date) from test


DECLARE @today_a datetime
DECLARE @today_b datetime
set @today_a = getdate()
set @today_b = getdate()

CREATE TABLE #temp (startdate DateTime, enddate DateTime)

WHILE @today_a > @min_date
BEGIN
    INSERT INTO #temp (startDate, endDate) VALUES (dateadd(month, -6, @today_a), @today_a)
    SET @today_a = dateadd(month, -6, @today_a)
END
WHILE @today_b < @max_date
BEGIN
    INSERT INTO #temp (startDate, endDate) VALUES (@today_b, dateadd(month, 6, @today_b))
    SET @today_b = dateadd(month, 6, @today_b)
END

SELECT * FROM #temp

SELECT 
    sum(counter), 
    'Between ' + Convert(nvarchar(10), startdate, 121) + ' => ' +  Convert(nvarchar(10), enddate, 121) as Period
FROM test t
    JOIN #Temp ht
        ON t.Date between ht.startDate AND ht.EndDate
GROUP BY 
    'Between ' + Convert(nvarchar(10), startdate, 121) + ' => ' +  Convert(nvarchar(10), enddate, 121)

DROP TABLE #temp

我真的希望有人能够想出更好的解决方案我的大脑显然已经融化了。

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

ID int
Date DateTime
Counter int

And you're trying to sum all the counter's in six month periods

DECLARE @min_date datetime
select @min_date = min(date) from test

DECLARE @max_date datetime
select @max_date = max(date) from test


DECLARE @today_a datetime
DECLARE @today_b datetime
set @today_a = getdate()
set @today_b = getdate()

CREATE TABLE #temp (startdate DateTime, enddate DateTime)

WHILE @today_a > @min_date
BEGIN
    INSERT INTO #temp (startDate, endDate) VALUES (dateadd(month, -6, @today_a), @today_a)
    SET @today_a = dateadd(month, -6, @today_a)
END
WHILE @today_b < @max_date
BEGIN
    INSERT INTO #temp (startDate, endDate) VALUES (@today_b, dateadd(month, 6, @today_b))
    SET @today_b = dateadd(month, 6, @today_b)
END

SELECT * FROM #temp

SELECT 
    sum(counter), 
    'Between ' + Convert(nvarchar(10), startdate, 121) + ' => ' +  Convert(nvarchar(10), enddate, 121) as Period
FROM test t
    JOIN #Temp ht
        ON t.Date between ht.startDate AND ht.EndDate
GROUP BY 
    'Between ' + Convert(nvarchar(10), startdate, 121) + ' => ' +  Convert(nvarchar(10), enddate, 121)

DROP TABLE #temp

I really hope someone can come up with a better solution my brain has obviously melted.

浅沫记忆 2024-07-28 22:02:50

不完全是您想要完成的任务,但您可以使用 DATEDIFF 函数来区分每个记录的范围:

SELECT t.MonthGroup, SUM(t.Counter) AS TotalCount
FROM (    
      SELECT Counter, (DATEDIFF(m, GETDATE(), Date) / 6) AS MonthGroup
      FROM Table
     ) t
GROUP BY t.MonthGroup

这将创建一个子查询,其中的表达式表示您想要的日期范围组。 然后它会按此日期范围组对子查询进行分组,然后您可以对结果执行任何您想要的操作。

编辑:我根据您的示例修改了示例。

Not quite what you're attempting to accomplish, but you could use the DATEDIFF function to distinguish the ranging of each record:

SELECT t.MonthGroup, SUM(t.Counter) AS TotalCount
FROM (    
      SELECT Counter, (DATEDIFF(m, GETDATE(), Date) / 6) AS MonthGroup
      FROM Table
     ) t
GROUP BY t.MonthGroup

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.

怂人 2024-07-28 22:02:50

如果您使用的是 SQL Server:

SELECT *,
    (
        FLOOR
        (
            (
                DATEDIFF(month, GETDATE(), date_column)
                - CASE WHEN DAY(GETDATE()) > DAY(date_column) THEN 1 ELSE 0 END
            ) / 6.0
        ) * 6
    ) AS SixMonthlyInterval
FROM your_table

如果您使用的是 MySQL:

SELECT *,
    (
        FLOOR
        (
            (
                ((YEAR(date_column) - YEAR(CURDATE())) * 12)
                + MONTH(date_column) - MONTH(CURDATE())
                - CASE WHEN DAY(CURDATE()) > DAY(date_column) THEN 1 ELSE 0 END
            ) / 6.0
        ) * 6
    ) AS SixMonthlyInterval
FROM your_table

If you're using SQL Server:

SELECT *,
    (
        FLOOR
        (
            (
                DATEDIFF(month, GETDATE(), date_column)
                - CASE WHEN DAY(GETDATE()) > DAY(date_column) THEN 1 ELSE 0 END
            ) / 6.0
        ) * 6
    ) AS SixMonthlyInterval
FROM your_table

If you're using MySQL:

SELECT *,
    (
        FLOOR
        (
            (
                ((YEAR(date_column) - YEAR(CURDATE())) * 12)
                + MONTH(date_column) - MONTH(CURDATE())
                - CASE WHEN DAY(CURDATE()) > DAY(date_column) THEN 1 ELSE 0 END
            ) / 6.0
        ) * 6
    ) AS SixMonthlyInterval
FROM your_table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文