如何为 SQL 中的每个特定列应用 windows?

How to apply windows for each specific column in SQL?

我想为每个特定的每个用户完成最后一个事件 unit_of_measure:

我有这个table:

person_id   event_time       event_derscription   unit_of_measure 
-----------------------------------------------------------------
1           20200801120101  "some description"     "unit1"
1           20200801120501  "some description 2"   "unit1"
1           20200801120501  "some description 2"   "unit9"
2           20200801120301  "some description 3"   "unit1"
2           20200801120501  "some description 4"   "unit1"

预期输出为:

person_id   event_time       event_derscription   unit_of_measure 
-----------------------------------------------------------------
1           20200801120101  "some description"     "unit1"
2           20200801120301  "some description 2"   "unit1"
1           20200801120501  "some description 2"   "unit9"

我尝试了什么:

select * 
from 
    (select 
         person_id, event_time, event_derscription, unit_of_measure, 
         rank() over (partition by unit_of_measure order by event_time desc) as RN 
     from 
         test.person_events 
     where 
         partition_name = 20200801 
     group by 
         person_id, event_time, event_description, unit_of_measure) 
where 
    RN = 1;  // I try to use group by person_id to get the result for each person_id but it did not work 

我上面代码的输出是:

person_id   event_time       event_derscription   unit_of_measure 
-----------------------------------------------------------------
2           20200801120301  "some description 2"   "unit1"
1           20200801120501  "some description 2"   "unit9"

我有没有做错什么?

我想你想要的查询是:

select person_id, event_time, event_derscription, unit_of_measure
from (select pe,
             row_number() over (partition BY unit_of_measure, person_id order by event_time desc) as seqnum
      from test.person_events pe
      where partition_name = 20200801 
     ) pe
where seqnum = 1; 

备注:

  • 解决问题的主要方法是在 partition by.
  • 中包含 person_id
  • 我觉得没必要 group by。您的问题中没有任何内容提到为什么它是可取的。
  • 要获取一行,请使用 row_number() 而不是 rank()。即使你没有重复,它也传达了你想要一行的意图。