Oracle SQL to sum over rows

I need an output that displays a row for each customer and includes a column for each order placed by customer even those who placed no orders.

Additionally, the Order number column needs to display a comma delimited list of order numbers for customers who placed more than one order.

I have a Customer table and an Orders table
Customer cols: CustName,  CustID
Orders cols: OrderDate,  OrderID, CustID(foreign key)
--Select all customer rows even if there are customers who have no orders
SELECT Customer.CustID, Customer.CustName,
Orders.OrderID,Orders. OrderDate
FROM Customer
LEFT OUTER JOIN Customer  
ON CUSTOMER.CustID= Orders.CustID
WHERE CUSTOMER.CustID>= 11 AND CUSTOMER.CustID<=16
ORDER BY CUSTOMER.CustID;
CustID      CustName      OrderID            OrderDate
11      Mike            27            10/17/2013
11      Mike            32            10/20/2013
11      Charles                        
11      David            37            10/17/2013
11      James            27            10/26/2013

I need to combine the orders for Mike in a single row and display the order ID’s in a single cell
Where there is more than one order per customer.
CustID      CustName      OrderID            OrderDate
11      Mike            27,32            10/20/2013   (most recent order date for that customer.)
11      Charles                        
11      David            37            10/17/2013
11      James            27            10/26/2013

What are the recommended steps ?

Create a recordset, then step through then recordset.

Build a single sql statement that sums the rows when there are multiple orders per customer.

Thank you.
DovbermanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tomas Helgi JohannssonCommented:
Hi!

This sql query does this for you

SELECT Customer.CustID, Customer.CustName, LISTAGG(Orders.OrderID, ',') OrdersIDs , LISTAGG(Orders. OrderDate, ',') WITHIN GROUP ( ORDER BY Orders.OrderDate DESC) OrderDates
FROM Customer
LEFT OUTER JOIN Customer  
ON CUSTOMER.CustID= Orders.CustID
WHERE CUSTOMER.CustID>= 11 AND CUSTOMER.CustID<=16
GROUP BY CUSTOMER.CustID,CUSTOMER.CustName 
ORDER BY CUSTOMER.CustID;

Open in new window


More on the LISTAGG function
http://docs.oracle.com/cd/E11882_01/server.112/e10592/functions089.htm


Regards,
     Tomas Helgi
0
DovbermanAuthor Commented:
I will try and give you feedback.  I need to rebuild the statement based on our own tables.  I used the generic customer order scenario for simplification.
0
DovbermanAuthor Commented:
I tried this.

Customer is work item (w) T_PSA_MASTER, Orders is letters (l) T_PSA_LETTERS

*/

SELECT T_PSA_MASTER.psa_master_seqnum, w.PLANNING_STATUS, LISTAGG(L.PSA_LETTERS_SEQNUM, ',')
"LetterIDs" , l.OrderDate
FROM T_PSA_MASTER w
LEFT OUTER JOIN T_PSA_LETTERS l  
ON w.psa_master_seqnum= l.psa_master_seqnum
WHERE w.psa_master_seqnum >= 11 AND w.psa_master_seqnum <=16
GROUP BY w.psa_master_seqnum,w.PLANNING_STATUS  
ORDER BY w.psa_master_seqnum;
/

Error
ORA-02000 missing WITHIN keyword
"LetterIDs" is highlighted in red.

What am I missing?

Thanks
0
Tomas Helgi JohannssonCommented:
Hi!

You will have to do it like this

SELECT T_PSA_MASTER.psa_master_seqnum, w.PLANNING_STATUS, LISTAGG(L.PSA_LETTERS_SEQNUM, ',')
WITHIN GROUP (ORDER BY L.PSA_LETTERS_SEQNUM ) "LetterIDs" , l.OrderDate
FROM T_PSA_MASTER w
LEFT OUTER JOIN T_PSA_LETTERS l  
ON w.psa_master_seqnum= l.psa_master_seqnum
WHERE w.psa_master_seqnum >= 11 AND w.psa_master_seqnum <=16
GROUP BY w.psa_master_seqnum,w.PLANNING_STATUS  

Open in new window


Regards,
    Tomas Helgi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DovbermanAuthor Commented:
Excellent.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.