我正在尝试分配一个级别,如下面的示例(rowFinal)所示,其中行由GalleryId划分,并由StartDateKey升序。但是,我需要一个'New‘EventName从1重新启动计数,如下面的StartdateKey 20131219示例所示。
GalleryId StartDateKey row EventName rowFinal
425934233 20130226 1 Renew 1
425934233 20130326 2 Renew 2
425934233 20130426 3 Renew 3
425934233 20130526 4 Renew 4
425934233 20130626 5 Renew 5
425934233 20131219 6 New 1
425934233 20140119 7 Renew 2
425934233 20140219 8 Renew 3
425934233 20140319 9 Renew 4
425934233 20140419 10 Renew 5
…
425931351 20121210 1 Renew 1
425931351 20130110 2 Renew 2
425931351 20130210 3 Renew 3
425931351 20130310 4 Renew 4
425931351 20130410 5 Renew 5
425931351 20130510 6 Renew 6
425931351 20130610 7 Renew 7
425931351 20130710 8 Renew 8
425931351 20130810 9 Renew 9 由于速度的原因,分析和更新每一行(而循环)被证明是不切实际的。任何建议都将不胜感激。
发布于 2015-07-29 16:53:15
你可以用诡计来完成这个任务。这样做的目的是根据前面“新建”记录的数量对行进行分组。然后,您可以使用累积和来完成此操作:
select t.*,
row_number() over (partition by GalleryId, numRenew order by startdatekey) as rowFinal
from (select t.*,
sum(case when EventName = 'Renew' then 1 else 0 end) over
(partition by partition by GalleryId order by startdatekey) as numRenew
from table t
) t;发布于 2015-07-29 16:41:39
尝试将不同的部分分离成不同的查询,并将它们合并在一起。
发布于 2015-07-29 16:51:18
select *,
row_number() over(partition by galleryid,substring(startdatekey,7,2) order by startdatekey)
as rowFinal
from tablename;看起来,您正在对galleryid以及startdatekey的最后两个字符进行分区。
https://stackoverflow.com/questions/31706322
复制相似问题