如何根据密集的排名结果更新值?

gmxoilav  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(235)

我有一个查询(sql server 2017),在同一天找到两个不同的折扣。

WITH CTE AS (SELECT    [date_id], [good_id], [store_id], [name_promo_mech], [discount],
                       RN = DENSE_RANK() OVER (PARTITION BY [date_id], [good_id], [store_id], [name_promo_mech]
                       ORDER BY [discount]) + 
                       DENSE_RANK() OVER (PARTITION BY [date_id], [good_id], [store_id], [name_promo_mech]
                       ORDER BY [discount] DESC) - 1

                       FROM [dbo].[ds_promo_list_by_day_new] AS PL
                       )

SELECT * FROM CTE 
WHERE RN > 1;
GO

查询结果:

+------------+----------+---------+-----------------+----------+----+
| date_id    | store_id | good_id | name_promo_mech | discount | RN |
+------------+----------+---------+-----------------+----------+----+
| 2017-01-01 | 3        | 98398   | January 2017    | 15       | 2  |
+------------+----------+---------+-----------------+----------+----+
| 2017-01-01 | 3        | 98398   | January 2017    | 40       | 2  |
+------------+----------+---------+-----------------+----------+----+
| 2017-01-01 | 5        | 98398   | January 2017    | 15       | 3  |
+------------+----------+---------+-----------------+----------+----+
| 2017-01-01 | 5        | 98398   | January 2017    | 40       | 3  |
+------------+----------+---------+-----------------+----------+----+
| 2017-01-01 | 5        | 98398   | January 2017    | 30       | 3  |
+------------+----------+---------+-----------------+----------+----+

现在我想对source表中所有唯一的good\u id、store\u id、name\u promo\u merch进行相同的折扣。这是有规律的。例如,对于good\u id=98398,store\u id=3,name\u promo\u mech=n'january 2017'行,有10个条目有15折,20个条目有40折,那么15折应该替换为40折。但是,如果每个折扣的条目数相同,则为所有条目设置最大折扣。
我能做这个吗?源表中的行数约为1亿。

1u4esq0p

1u4esq0p1#

您要做的是在每个日期将值设置为mode(最常见值的统计术语)以及任意组合。可以使用窗口函数:

with toupdate as (
      select pl.*,
             first_value(discount) over (partition by date_id, good_id, store_id, name_promo_mech order by cnt desc, discount desc) as mode_discount
      from (select pl.*,
                   count(*) over (partition by date_id, good_id, store_id, name_promo_mech, discount) as cnt
            from ds_promo_list_by_day_new pl
           ) pl
     )
update toupdate
    set discount = mode_discount
    where mode_discount <> discount;

子查询统计每天每个折扣的值数。外部查询获取计数最大的折扣,如果是平局,则获取值较大的折扣。
剩下的就是一个简单的例子 update .

相关问题