Advertisement
Pandaaaa906

去重总金额

Dec 1st, 2022 (edited)
1,266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.60 KB | None | 0 0
  1. -- database: test
  2. with tmp as (  -- 因为不能嵌套窗口函数, 如: win_func1(win_func2() over({window1})) over({window2})
  3.     SELECT
  4.     *,
  5.     sum(price) OVER(partition by inq_code) old_total,
  6.     case
  7.     when max(price) over(partition by inq_code, cat_no) = price
  8.         and row_number() over(partition by inq_code, cat_no, price ORDER BY id) = 1
  9.     then price
  10.     else null end distinct_price -- 只显示当前金额为窗口最大金额,且第一行的记录
  11.  
  12.     FROM [dbo].[test_complex_win_sum]
  13. )
  14.  
  15. SELECT
  16. *,
  17. sum(distinct_price) OVER(partition by inq_code) total_price  -- 计算去重总金额
  18. FROM tmp
  19.  
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement