Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- database: test
- with tmp as ( -- 因为不能嵌套窗口函数, 如: win_func1(win_func2() over({window1})) over({window2})
- SELECT
- *,
- sum(price) OVER(partition by inq_code) old_total,
- case
- when max(price) over(partition by inq_code, cat_no) = price
- and row_number() over(partition by inq_code, cat_no, price ORDER BY id) = 1
- then price
- else null end distinct_price -- 只显示当前金额为窗口最大金额,且第一行的记录
- FROM [dbo].[test_complex_win_sum]
- )
- SELECT
- *,
- sum(distinct_price) OVER(partition by inq_code) total_price -- 计算去重总金额
- FROM tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement