Page 85 - SQL
P. 85

Examples



        SUM


        Sum function sum the value of all the rows in the group. If the group by clause is omitted then sums
        all the rows.


         select sum(salary) TotalSalary
         from employees;



          TotalSalary

          2500



         select DepartmentId, sum(salary) TotalSalary
         from employees
         group by DepartmentId;


          DepartmentId      TotalSalary


          1                 2000

          2                 500



        Conditional aggregation


        Payments Table


          Customer     Payment_type       Amount


          Peter        Credit             100


          Peter        Credit             300

          John         Credit             1000


          John         Debit              500


         select customer,
                sum(case when payment_type = 'credit' then amount else 0 end) as credit,
                sum(case when payment_type = 'debit' then amount else 0 end) as debit
         from payments
         group by customer


        Result:








        https://riptutorial.com/                                                                               67
   80   81   82   83   84   85   86   87   88   89   90