如何按操作日期进行分区(mysql)

时间:2019-05-30 09:07:36

标签: mysql

我是sql的新手,目前练习sakila数据库,目前我正在处理付款表(https://www.jooq.org/sakila)。

我正在尝试在前一天的付款次数旁边设置每天的付款次数。

这是我想出的:

SELECT
  DISTINCT(CAST(PAYMENT_DATE AS DATE)) AS 'Date',
  COUNT(payment_id) 
    OVER (PARTITION BY CAST(PAYMENT_DATE AS DATE) 
    ORDER BY CAST(PAYMENT_DATE AS DATE)) AS 'total_payments',
  COUNT(payment_id) 
    OVER (PARTITION BY DATE_ADD(CAST(PAYMENT_DATE AS DATE), INTERVAL -1 DAY)
    ORDER BY CAST(PAYMENT_DATE AS DATE) ) AS 'yesterday'

FROM PAYMENT
ORDER BY CAST(PAYMENT_DATE AS DATE)
LIMIT 100;    

但是,昨天列仍与total_payments列计算同一天。

为什么会发生,该如何解决?

我知道还有其他方法可以做到,但是我真的很想了解为什么这种方法不起作用

0 个答案:

没有答案