If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Select count through tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-12, 08:01
eSimon eSimon is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
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
Reply With Quote
  #2 (permalink)  
Old 01-30-12, 08:47
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Join these tables, GROUP BY customer ID.
Reply With Quote
  #3 (permalink)  
Old 01-30-12, 23:43
Mr.Vivek Mr.Vivek is offline
Registered User
 
Join Date: Dec 2011
Posts: 15
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
Reply With Quote
  #4 (permalink)  
Old 01-30-12, 23:45
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
learning on schema hr
are you two in the same class?
__________________
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.
Reply With Quote
  #5 (permalink)  
Old 02-01-12, 05:50
eSimon eSimon is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks you folks for your help and detailed answer.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On