[英]Rank() Over Partition assigning everything as 1
I am trying to rank a data set to determine how many times an account number appears per day so I can take an action based on how many.我正在尝试对数据集进行排名,以确定帐号每天出现的次数,以便我可以根据次数采取行动。
My data appears as follows:我的数据显示如下:
+---------------+-----------+-----------+------------------+-----------+---------------+-----------+-----------+-----------+-------------+
| accountnumber | ctry_code | prod_code | comm_file_postdt | post_dt | comm_file_pay | payment | comm_diff | days_diff | mindue_diff |
+---------------+-----------+-----------+------------------+-----------+---------------+-----------+-----------+-----------+-------------+
| 1234 | MX | PR | 6/29/2020 | 6/26/2020 | -583.5 | -583.5 | 0.01 | 105 | |
| 1234 | MX | PR | 6/29/2020 | 6/27/2020 | -443.85 | -443.85 | 0.01 | 138 | |
| 1234 | MX | GL | 6/30/2020 | 6/26/2020 | -2783.25 | -2783.25 | 0.01 | 141 | |
| 1234 | MX | OP | 6/30/2020 | 6/26/2020 | -4000 | -4000 | 0.01 | 57 | 0 |
| 1235 | MX | OP | 6/29/2020 | 6/27/2020 | -3794.65 | -3794.65 | -35.84 | 102 | 239 |
| 1236 | MX | OP | 6/29/2020 | 6/27/2020 | -239 | -239 | 35.85 | 102 | -537.5 |
| 1237 | MX | OP | 6/29/2020 | 6/27/2020 | -345.67 | -345.67 | -34.57 | 38 | 345.67 |
| 1238 | MX | OP | 6/29/2020 | 6/26/2020 | -3000 | -3000 | 371.91 | 63 | -2479.4 |
| 1238 | MX | OP | 6/29/2020 | 6/26/2020 | -1661.5 | -1661.5 | 0.01 | 41 | -11950.16 |
| 1238 | MX | OP | 6/29/2020 | 6/27/2020 | -15466.24 | -15466.24 | -1091.34 | 12 | 10913.46 |
+---------------+-----------+-----------+------------------+-----------+---------------+-----------+-----------+-----------+-------------+
What I am trying to do is rank each accountnumber for each separate comm_file_postdt.我要做的是为每个单独的 comm_file_postdt 对每个帐号进行排名。
Based on the table below my expected restful would be:根据下表,我预期的宁静将是:
+---------------+------------------+------+
| accountnumber | comm_file_postdt | rank |
+---------------+------------------+------+
| 1234 | 6/29/2020 | 1 |
| 1234 | 6/29/2020 | 2 |
| 1234 | 6/30/2020 | 1 |
| 1234 | 6/30/2020 | 2 |
| 1235 | 6/29/2020 | 1 |
| 1236 | 6/29/2020 | 1 |
| 1237 | 6/29/2020 | 1 |
| 1238 | 6/29/2020 | 1 |
| 1238 | 6/29/2020 | 2 |
| 1238 | 6/29/2020 | 3 |
+---------------+------------------+------+
However, I am getting Rank as 1 for every iteration i have tried.但是,对于我尝试过的每次迭代,我都将 Rank 设为 1。
I have done the following:我做了以下事情:
Select *,
rank() over(partition by accountnumber order by comm_file_postdt) as rank from tableA
select*,
rank() over(partition by accountnumber, comm_file_postdt order by post_dt) as rank from tableA
As well as a few others, but no matter what I try any combination of values in the partition and order I get everything being ranked as 1.以及其他一些,但无论我在分区和顺序中尝试任何值组合,我都会将所有内容列为 1。
Any guidance on to what I may be doing wrong would be very helpful.任何关于我可能做错的指导都会非常有帮助。
This is your code:这是你的代码:
rank() over(partition by accountnumber order by comm_file_postdt)
Your data has multiple rows with the same accountnumber
and comm_file_postdt
: these are ties, so rank()
assigns them the same value.您的数据有多行具有相同的accountnumber
和comm_file_postdt
:这些是关系,因此rank()
为它们分配相同的值。
The cleanest solution would be to use another column to break the ties - possibly post_dt
:最干净的解决方案是使用另一列来打破关系 - 可能是post_dt
:
rank() over(partition by accountnumber order by comm_file_postdt, post_dt)
Or you can use row_number()
, which guarantees no duplicates.或者你可以使用row_number()
,它保证没有重复。 However, without a deterministic order by clause, it is undefined which of the tied rows will be ranked first: this might, or might not be what you want:但是,如果没有确定性order by 子句,则未定义哪些绑定行将排在第一位:这可能是您想要的,也可能不是您想要的:
row_number() over(partition by accountnumber order by comm_file_postdt)
what you are probably looking for is row_number() if you want to know how many times如果您想知道多少次,您可能正在寻找的是 row_number()
*sample code * *示例代码*
;with mycte as
(
select
1234 as account_number ,'6/29/2020' as comm_file_postdt
union all select
1234 , '6/29/2020'
union all select
1234 , '6/30/2020'
union all select
1234 , '6/30/2020'
union all select
1235 , '6/29/2020'
union all select
1236 , '6/29/2020'
union all select
1237 , '6/29/2020'
union all select
1238 , '6/29/2020'
union all select
1238 , '6/29/2020'
union all select
1238 , '6/29/2020'
)
Select *, row_number() over (partition by account_number,comm_file_postdt order by comm_file_postdt) as [rank]
from mycte
result结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.