473,809 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ 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 5289
"Edward" <ed**********@g mail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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
3866
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
7977
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 data in the form is linked from a one-to-many table relationship. In this instance, I really can't sum the value from the one's table since if there were 3 records in the many table for a record id, the value would be summed 3 times. IOW, I may be...
2
10539
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 (select distinct test.ID FROM test), Sum(test.balance) AS BAL, test.statement, billing.YYMM FROM billing INNER JOIN test ON billing.ID = test.ID WHERE (((test.invoice) Like "X*") AND ((test.due)=0) AND ((test.collections)=0)) GROUP BY test.statement,...
3
1501
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 SUM(tuition) FROM child INNER JOIN guardian ON guardian.ID = child.child_ID I would like the results to show the records from the guardian table as well tables: GUARDIAN
5
1649
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 value 96 (50) 104 (50) 96 (100)
10
10005
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 ITEM_SEQ NR_ITEMS PERSONS_SEQ 1 A 2 aa 1 A 2 bb
0
2843
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 column that you want to all be unique. But i have a query where i want just some of the columns to be unique and some not. Here is the full query (It's in a stored procedure): set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER PROCEDURE...
8
1974
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 stored historic data from past years. To generate a shopping list report I am using this sql query: SELECT tblIngredient.ItemName, Sum(tblRecipeIngredient.Quantity) AS TotQuantity, tblMeasureType.MeasureDesc, tblIngredient.RecipeMeasureID,...
4
2492
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 the query in design view to get the min, max, and avg length of two particular species. The query should also report the number of specimens. For some reason I am not getting the right numbers. For example, I know for a fact that there were 84...
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.