Average Price Per Department : AVG « Aggregate Functions « Oracle PL / SQL

Home
Oracle PL / SQL
1.Aggregate Functions
2.Analytical Functions
3.Char Functions
4.Constraints
5.Conversion Functions
6.Cursor
7.Data Type
8.Date Timezone
9.Hierarchical Query
10.Index
11.Insert Delete Update
12.Large Objects
13.Numeric Math Functions
14.Object Oriented Database
15.PL SQL
16.Regular Expressions
17.Report Column Page
18.Result Set
19.Select Query
20.Sequence
21.SQL Plus
22.Stored Procedure Function
23.Subquery
24.System Packages
25.System Tables Views
26.Table
27.Table Joins
28.Trigger
29.User Previliege
30.View
31.XML
Oracle PL / SQL » Aggregate Functions » AVG 




Average Price Per Department
    
SQL>
SQL> CREATE TABLE Department (
  2  DepartmentID INT NOT NULL PRIMARY KEY,
  3  Name VARCHAR(50NOT NULL,
  4  Description VARCHAR(200NULL);
SQL>
SQL> CREATE SEQUENCE DepartmentIDSeq;
SQL>
SQL> CREATE OR REPLACE TRIGGER DepartmentAutonumberTrigger
  2  BEFORE INSERT ON Department
  3  FOR EACH ROW
  4  BEGIN
  5    SELECT DepartmentIDSeq.NEXTVAL
  6    INTO :NEW.DepartmentID FROM DUAL;
  7  END;
  8  /
SQL>
SQL> INSERT INTO Department (Name, DescriptionVALUES ('Software', 'Coding');
SQL> INSERT INTO Department (Name, DescriptionVALUES ('Hardware', 'Building');
SQL> INSERT INTO Department (Name, DescriptionVALUES ('QA', 'Testing');
SQL>
SQL>
SQL>
SQL>
SQL> CREATE TABLE Category (
  2  CategoryID INT NOT NULL PRIMARY KEY,
  3  DepartmentID INT NOT NULL,
  4  Name VARCHAR(50NOT NULL,
  5  Description VARCHAR (200NULL);
SQL>
SQL> CREATE SEQUENCE CategoryIDSeq;
SQL>
SQL> CREATE OR REPLACE TRIGGER CategoryAutonumberTrigger
  2  BEFORE INSERT ON Category
  3  FOR EACH ROW
  4  BEGIN
  5     SELECT CategoryIDSeq.NEXTVAL
  6     INTO :NEW.CategoryID FROM DUAL;
  7  END;
  8  /
SQL> INSERT INTO Category (DepartmentID, Name, Description)
  2     VALUES (1'Local', 'In town');
SQL> INSERT INTO Category (DepartmentID, Name, Description)
  2     VALUES (1'Remote', 'Telecommute');
SQL> INSERT INTO Category (DepartmentID, Name, Description)
  2     VALUES (2'Masks', 'By bits');
SQL> INSERT INTO Category (DepartmentID, Name, Description)
  2     VALUES (3'Wireless', 'Not connected');
SQL> INSERT INTO Category (DepartmentID, Name, Description)
  2     VALUES (3'Wired', 'Connected');
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> CREATE TABLE Product (
  2  ProductID INT NOT NULL PRIMARY KEY,
  3  Name VARCHAR(50NOT NULL,
  4  Description VARCHAR(1000NOT NULL,
  5  Price NUMBER NULL,
  6  ImagePath VARCHAR(50NULL,
  7  soldout NUMBER(1,0NULL,
  8  Promotion NUMBER(1,0NULL);
SQL>
SQL> CREATE SEQUENCE ProductIDSeq;
SQL>
SQL> CREATE OR REPLACE TRIGGER ProductAutonumberTrigger
  2  BEFORE INSERT ON Product
  3  FOR EACH ROW
  4  BEGIN
  5     SELECT ProductIDSeq.NEXTVAL
  6     INTO :NEW.ProductID FROM DUAL;
  7  END;
  8  /
SQL>
SQL> INSERT INTO Product (Name, Description, Price, ImagePath,soldout, Promotion)
  2  VALUES ('Pen', 'Ball Pen',5.99'pen.jpg', 10);
SQL>
SQL> INSERT INTO Product (Name, Description, Price, ImagePath, soldout, Promotion)
  2  VALUES ('Ruler','Long',14.99'ruler.jpg', 00);
SQL>
SQL> INSERT INTO Product (Name, Description, Price, ImagePath, soldout, Promotion)
  2  VALUES ('Desk', 'Computer Desk',5.99'desk.jpg', 01);
SQL>
SQL> INSERT INTO Product (Name, Description, Price, ImagePath, soldout, Promotion)
  2  VALUES ('PC', 'Notebook',49.99'pc.jpg', 01);
SQL>
SQL> INSERT INTO Product (Name, Description, Price, ImagePath, soldout, Promotion)
  2  VALUES ('Mouse', 'Wireless',9.99'mouse.jpg',  10);
SQL>
SQL> INSERT INTO Product (Name, Description, Price, ImagePath, soldout, Promotion)
  2  VALUES ('Keyboard','keyboard',3.75'keyboard.jpg', 00);
SQL>
SQL>
SQL>
SQL>
SQL> CREATE TABLE ProductCategory (
  2  ProductID INT NOT NULL,
  3  CategoryID INT NOT NULL,
  4  PRIMARY KEY (ProductID, CategoryID)
  5  );
SQL>
SQL>
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (1,3);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (2,1);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (2,3);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (3,3);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (4,1);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (5,2);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (6,3);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (6,4);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (7,4);
SQL> INSERT INTO ProductCategory (ProductID, CategoryIDVALUES (8,5);
SQL>
SQL>
SQL> SELECT Department.Name AS "Department",
  2         AVG(Product.PriceAS "Average Price"
  3  FROM Product
  4     INNER JOIN ProductCategory
  5     ON Product.ProductID = ProductCategory.ProductID
  6     INNER JOIN Category
  7     ON ProductCategory.CategoryID = Category.CategoryID
  8     INNER JOIN Department
  9     ON Category.DepartmentID = Department.DepartmentID
 10  GROUP BY Department.Name;
Hardware
         7.68

QA
         3.75

Software
        24.99

SQL>
SQL> drop table department;
SQL> drop sequence DepartmentIDSeq;
SQL> drop table Product;
SQL> drop table ProductCategory;
SQL> drop table Category;
SQL> drop sequence CategoryIDSeq;
SQL> drop sequence ProductIDSeq;

   
    
    
    
  














Related examples in the same category
1.AVG: returns the average of all NOT NULL values passed into the function
2.Syntax: AVG ([DISTINCT]|[ALL] )
3.find the average of the uniquely priced cars: use the DISTINCT modifier:
4.To find out the average price for each manufacturer: use the GROUP BY clause
5.AVG function and NULLs
6.Average salary in Toronto: AVG with where clause
7.AVG(DISTINCT salary)
8.GROUP BY clause and AVG() function
9.Having with avg
10.Use avg and nvl together
11.Use NVL in set statement
12.Average Price Per Departments Having More Than 3 Products
13.Employees whose salary is more than average salary
14.Employees whose salary is more than average salary(use having clause only)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.