过滤汇总查询

时间:2018-10-24 18:06:21

标签: sql sql-server

在获取sum(count_ID)等于一定金额的日期方面,我请求您的协助。

start             complete        revenue            count_ID

1/1/2016            Yes             $5                  10
2/5/2016            Yes             $5                  20
3/4/2016            Yes             $15                 50
3/5/2016            No              $25                 100  
3/15/2016           No              $25                 70 


select start from table
group by start
having sum(count_ID) = 80

请求的结果:

1/1/2016            Yes             $5                  10
2/5/2016            Yes             $5                  20
3/4/2016            Yes             $15                 50

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用sum窗口函数在子查询中获取累计金额,然后在主查询中获取等于一定金额的金额。

select * from (
    select *,SUM(count_ID) over(order by start) total 
    from table
) t1
where total <= 80