简体   繁体   English

非顺序SQL分区

[英]non sequential SQL partitioning

Blockquote 大段引用

All, 所有,

I am trying to partition a set of data but I don't want my partitions to be "sequential". 我正在尝试对一组数据进行分区,但是我不希望分区是“顺序的”。

Traditionally by partitioning by ID and then by sale stage I get this. 传统上,通过按ID划分分区,然后按销售阶段划分,我得到了这个。 Sequential partitioning. 顺序分区。

ROW_NUMBER() OVER(PARTITION BY  ID,  sale stage ORDER BY  Converge, modified date desc)

ID               sale stage      modified date      partition
01_AAI_73133    Closed Lost        5/1/2015             1
01_AAI_73133    Closed Lost        4/26/2015            2
01_AAI_73133    Closed Lost        4/20/2015            3
01_AAI_73133    Locked and Loaded  4/5/2015             1
01_AAI_73133    Locked and Loaded  3/29/2015            2
01_AAI_73133    Pitching           3/7/2015             1
01_AAI_73133    Pitching           2/14/2015            2
01_AAI_73133    Pitching           2/1/2015             3
01_AAI_73134    Pitching           1/20/2015            4

What I actually want is this 我真正想要的是

ID               sale stage      modified date      partition
01_AAI_73133    Closed Lost        5/1/2015             1
01_AAI_73133    Closed Lost        4/26/2015            1
01_AAI_73133    Closed Lost        4/20/2015            1
01_AAI_73133    Locked and Loaded  4/5/2015             2
01_AAI_73133    Locked and Loaded  3/29/2015            2
01_AAI_73133    Pitching           3/7/2015             3
01_AAI_73133    Pitching           2/14/2015            3
01_AAI_73133    Pitching           2/1/2015             3
01_AAI_73134    Pitching           1/20/2015            3

Does anyone have a idea how to achieve this? 有谁知道如何实现这一目标?

Thanks 谢谢

You are probably looking for dense_rank() : 您可能正在寻找dense_rank()

DENSE_RANK() OVER (ORDER BY ID, salestage)

If you are trying to order by the maximum date for each id and sale stage, then you might need a subquery: 如果您要按每个ID和销售阶段的最大日期订购,则可能需要一个子查询:

select dense_rank() over (order by maxmd, id, salestate)
from (select . . .,
             MAX(ModifiedDate) OVER (PARTITION BY ID, salestage) as maxmd
      from . . .
     ) t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM