带有over的RANK()函数为每次运行动态创建排名

时间:2019-06-04 07:44:43

标签: sql google-bigquery

我正在为表的分区创建等级。按名称列执行分区,并按其事务值排序。在生成这些分区并检查每个等级的计数时,我执行的每个查询在每个等级中得到的编号都不相同。

select count(*)  FROM (
                        --
                        -- Sort and ranks the element of RFM
                        --
                        SELECT
                            *,
                            RANK() OVER (PARTITION BY name ORDER BY date_since_last_trans desc) AS rfmrank_r,
                        FROM (
                            SELECT
                                name,
                                id_customer,
                                cust_age,
                                gender,
                                DATE_DIFF(entity_max_date, customer_max_date, DAY ) AS date_since_last_trans,
                                txncnt,
                                txnval,
                                txnval / txncnt AS avg_txnval
                            FROM 
                            (
                                SELECT
                                    name,
                                    id_customer,
                                    MAX(cust_age) AS cust_age,
                                 COALESCE(APPROX_TOP_COUNT(cust_gender,1)[OFFSET(0)].VALUE, MAX(cust_gender)) AS gender,
                                    MAX(date_date) AS customer_max_date,
                                    (SELECT MAX(date_date) FROM xxxxx) AS entity_max_date,
                                    COUNT(purchase_amount) AS txncnt,
                                    SUM(purchase_amount) AS txnval
                                FROM
                                    xxxxx
                                WHERE
                                date_date > (
                                    SELECT
                                        DATE_SUB(MAX(date_date), INTERVAL 24 MONTH) AS max_date
                                    FROM
                                        xxxxx)
                                AND cust_age >= 15
                                AND cust_gender IN ('M','F')
                                GROUP BY
                                    name,
                                    id_customer
                            )
                        )
                    )
          group by rfmrank_r

我第一次跑步

Row f0
1 3970
2 3017
3 2116
4 2118

我正在第二次奔跑

Row f0
1 4060
2 3233
3 2260
4 2145

该怎么办,如果我需要获得相同数量的分区,并且每次运行的排名相同 编辑: 对不起,场模糊 This is the output of field ```query to get this column````

1 个答案:

答案 0 :(得分:0)

RANK窗口函数确定一组值中值的等级。 每个值都在其分区内排名。排名条件的值相等的行将获得相同的排名。 Drill将绑定的行数添加到绑定的等级以计算下一个等级,因此这些等级可能不是连续的数字。

例如,如果两行的排名为1,则下一个排名为3。