我想在某些条件下分配排名
我想根据数据集中每个'detter_id'的顺序,将等级分配给“ drug_name”。 (在这里,为了描述问题,我只在图像中显示一个患者_id)
select patient_id
,svcdate
,drug_name
,dense_rank() over(partition by patient_id order by first_date) as rank
from (
select *
,first_value(svcdate) over (
partition by patient_id, drug_name
order by svcdate) as first_date
from table
)
order by 1,2;
使用此查询,我将获得以下输出,
尽管我想要这样的东西(如下图所示,如下图所示)
请帮助我了解查询中我错过的内容以及如何解决此问题。 谢谢!!
I want to assign the rank to 'drug_name' as per the order of 'svcdate' for each 'patient_id' in a dataset. (here, to describe the issue I'm only showing one patient_id in the image)
select patient_id
,svcdate
,drug_name
,dense_rank() over(partition by patient_id order by first_date) as rank
from (
select *
,first_value(svcdate) over (
partition by patient_id, drug_name
order by svcdate) as first_date
from table
)
order by 1,2;
With this query I'm getting the following output,
Although, I want something like this (as shown in image below)
Please help me understand what I'm missing out in the query and how to address this issue.
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此cte进行数据:
并使用 conditonal_change_event 想要
:
using this CTE for the data:
And using CONDITONAL_CHANGE_EVENT gives you what you want
gives:
我们可以尝试在子查询中使用
lag
窗口函数以获取每个先前的drug_name,然后按条件汇总窗口函数比较rank
列。We can try to use
LAG
window function in the subquery to get each previous drug_name, then compare by condition aggregate window function to makerank
column.