分区结束后的附加条件

时间:2019-02-25 11:00:59

标签: sql select

https://www.db-fiddle.com/f/rgLXTu3VysD3kRwBAQK3a4/3

我的问题是我希望函数分区开始以仅从特定时间范围开始对行进行计数。

在此示例中,如果我在末尾添加rn = 1,则将从结果中排除order_id = 5(因为分区按paid_date排序,并且order_id = 6的日期较早),但不应该这样,因为我希望分区的时间范围从'2019-01-10'开始。

添加条件rn = 1的预期输出应为order_id 3,5,11,15,现在是其唯一的3,11,15

  • 它应仅包含在给定时间范围内的第一个订单,其中包含is_paid = 0的订单(如果之前的订单中包含is_paid = 1的订单,则不计算在内)

2 个答案:

答案 0 :(得分:1)

使用关联的子查询与not exists

DEMO

 SELECT order_id, customer_id, amount, is_paid, paid_date,  rn FROM (
        SELECT o.*, 
            ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY paid_date,order_id) rn
        FROM orders o
         WHERE paid_date between '2019-01-10' 
    and '2019-01-15'
    ) x  where rn=1 and not exists (select 1 from orders o1 where x.order_id=o1.order_id
    and is_paid=1)

输出:

order_id    customer_id amount  is_paid paid_date      rn
3           101          30      0  10/01/2019 00:00:00 1
5           102          15      0  10/01/2019 00:00:00 1
11          104          31      0  10/01/2019 00:00:00 1
15          105          11      0  10/01/2019 00:00:00 1

答案 1 :(得分:0)

如果应该给order_id优先级,然后在分区功能order by子句中将付款日期之前的优先级放在优先级,则可以解决您的问题。

SELECT order_id, customer_id, amount, is_paid, paid_date,  rn FROM (
            SELECT o.*, 
                ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY  order_id,paid_date) rn
            FROM orders o
        ) x WHERE is_paid = 0 and paid_date between 
        '2019-01-10' and '2019-01-15' and rn=1

由于您需要首先安排付款日期,因此需要在分区表中暗示一个where条件,以避免不必要的日期中断分区功能。

SELECT order_id, customer_id, amount, is_paid, paid_date,  rn FROM (
        SELECT o.*, 
            ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY paid_date, order_id) rn
        FROM orders o
  where paid_date between '2019-01-10' and '2019-01-15'
    ) x WHERE is_paid = 0 and rn=1