在SQL Server中按段中的客户交易排名
我有以下表,其中包含客户的交易详细信息。
tranactaction Date | culting ind |
---|---|
1/27/2022 | 1 |
1/29/2022 | 1 |
2/27/2022 | 1 |
3/27/2022 | 1 |
3/29/2022 | 1 |
3/31/2022 | 1 |
4/2/2022 | 1 |
4/4/4/20222222 | 1 |
4/6/2022 | 1 |
在该表中连续交易发生在每两天被视为一个细分市场中。 例如, 1月27日至1月29日之间的交易被认为是段1& 3月29日和4月6日之间的交易被认为是段2。我需要按日期顺序对每段的交易进行排名。如果默认情况下,交易不属于任何细分市场,则排名为1。预期输出为低于。
细分等级 | tranactaction date | cusitiverid |
---|---|---|
1 | 1/27/2022 | 1 |
2 | 1/29/2022 | 1 |
1 | 2/27/2022 | 1 |
1 1 | 3/27/2022 | 1 |
2 | 3/29/2022 | 1 |
3 | 3/31/2022 | 1 |
4 4 4/ 4 | /4/ 2/2022 | 1 |
5 | 4/4/2022 | 1 |
6 | 4/6/2022 | 1 |
有人可以指导如何在T-SQL中实现这一目标吗?
I have below table which has customer's transaction details.
Tranactaction date | CustomerID |
---|---|
1/27/2022 | 1 |
1/29/2022 | 1 |
2/27/2022 | 1 |
3/27/2022 | 1 |
3/29/2022 | 1 |
3/31/2022 | 1 |
4/2/2022 | 1 |
4/4/2022 | 1 |
4/6/2022 | 1 |
In this table consecutive transactions occurred in every two days considered as a segment.
For example, Transactions between Jan 27th and Jan 29th considered as segment 1 & Transactions between Mar 29th and Apr 6th considered as Segment 2. I need to rank the transactions per segment with date order. If a transaction not fall under any segment by default the rank is 1. Expected output is below.
Segment Rank | Tranactaction date | CustomerID |
---|---|---|
1 | 1/27/2022 | 1 |
2 | 1/29/2022 | 1 |
1 | 2/27/2022 | 1 |
1 | 3/27/2022 | 1 |
2 | 3/29/2022 | 1 |
3 | 3/31/2022 | 1 |
4 | 4/2/2022 | 1 |
5 | 4/4/2022 | 1 |
6 | 4/6/2022 | 1 |
Can somebody guide how to achieve this in T-sql?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ()检查
transdate
的更改,即在2天之内并组合在一起(作为一个细分)。 After that userow_number()
to generate the required sequenceUsing lag() to check for change in
TransDate
that is within 2 days and groups together (as a segment). After that userow_number()
to generate the required sequencedb<>fiddle demo