查询sql,现在要求是查出coupon_no为a,日期最近的一条数据

就是一个查询sql,现在要求是查出coupon_no为a,日期最近的一条数据,但金额是两条数据相加的值,也就是金额查询结果为25,目前写出来的sql是这种,得不到想要的结果..这种情况应该要怎么写啊(我这写的有点简化,实际会展示很多列)

数据: coupon_no   total_prices    accept_date
          a            20         2022/04/17
          a            5          2022/04/18


select total_prices from (select total_prices,
row_number() OVER(PARTITION BY coupon_no ORDER BY accept_date desc) counts)
from tb) temp where temp.counts = '1'

select temp.*,b.sum_total_prices from (select *,
row_number() OVER(PARTITION BY coupon_no ORDER BY accept_date desc) counts)
from tb) temp 
(select coupon_no,sum(total_prices) as sum_total_prices from t_consumption group by t_user_id) b
on b.coupon_no= temp.coupon_no
where temp.counts = '1'

这样可以实现 估计查询效率不是很好


select coupon_no, sum(total_prices), max(accept_date)
from tb
where coupon_no = "a"
group by coupon_no

要统计最近3条就是 top 3 替换相应的表名即可

select  SUM(total_prices)
from Table_1 
where accept_date in (select top 2 accept_date 
from Table_1
where coupon_no = 'a'
 order by accept_date desc )

不需要分组,直接用窗口函数
sum进行累加。如何拿到金额的总和?
通过row_number进行确定最后金额总和所在的位次。
通过rk找到sum 的最终结果。 这个不需要分组,不过这个也有局限。最后返回的是每个用户只有一条记录