Solved

Mysql query syntax

Posted on 2013-11-11
6
292 Views
Last Modified: 2013-11-11
TableA
--------------------
productID,Name
1,hat
2,cup
3,pen

TableB
----------------------
orderID, productID, qtyOrdered, custID
1,1,10,1
2,1,10,1
3,3,30,1

TableC
----------------
custID, Name
1,jones

select a.productName,c.Name, sum(b.qtyOrdered) as qSum from TableA a
left join TableB b on a.productID=b.productID
left join TableC c on b.custID=c.custID
group by a.productID

returns
Hat,20,Jones
Pen,30,Jones

I want the following result
Hat,20,Jones
Cup,0,Null
Pen,30,Jones

What I want is to return a quantity of zero where no orders were placed for a product?

How do I do this ?
0
Comment
Question by:joomla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:warddhooghe
ID: 39638492
Assuming you dont want to left join your customer's table (C) the solution could be to do the sum in a subquery inner joining B and C, then left/right join it with your products table (A)
0
 

Author Comment

by:joomla
ID: 39638502
Can you give me an example of how to construct that statement ?
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39638591
There's a function in MySQL called COALESCE that you can use to return 0 instead of NULL, but the bigger issue is with the logic in your SQL. You are grouping on ProductID, but showing the customer ID, so if 10 different customer each ordered 10 particular products, you show a SUM of 100 for the first customer - not sure that's what you want (you may need to group on the customer ID as well):

SELECT a.productName, COALESCE(SUM(b.qtyOrdered),0) AS qSum, c.Name FROM TableA a
LEFT JOIN TableB b ON a.productID=b.productID
LEFT JOIN TableC c ON b.custID=c.custID
GROUP BY a.productID, b.custID

Open in new window

0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Author Comment

by:joomla
ID: 39638599
thanks, I didn't know of 'coalesce' and appreciate you pointing out the need for extra grouping.

with this syntax you provided, I would assume that record in tableA without matching record in tableB would not be shown ?
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 39638617
No. Because of the LEFT join, all records from TableA will be shown (even if they have no matching record on TableB)- that's the whole point of a LEFT join.

In your original question, that's what you asked for:

What I want is to return a quantity of zero where no orders were placed for a product?
0
 

Author Closing Comment

by:joomla
ID: 39638625
Brilliant
thanks for your help
regards
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question