473,409 Members | 2,063 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,409 software developers and data experts.

SQL question on summary

Pal
Hi,

I have the following data to query. I do not how to get the output
(below) in a single query statement in Access '02. No problem in
using separately 3 queries. Can anyone help here?

Thanks in advance.

INPUT
-----
Table 1 (Customer)
Customer Category amount
1 A 20
1 B 30
1 C 35
2 A 40
2 B 45

Table 2 (Supplier)
Supplier Category amount
1 A 100
1 B 200
2 A 100
2 B 200

I want the following output:
OUTPUT
------

Category TotalDemandedCustomer TotalProvidedSupplier
A 60 200
B 75 400
C 35 0
Pal
Nov 13 '05 #1
3 1133
This SQL does the basics of what you ask:

SELECT [table 1].category, Sum(Nz([Table 1].[amount])) AS
customer_demanded, Sum(Nz([Table 2].[amount])) AS supplier_demanded
FROM [table 1] INNER JOIN [table 2] ON [table 1].category = [table
2].category
GROUP BY [table 1].category;

However this doesn't achieve exactly what you want, because it only
gives a result for categories that appear in both tables. To get all
results from one table (e.g. [Table 1]) and matching results from Table
2, use this instead:

SELECT [table 1].category, Sum(Nz([Table 1].[amount])) AS
customer_demanded, Sum(Nz([Table 2].[amount])) AS supplier_demanded
FROM [table 1] LEFT JOIN [table 2] ON [table 1].category = [table
2].category
GROUP BY [table 1].category;

Ideally, you actually have a separate table that contains ALL
categories, then you could make sure all categories had an amount
against them, eg:

SELECT Categories.category, Sum(Nz([Table 1].[amount])) AS
customer_demanded, Sum(Nz([Table 2].[amount])) AS supplier_demanded
FROM (Categories LEFT JOIN [Table 1] ON Categories.category = [Table
1].category) LEFT JOIN [Table 2] ON Categories.category = [Table
2].category
GROUP BY Categories.category;

Good luck!

------------
LoopyNZ
------------

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #2
Pal
Hi,

Thanks. But I just tried your first suggest and it does not work.
The totals are double counted as before.

Pal


LoopyNZ <Lo*****@NOguruSPPAM.net.nz> wrote in message news:<41**********************@news.newsgroups.ws> ...
This SQL does the basics of what you ask:

SELECT [table 1].category, Sum(Nz([Table 1].[amount])) AS
customer_demanded, Sum(Nz([Table 2].[amount])) AS supplier_demanded
FROM [table 1] INNER JOIN [table 2] ON [table 1].category = [table
2].category
GROUP BY [table 1].category;

However this doesn't achieve exactly what you want, because it only
gives a result for categories that appear in both tables. To get all
results from one table (e.g. [Table 1]) and matching results from Table
2, use this instead:

SELECT [table 1].category, Sum(Nz([Table 1].[amount])) AS
customer_demanded, Sum(Nz([Table 2].[amount])) AS supplier_demanded
FROM [table 1] LEFT JOIN [table 2] ON [table 1].category = [table
2].category
GROUP BY [table 1].category;

Ideally, you actually have a separate table that contains ALL
categories, then you could make sure all categories had an amount
against them, eg:

SELECT Categories.category, Sum(Nz([Table 1].[amount])) AS
customer_demanded, Sum(Nz([Table 2].[amount])) AS supplier_demanded
FROM (Categories LEFT JOIN [Table 1] ON Categories.category = [Table
1].category) LEFT JOIN [Table 2] ON Categories.category = [Table
2].category
GROUP BY Categories.category;

Good luck!

------------
LoopyNZ
------------

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #3
Pal,

I'm not sure what the "double-counting as before" is that you mention -
you didn't refer to it in your original post.

Can you post the exact SQL you used and the results it returned?

------------
LoopyNZ
------------

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.