Doing calculation in sum() function : SUM « Aggregate Functions « Oracle PL / SQL






Doing calculation in sum() function

    

SQL>
SQL>
SQL> create table sale(
  2           gift_id            integer
  3          ,product_id             integer
  4          ,quantity               number(4,0)
  5          ,price                 number(7,2)
  6          ,primary key (gift_id ,product_id)
  7  );

Table created.

SQL>
SQL> -- order_item table inserts
SQL> insert into sale(gift_id, product_id, quantity, price) values(1, 2, 10, 23.00 );

1 row created.

SQL> insert into sale(gift_id, product_id, quantity, price) values(2, 1, 1, 23.11 );

1 row created.

SQL>
SQL>
SQL> select product_id, sum(quantity) "TOTAL SOLD",
  2         sum(price * quantity) "GROSS $"
  3    from sale
  4   group by product_id;

PRODUCT_ID TOTAL SOLD    GROSS $
---------- ---------- ----------
         1          1      23.11
         2         10        230

2 rows selected.

SQL>
SQL>
SQL> drop table sale;

Table dropped.

   
    
    
    
  








Related examples in the same category

1.SUM: total for all NOT NULL values, accepts only numeric datatype values
2.Syntax: SUM([DISTINCT]|[ALL] )
3.GROUP BY function would produce inventory totals distributed across different vendors
4.SUM function and NULLs
5.Using the SUM function with GROUP BY Clause
6.sum with column calculation
7.Add an "OTHER" and "TOTAL" column to the report:
8.Compute sum on salary
9.Sum column for a certain time period
10.Sum salary group by department number
11.Sum salary over
12.Sum() function and having clause
13.Wrap case when into sum() function