473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Selecting all records from a join, even when the value of one table is null

Hi there,

I've been struggling with a query for a while now, and would be grateful if anyone here could help. The database is for an invoicing system, with the following tables & fields involved in said query:

Invoice (id, orderDate)
Product (id, productName)
Invoice_Item (id, invoice_id, product_id, quantity)

Basically, I've been trying to write some SQL which returns a list of all products in the product table along with the total quantities of these products sold (in invoices) between two dates.

So far - my query returns a list of products where total quantity sold is greater than zero, but does not return a complete list of products.

My query so far is below, any help would be greatly appreciated!

Expand|Select|Wrap|Line Numbers
  1. SELECT productName, SUM(quantity) AS quantity
  2. FROM Invoice 
  3. INNER JOIN Invoice_Item ON Invoice.id=Invoice_Item.invoiceRecordID 
  4. WHERE unix_timestamp(orderDate) >= $startDate AND unix_timestamp(orderDate) <= $endDate 
  5. GROUP BY Product.id 
  6.  
Oct 7 '07 #1
4 1326
pbmods
5,821 Expert 4TB
Heya, ajcolburn.

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Datetimes don't have to be converted to Unix timestamps; you can just compare them as you would any other variable.

Incidentally, you never joined Products, so `Products`.`id` doesn't exist to this query. Did you mean to group by `Invoice_Item`.`productID` instead?

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         `productName`,
  3.         SUM(`quantity`) AS `quantity`
  4.     FROM
  5.     (
  6.             `Invoice`
  7.         LEFT JOIN
  8.             `Invoice_Item`
  9.                 ON
  10.                     `Invoice`.`id` = `Invoice_Item`.`invoiceRecordID`
  11.     )
  12.     WHERE
  13.         `orderDate`
  14.             BETWEEN
  15.                     `startDate`
  16.                 AND
  17.                     `endDate`
  18.     GROUP BY
  19.         `Invoice_Item`.`productID`
  20.  
Oct 7 '07 #2
Thanks pbmods,

That cleared a few things up> You're right - I missed out the Products table, your solution is great but I was trying to list all products from the products table regardless of whether they are related to any Invoice_Item records. So you'd have a complete 'stock list' with sales totals for each product alongside, even if a product's sales total is 0. Could you give any further assistance? Many thanks for your help so far.

Alex.
Oct 7 '07 #3
pbmods
5,821 Expert 4TB
Heya, Alex.

Make `Products` the first table in your join, and it will fetch every row from Products that matches the WHERE clause.
Oct 7 '07 #4
This didn't solve the problem of returning all rows from Product table, although the guide below helped me write the correct query in the end:

SQL Group By Techniques

My final query:

Expand|Select|Wrap|Line Numbers
  1. SELECT P.productName, P.id, SW.wholesaleUnits, SW.wholesaleOrders
  2. FROM Product P
  3. LEFT JOIN 
  4. (SELECT productID, SUM(quantity) as wholesaleUnits, COUNT(Invoice.id) AS wholesaleOrders 
  5. FROM Invoice_Item 
  6. INNER JOIN Invoice 
  7. ON Invoice_Item.invoiceRecordID=Invoice.id 
  8. WHERE orderDate BETWEEN $startDate AND $endDate    
  9. GROUP BY productID) SW
  10. ON P.id=SW.productID 
  11.  
Oct 8 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: AG | last post by:
I have a very big table with 20 million records DistinctProjectionKey which i join several times to different tables in this query. select distinct distinctprojectionkeyid,dpk.MarketID,...
1
by: Ramesh | last post by:
hi, I am selecting fields from three table for manupulating data and i want to display total number of records selected. But i am always getting -1 value, eventhough 1000 of records are selected....
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
4
by: CK | last post by:
Good Morning, I have a person table with personID. I have a personRate table with personID, rateID, and effectiveDate. I need to select fields from personRate, but I want the fields from the...
3
by: wgblackmon | last post by:
I'm currently running the following statement that is used in a Crystal Report. Basically, a record is returned when the T_PAYMENT.amount has a record in the database based on the value of the...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
4
by: J | last post by:
I am editing a pre-existing view. This view is already bringing data from 40+ tables so I am to modify it without screwing with anything else that is already in there. I need to (left) join it...
1
by: Vivienne | last post by:
Hi there This is a hard problem that I have - I have only been using sql for a couple of weeks and have gone past my ability level quickly! The real tables are complex but I will post a simple...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.