[英]how to use row_number to count dates after a certain date?
I have a dataset where I want to count the rows before and after my anchor date.我有一个数据集,我想在锚定日期之前和之后计算行数。 I think a window function using
row_number()
would work, but I am not sure how it would be written.我认为使用
row_number()
的 window function 会起作用,但我不确定它是如何编写的。
my current table:我当前的表:
order_id contact_id placed_at anchor_date
13236647 123456 2020-06-24T12:47:18
16253983 123456 2020-07-19T05:54:52
16720335 123456 2020-08-20T02:02:06
17823059 123456 2020-09-17T02:02:04 2020-09-17T02:02:04
18523920 123456 2020-10-12T13:53:19
19324467 123456 2020-11-12T01:02:18
20234536 123456 2020-12-04T01:02:42
70523487 654321 2015-09-21T09:25:25
71234048 654321 2015-10-01T19:02:28
14145443 654321 2020-03-28T10:21:57
14134525 654321 2020-03-28T10:31:33
11244748 654321 2020-04-03T06:20:57 2020-04-03T06:20:57
My desired output would look like this:我想要的 output 看起来像这样:
rows_before_anchor
is numbering all the rows before anchor_date
ordered by placed_at
and grouped by contact_id
. rows_before_anchor
对 anchor_date 之前的所有行进行编号,由anchor_date
排序placed_at
contact_id
分组。
rows_after_anchor
is numbering all the rows after anchor_date
ordered by placed_at
grouped by contact_id
rows_after_anchor
对placed_at
之后的所有行进行编号,由anchor_date
排序,按contact_id
分组
Here's what I tried:这是我尝试过的:
SELECT
order_id,
contact_id,
placed_at,
ROW_NUMBER() OVER (PARTITION BY contact_id ORDER BY placed_at < anchor_date) AS rows_before_anchor,
ROW_NUMBER() OVER (PARTITION BY contact_id ORDER BY placed_at > anchor_date) AS rows_after_anchor
FROM mytable
My desired table:我想要的表:
order_id contact_id placed_at anchor_date rows_before_anchor rows_after_anchor
13236647 123456 2020-06-24T12:47:18 1
16253983 123456 2020-07-19T05:54:52 2
16720335 123456 2020-08-20T02:02:06 3
17823059 123456 2020-09-17T02:02:04 2020-09-17T02:02:04
18523920 123456 2020-10-12T13:53:19 1
19324467 123456 2020-11-12T01:02:18 2
20234536 123456 2020-12-04T01:02:42 3
70523487 654321 2015-09-21T09:25:25 1
71234048 654321 2015-10-01T19:02:28 2
14145443 654321 2020-03-28T10:21:57 3
14134525 654321 2020-03-28T10:31:33 4
11244748 654321 2020-04-03T06:20:57 2020-04-03T06:20:57
Here's one way you can do it.这是您可以做到的一种方法。 First you need to identify all the rows either side of your anchor date and assign them a common grouping which is done in the CTE below.
首先,您需要识别锚日期两侧的所有行,并为它们分配一个通用分组,这在下面的 CTE 中完成。 Once you have this grouping you can use it to apply the desired numbering by including it as a partition.
拥有此分组后,您可以使用它通过将其包含为分区来应用所需的编号。
It's not clear from your sample data whether the row numbering should be zero or a blank string, since row numbers are integers by definition I've defaulted the blank values to be zero - if you really want blanks then just cast the row numbers as a varchar.从您的示例数据中不清楚行号应该为零还是空白字符串,因为根据定义,行号是整数我默认空白值为零 - 如果您真的想要空白,那么只需将行号转换为varchar。
with grp as (
select *,
Row_Number() over(partition by contact_id order by placed_at)
- Row_Number() over(partition by contact_id, anchor_date order by placed_at) gnum
from t
)
select order_id, contact_id, placed_at, anchor_date,
case when anchor_date is null and gnum=0 then
Row_Number() over(partition by contact_id, gnum order by placed_at)
else 0 end as rows_before_anchor,
case when anchor_date is null and gnum>0 then
Row_Number() over(partition by contact_id, gnum order by placed_at)
else 0 end as rows_after_anchor
from grp
order by contact_id, placed_at;
There's no Fiddle I know of for Amazon Redshift but see this example DB<>Fiddle using SQL Server, it should hopefully share the same or similar syntax.我不知道 Amazon Redshift 有 Fiddle,但请参阅使用 SQL 服务器的示例 DB<>Fiddle ,它应该希望共享相同或相似的语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.