Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2012
    Posts
    2

    Unanswered: Select count through tables

    Hello,

    I need help with a query.
    I have 2 tables.
    - table1: Customer ID - Customer name
    - table 2: Customer ID - Orders

    I would like to run a query that reports for each Customer the total number of orders made.

    Customer name Orders Count
    Jhon Smith 10
    Jhon Doe 22

    Thanks

  2. #2
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    Join these tables, GROUP BY customer ID.

  3. #3
    Join Date
    Dec 2011
    Posts
    25

    Count() Group BY

    CREATE TABLE CUSTOMER(CUSTOMER_ID NUMBER , CUSTOMER_NAME VARCHAR2(20));

    INSERT INTO CUSTOMER (CUSTOMER_ID,CUSTOMER_NAME) VALUES (1,'Jhon Smith ');
    INSERT INTO CUSTOMER (CUSTOMER_ID,CUSTOMER_NAME) VALUES (2,'Jhon Doe');



    CREATE TABLE CUSTOMER_ORDER(CUSTOMER_ID NUMBER , ORDER_ID VARCHAR2(20));

    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (1,'50');
    Insert into CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) values (2,'51');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (1,'25');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (2,'28');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (1,'26');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (2,'52');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (1,'66');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (2,'47');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (1,'32');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (1,'8');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (1,'5');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (1,'7');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (2,'7');
    INSERT INTO CUSTOMER_ORDER (CUSTOMER_ID,ORDER_ID) VALUES (2,'8');



    SELECT CUSTOMER_ID, CUSTOMER_NAME FROM CUSTOMER ;

    CUSTOMER_ID CUSTOMER_NAME
    ---------------------- --------------------
    1 JHON SMITH
    2 Jhon Doe

    SELECT CUSTOMER_ID, ORDER_ID FROM CUSTOMER_ORDER ;

    CUSTOMER_ID ORDER_ID
    ---------------------- --------------------
    1 50
    2 51
    1 25
    2 28
    1 26
    2 52
    1 66
    2 47
    1 32
    1 8
    1 5
    1 7
    2 7
    2 8

    14 rows selected


    SELECT CUS.CUSTOMER_ID, CUS.CUSTOMER_NAME, COUNT(*) TOTAL_ORDERS FROM CUSTOMER CUS
    INNER JOIN CUSTOMER_ORDER ORD
    ON CUS.CUSTOMER_ID = ORD.CUSTOMER_ID
    GROUP BY CUS.CUSTOMER_ID ,CUS.CUSTOMER_NAME ;



    CUSTOMER_ID CUSTOMER_NAME TOTAL_ORDERS
    ---------------------- -------------------- ----------------------
    2 Jhon Doe 6
    1 JHON SMITH 8

  4. #4
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  5. #5
    Join Date
    Jan 2012
    Posts
    2
    Thanks you folks for your help and detailed answer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •