sql获取条件存在的第一个日期

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

我有一个collections表:subscription\u id,transaction\u date\u est,start\u date\u est,end\u date\u est,transaction\u id,invoice\u number,user\u id,transaction\u amount,plan\u code,transaction\u type
我有购买订阅的用户,我为每个订阅生成一个订阅id。每个订阅有1到多个发票。对于每个订阅,我试图找出用户何时通过使用窗口函数传递了100美元的累计金额。我的预期结果:订阅\u id,交易\u日期
我试过:

SELECT subscription_id, MAX(date) transaction_date
FROM (SELECT subscription_id, 
SUM(usd_price) LAG (partition by subscription_id
ORDER BY first_transaction_date ASC) AS total
GROUP BY(current_period_started_at)
HAVING BY total >= 100
ORDER BY usd_price)

但是我没有成功提取出用户超过100美元的第一个日期。

t2a7ltrp

t2a7ltrp1#

我想你想要:

select c.*
from (
    select c.*, sum(usd_price) over((partition by subscription_id order by transaction_date) sum_usd_price
    from collections c
) c
where sum_usd_price >= 100 and sum_usd_price - usd_price < 100

相关问题