SQL 如何按日期范围分组

发布于 2024-12-08 02:46:20 字数 380 浏览 1 评论 0原文

有人可以帮我弄清楚按日期范围进行分组吗?

现在我有类似这样的查询

    Select date, count(x)
    from data
    group by date

这返回的结果看起来像这样

    2011/1/1   10
    2011/1/2   5
    2011/1/3   8
    2011/1/4   3

等等...

但我想每2天计数一次,以便数据看起来像这样

    2011/1/1   15
    2011/1/3   11

有什么想法吗?

谢谢

Can someone help me figure out to group by a range of dates??

Right now I have query similar to this

    Select date, count(x)
    from data
    group by date

This returns results that look like this

    2011/1/1   10
    2011/1/2   5
    2011/1/3   8
    2011/1/4   3

etc...

But I would like to count every 2 days so that the data would look like this

    2011/1/1   15
    2011/1/3   11

Any ideas??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

清风疏影 2024-12-15 02:46:20

您可以通过将日期转换为整数值并减少为偶数,将日期标准化为 2 组。一个简单的方法是 val / 2 * 2,因为第一个 / 2 将被截断任何小数位(只要 val 的类型 是一个整数!),* 2 会将其返回到原始值,但标准化为偶数除外。以下是使用 CTE 数据源对结果进行标准化和分组的示例:

;with Data as ( 
    select '1/1/2011' as [date], 1 as x union
    select '1/1/2011' as [date], 2 as x union
    select '1/1/2011' as [date], 3 as x union
    select '1/1/2011' as [date], 4 as x union
    select '1/1/2011' as [date], 5 as x union
    select '1/1/2011' as [date], 6 as x union
    select '1/1/2011' as [date], 7 as x union
    select '1/1/2011' as [date], 8 as x union
    select '1/1/2011' as [date], 9 as x union
    select '1/1/2011' as [date], 10 as x union
    select '1/2/2011' as [date], 11 as x union
    select '1/2/2011' as [date], 12 as x union
    select '1/2/2011' as [date], 13 as x union
    select '1/2/2011' as [date], 14 as x union
    select '1/2/2011' as [date], 15 as x union
    select '1/3/2011' as [date], 16 as x union
    select '1/3/2011' as [date], 17 as x union
    select '1/3/2011' as [date], 18 as x union
    select '1/3/2011' as [date], 19 as x union
    select '1/3/2011' as [date], 20 as x union
    select '1/3/2011' as [date], 21 as x union
    select '1/3/2011' as [date], 22 as x union
    select '1/3/2011' as [date], 23 as x union
    select '1/4/2011' as [date], 24 as x union
    select '1/4/2011' as [date], 25 as x union
    select '1/4/2011' as [date], 26 as x
)
Select
    cast(cast(cast(Date as datetime) as integer) / 2 * 2 as datetime) as date,
    count(x)
from data
group by cast(cast(Date as datetime) as integer) / 2 * 2

输出:

date                    (No column name)
2011-01-01 00:00:00.000 15
2011-01-03 00:00:00.000 11

You could normalize the dates into groups of 2 by converting to a numerical integer value, and reducing to the even numbers. A simple way to do that is val / 2 * 2, because the first / 2 will be truncated of any decimal places (as long as the type of val is an integer!), and * 2 will return it to the original value except normalized to an even number. Here is an example that normalizes and groups the results using a CTE data source:

;with Data as ( 
    select '1/1/2011' as [date], 1 as x union
    select '1/1/2011' as [date], 2 as x union
    select '1/1/2011' as [date], 3 as x union
    select '1/1/2011' as [date], 4 as x union
    select '1/1/2011' as [date], 5 as x union
    select '1/1/2011' as [date], 6 as x union
    select '1/1/2011' as [date], 7 as x union
    select '1/1/2011' as [date], 8 as x union
    select '1/1/2011' as [date], 9 as x union
    select '1/1/2011' as [date], 10 as x union
    select '1/2/2011' as [date], 11 as x union
    select '1/2/2011' as [date], 12 as x union
    select '1/2/2011' as [date], 13 as x union
    select '1/2/2011' as [date], 14 as x union
    select '1/2/2011' as [date], 15 as x union
    select '1/3/2011' as [date], 16 as x union
    select '1/3/2011' as [date], 17 as x union
    select '1/3/2011' as [date], 18 as x union
    select '1/3/2011' as [date], 19 as x union
    select '1/3/2011' as [date], 20 as x union
    select '1/3/2011' as [date], 21 as x union
    select '1/3/2011' as [date], 22 as x union
    select '1/3/2011' as [date], 23 as x union
    select '1/4/2011' as [date], 24 as x union
    select '1/4/2011' as [date], 25 as x union
    select '1/4/2011' as [date], 26 as x
)
Select
    cast(cast(cast(Date as datetime) as integer) / 2 * 2 as datetime) as date,
    count(x)
from data
group by cast(cast(Date as datetime) as integer) / 2 * 2

Output:

date                    (No column name)
2011-01-01 00:00:00.000 15
2011-01-03 00:00:00.000 11
雪化雨蝶 2024-12-15 02:46:20
Select floor((date - trunc(date,'MM')) / 2), count(x)
  from data
 group by floor((date - trunc(date,'MM')) / 2)
Select floor((date - trunc(date,'MM')) / 2), count(x)
  from data
 group by floor((date - trunc(date,'MM')) / 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文