473,513 Members | 2,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query help - Join on date, group by date

11 New Member
Hi there,

I have 2 tables, shopOrder and wholesaleOrder, each have the following structure:

Expand|Select|Wrap|Line Numbers
  1. orderID, orderDate, orderTotal
I want to generate a report with the following structure, between two given dates:
Expand|Select|Wrap|Line Numbers
  1. Date, Number of Wholesale Orders, Wholesale Orders Total, Number of Shop Orders, Shop Orders Total
Currently, my query only returns order data from the shopOrder table if orders exist in the wholesaleOrder table for that date, otherwise just wholesaleOrder data is returned. Ideally, i'd like to end up with with a query that can output data as shown in the table below (ie. shop order data is shown for a date even if there are no wholesale orders for that date and vice versa):

[HTML]<table>
<thead>
<tr>
<th>Date</th>
<th>Number of Wholesale Orders</th>
<th>Wholesale Orders Total</th>
<th>Number of Shop Orders</th>
<th>Shop Orders Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>11/05/2008</td>
<td>1</td>
<td>100.00</td>
<td>2</td>
<td>50.00</td>
</tr>
<tr>
<td>13/05/2008</td>
<td>0</td>
<td>0</td>
<td>4</td>
<td>130.00</td>
</tr>
<tr>
<td>14/05/2008</td>
<td>3</td>
<td>37.00</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>[/HTML]

Any help with the SQL query would be greatly appreciated! Here is my current query:

Expand|Select|Wrap|Line Numbers
  1. SELECT wholesaleOrder.orderDate,
  2. SUM(wholesaleOrder.orderTotal) AS wholesaleOrdersTotal,
  3. COUNT(wholesaleOrder.orderID) AS wholesaleOrdersQuantity
  4. SUM(shopOrder.orderTotal) AS shopOrdersTotal,
  5. COUNT(shopOrder.orderID) AS shopOrdersQuantity 
  6. FROM wholesaleOrder
  7. LEFT JOIN shopOrder ON shopOrder.orderDate=wholesaleOrder.orderDate 
  8. WHERE wholesaleOrder.orderDate BETWEEN $date1 AND $date2 
  9. OR shopOrder.orderDate BETWEEN $date1 AND $date2
  10. GROUP BY wholesaleOrder.orderDate
Kind Regards,
Alex
Jun 2 '08 #1
3 1745
code green
1,726 Recognized Expert Top Contributor
shop order data is shown for a date even if there are no wholesale orders for that date and vice versa
I don't quite understand. This sounds like you just want all the data from both tables.
Expand|Select|Wrap|Line Numbers
  1. orderID, orderDate, orderTotal
There is no database structure here.
It is simply two flat files with shopOrder and wholesaleOrder data.
A common field is needed between the two tables for a JOIN.
As suggested orderDate is not a common field.
It may be a UNION you are looking for
Jun 3 '08 #2
ajcolburn
11 New Member
Hi there,

Thanks for your reply,

I haven't explained this very well, I aim to generate a report which combines totals from the shopOrder and wholesaleOrder tables by date, for a given date span. I can achieve this with nested queries and php loops, although I would be interested to know whether this could be done with a single query.

Here is a screenshot of the intended output:

Report Screenshot

Kind Regards,
Alex
Jun 3 '08 #3
Atli
5,058 Recognized Expert Expert
Hi.

Have you considered executing this as two statements, joined by a UNION.
Like:
Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(total), count(id) FROM table1
  2. WHERE dateField BETWEEN start AND end
  3. UNION
  4. SELECT SUM(total), count(id) FROM table2
  5. WHERE dateField BETWEEN start AND end
  6.  
That would give you two rows, one for each table.
Jun 3 '08 #4

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

Similar topics

0
2436
by: Doug Reese | last post by:
hello, i have what seems to me a very common operation i'm performing. i need to find the balance on an invoice. i was not having any problems until the production server was upgraded to mysql...
4
2962
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and...
9
3109
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
6
17892
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by...
3
1387
by: Rob | last post by:
I have a problem I can't seem to solve although it should be easy. I've done these many times before. But I keep getting an exception, "Line 1: Incorrect syntax near 'GetFinancialData'." This...
0
2312
by: rdnews | last post by:
Dear group, I got excellent help here a while back in optimizing a slow query, so I thought I'd try again with another one... I have a small table, around 3000 rows with two columns. One is a...
7
3371
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
24
19861
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...
1
3683
by: bruce24444 | last post by:
First of all I'm new to the forum and am working on my first database. So far I think I've done not too bad but have hit a stumbling block for which I'm not sure how to get around. What I have is...
7
2831
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.