Oracle SUM Function


/ Published in: SQL
Save to your folder(s)

A few examples of the SUM function.


Copy this code and paste it in your HTML
  1. SELECT * FROM student;
  2.  
  3. --Example 1
  4. SELECT SUM(fees_paid) AS SUM_FEES_PAID
  5. FROM student;
  6.  
  7. --Example 2
  8. SELECT SUM(fees_paid) AS SUM_FEES_PAID
  9. FROM student
  10. WHERE fees_paid <> fees_required;
  11.  
  12. --Example 3
  13. SELECT EXTRACT(MONTH FROM enrolment_date) AS ENROLMENT_MONTH,
  14. SUM(fees_paid) AS SUM_FEES_PAID
  15. FROM student
  16. GROUP BY EXTRACT(MONTH FROM enrolment_date);
  17.  
  18. --Example 4
  19. SELECT SUM(fees_paid) AS SUM_FEES_PAID,
  20. SUM(fees_paid * 0.1) AS SUM_TAX
  21. FROM student;
  22.  
  23. --Example 5
  24. SELECT SUM(fees_required - fees_paid) AS FEES_OWING
  25. FROM student;
  26.  
  27. --Example 6
  28. SELECT
  29. first_name,
  30. last_name,
  31. EXTRACT(MONTH FROM enrolment_date) AS ENROLMENT_MONTH,
  32. SUM(fees_paid) OVER (PARTITION BY (EXTRACT(MONTH FROM enrolment_date))) AS FEES_MONTH
  33. FROM student;
  34.  
  35. --Example 7
  36. SELECT SUM(fees_paid) AS SUM_ALL_FEES,
  37. SUM(DISTINCT fees_paid) AS SUM_DISTINCT_FEES
  38. FROM student;

URL: http://www.databasestar.com/oracle-sum/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.