SQL - 每周分区

发布于 2025-01-13 03:32:26 字数 574 浏览 2 评论 0原文

假设员工数据集要大得多。我们希望按周范围对员工加入的日期进行分区,但仅限于过去 6 个月,

我们该怎么做?

输入图片这里的描述

这是预期的结果。

我们看到第 1 行和第 2 行落在相同的周范围内,并且被划分在一起。第 3 行是该周的唯一值,因此它本身为 1。第 4 行和第 5 行属于同一周范围,因此它们为 2。第 6 行不属于 6 个月范围,因此为 0。

< a href="https://i.sstatic.net/8FjyN.png" rel="nofollow noreferrer">在此处输入图像描述

select id,name,date, count(*) over (partition by Date)
 from employee

Assume the employee dataset is much larger. And we want to partition on a weekly range the date an employee joined, but only for the last 6 months

How would we do that?

enter image description here

Here is expected result.

We see row 1&2 fall in same weekly range and are partitioned together. Row 3 is the only value of that week, so it is by itself as 1. Row 4&5 fall in the same weekly range, therefore they are 2. And row 6 does not fall within a 6 month range, therefore 0.

enter image description here

select id,name,date, count(*) over (partition by Date)
 from employee

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

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

发布评论

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

评论(1

两个我 2025-01-20 03:32:26

用这个

select id,name,date, count(*) over (partition by DATEPART(week,Date))
 from employee

use this

select id,name,date, count(*) over (partition by DATEPART(week,Date))
 from employee
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文