473,465 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SUM(DISTINCT) OF INNER JOIN

I have two tables recording the sales transactions with purchase_id the
common key

Table 1 (Recording total amounts and customer info)

Purchase_Id (Primary Key)
Sales_Total (Total sales amount)
Customer_Id
..... (Other fields)

Table 2 (Recording the detail transaction items)
Purchase_Id (Primary Key)
Product_Id (Product information)
...... (Other fields)

Here is a sample record set of my table

Table 1
Purchase_Id Sales_Total Customer_Id
1 200 1
2 300 4
3 500 4

.................

Table 2
Purchase_Id Product_Id
1 1001
1 1005
2 1004
3 1001
3 1002
3 1003
.....................
I need to generate the sales report and caculate the total sales of a
particular customer

Joining these two tables result in
Purhcase_Id Sales_Total Customer_Id Product_Id
1 200 1 1001
1 200 1 1005
2 300 4 1004
3 500 4 1001
3 500 4 1002
3 500 4 1003

I am using Mysql Database, I did select statement like this:
SELECT Customer_Id, SUM(Sales_Total) FROM Table 1 INNER JOIN Table 2
USING (Purchase_ID) WHERE Product_Id > 1000 GROUP BY Customer_Id

Result In
Customer_Id SUM(Sales_Total)
1 400
4 1800

Which is not correct, I want to count on UNIQUE (DISTINCT) Purchsae_Id
and no need to duplicately sum up on rows.

I.e, the result should be
Customer_Id SUM
1 200
4 800

What should I do???
Thanks.

Dec 14 '05 #1
1 5267
"Edward" <ed**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I need to generate the sales report and caculate the total sales of a
particular customer


See my reply to your crosspost on comp.databases.mysql.

Bill K.
Dec 15 '05 #2

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

Similar topics

4
by: David Link | last post by:
Hi, Why does adding SUM and GROUP BY destroy performance? details follow. Thanks, David Link s1.sql: SELECT t.tid, t.title, COALESCE(s0c100r100.units, 0) as w0c100r100units,
15
by: U N Me | last post by:
I have a continuous form that lists records from a query. One of the columns is an amount field, In the footer of the form I place a text box that displays the total (sum) of the amount. The...
2
by: Anne Heddal | last post by:
In the first line I'm trying to count the the unqiue values of the ID column, but I'm getting syntax error, any idea how to format the distinct count? SELECT Count(test.ID) AS IDCOUNT FROM...
3
by: | last post by:
need some help with some sql I can't seem to get the syntax right. I've tried several but to no avail the one below gets me the closest but I can't get the guardians records pulled. SELECT...
5
by: David | last post by:
Hi, I have an asp page which prints data as follows: The sum in brackets is a calculated result on the page, SQ * RS2("SelectorQty"), a variable SQ which holds a qty multiplied by a record...
10
by: Rudolf Bargholz | last post by:
Perhaps some kind soul could help me out with an SQL I have been trying all day to get to work, where one colum is just not summing up the way I want it to. I have the following data GRP_SEQ ...
0
by: NeoGeo | last post by:
I have a problem with a SQL SELECT query. As far as my research goes i figured out that UNIQUE is used when you have one column that you whant unique and DISTINCT is used when you have more than one...
8
Scott Price
by: Scott Price | last post by:
Hello Gurus of the Access world... This one has me stumped. I am developing a meal planning database for large functions (1000 people +- for up to 4 days). I have stored base recipes, as well as...
4
AccessIdiot
by: AccessIdiot | last post by:
I have three tables used in a query. Table A: Has field with name of species. Table B: Has the number of specimens caught per catch. Table C: Has measurements of specimens. I have generated...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.