Db2--sum over partition by usage

Source: Internet
Author: User
Tags db2

Reference to the source of the blog: http://www.cnblogs.com/luhe/p/4155612.html, the blog has been modified to add, modified the wrong place

Previously used Row_number (), rank () and other sort with over (partition by ... Order by ...), both of which are better understood: group First, then rank within the group.

Today suddenly came across the sum (...) over (partition by ... Order by ...), actually do not clear how to carry out, so looked up some information, did the next real exercise.

1. From the simplest start

SUM (...) over (), sum all rows

SUM (...) over (order by ...), and = First line to sum all values of the last row of the same ordinal line as the current line, the text is not well understood, see the algorithm parsing.

With AAAs(SELECT1A1B3CFrom dualUnionSELECT2A2B3CFrom dualUnionSELECT3A3B3CFrom dualUnionSELECT4A4B3CFrom dualUnionSELECT5A5B3CFrom dualUnionSELECT6A5B3CFrom dualUnionSELECT7A2B3CFrom dualUnionSELECT8A2B8CFrom dualUnionSELECT9A  3  B,   3  C from dual) select A,b,c,sum (c ) over (order by b) sum1,-- Sort, sum all the values of column C of the order number of the current line sum (c) over () Sum2-- No sort, sum column C all values              
From AA

Add by Pine door a flower:

  1. With AA as
  2. ( SELECT 1 A,1 B, 3 C from dual
  3. UNION
  4. SELECT 2 A,2 B, 3 C from dual
  5. UNION
  6. SELECT 3 A,3 B, 3 C from dual
  7. UNION
  8. SELECT 4 A,4 B, 3 C from dual
  9. UNION
  10. SELECT 5 A,5 B, 3 C from dual
  11. UNION
  12. SELECT 6 A,5 B, 3 C from dual
  13. UNION
  14. SELECT 7 A,2 B, 3 C from dual
  15. UNION
  16. SELECT 8 A,2 B, 8 C from dual
  17. UNION
  18. SELECT 9 A,3 B, 3 C from dual
  19. )
  20. SELECT A,
  21. B
  22. C
  23. SUM (c) over (order by a) sum1,--sorting, summing all values in column C of the order number of the current row--"New for bloggers"
  24. SUM (c) over (order by B) sum2,---have sort, sum all values in column C of the order number of the current row
  25. SUM (c) over () sum3 from AA order by A; --no sorting, sum column C all values


2. Combined with partition by

SUM (...) over (partition by ...), sum of rows in the same group

SUM (...) over (partition by ...), with the 1th sort summation principle, except that the scope is limited within the group

With AAAs(SELECT1A1B3CFrom dualUnionSELECT2A2B3CFrom dualUnionSELECT3A3B3CFrom dualUnionSELECT4A4B3CFrom dualUnionSELECT5A5B3CFrom dualUnionSELECT6A5B3CFrom dualUnionSELECT7A2B3CFrom dualUnionSELECT7A2B8CFrom dualUnionSELECT9A3B  3  C from dual) select a,b,c,sum (c) over (partition by b ) Partition_sum,sum (c) over (partition by b order by a desc) partition_order_sum from aa;          

  


Db2--sum over partition by usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.