473,554 Members | 3,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Query Showing Total Sales by Each Person and Each Product.

4 New Member
Hi There,

I'm trying to write a query that returns the total sames amount by each sales person for each product -- I'm having issues with the join sequence at this moment. Any help would be much appreciated!

Tables would look like this..

Products table
------------
ProductNum Name Price


SalesRep table
------------
PersonNum Name Region


Clients table
-----------
CustomerNum Company Region


Sales table
----------
OrderNUM CustomerNum PersonNum ProductNum TotalSales
Jul 31 '10 #1
2 14310
NeoPa
32,564 Recognized Expert Moderator MVP
You just need to focus on what you want as output first (the SELECT clause), then from there include all the tables involved in the FROM clause. At that point we consider how they connect together. This is fairly straightforward as logically they can only really connect one way. You'll see that in the design of the tables though - which FK fields connect to which PK fields.

Anyway, I expect you'd need something like :
Expand|Select|Wrap|Line Numbers
  1. SELECT   tSR.Name
  2.        , tSR.Region
  3.        , tP.Name
  4.        , SUM(tS.TotalSales) AS TotalProdSales
Now we know only three of the tables are even required - [Products] (tP), [SalesRep] (tSR) and [Sales] (tS). Aliases will become evident in the FROM clause :
Expand|Select|Wrap|Line Numbers
  1. FROM    ([Sales] AS tS INNER JOIN
  2.          [SalesRep] AS tSR
  3.   ON     tS.PersonNum = tSR.PersonNum) INNER JOIN
  4.          [Products] AS tP
  5.   ON     tS.ProductNum = tP.ProductNum
That's all the hard part done. Now we need to add the GROUP BY clause as we want this per Sales Rep and per Product :
Expand|Select|Wrap|Line Numbers
  1. GROUP BY tSR.Name
  2.        , tSR.Region
  3.        , tP.Name
Welcome to Bytes!
Jul 31 '10 #2
NeoPa
32,564 Recognized Expert Moderator MVP
A new question was posted in here which has been split across to CrossTab Problems. We don't encourage multiple questions in a single thread as it tends to cause all sorts of confusions and makes it less likely that things will work smoothly.
Aug 2 '10 #3

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

Similar topics

2
1503
by: Alun | last post by:
Hi Here's my problem. Let's say i have a table which records whenever a user logs on to a computer system. So someone logs on and it records something like Fred, 10th March 2004 19:54
4
5942
by: Richard | last post by:
In a manufacturing document control situation, a procedure revision table named has principal columns and and satellite data columns , , . A unique index and to ensure each procedure name has only unique revision numbers. A form has a listbox for selecting a procedure and revision for browsing, and a checkbox 'Show all'. When...
3
2924
by: penny111 | last post by:
Hi, I have created an MS Access database containing the following tables: Crime CrimeCommitted Criminal CriminalOrganization CriminalType HideoutType
6
4324
by: xian2 | last post by:
Hi All, I have created a query with 6 related tables as the record source. The query includes 4 fields from 4 of the tables: tblTourBookings TourStartDate tblAdditionalCosts
1
1726
by: silversubey | last post by:
I am using a query to total hours (Cltbudget.CBudhours) and dollars (CltBudget.CBudFee) Grouped by client names (Cltbudget.CBudCltName). here is the Query: SELECT CltBudget.CBudCltName AS Client , SUM(CltBudget.CBudfee) AS 'Total Dollars' , SUM(CltBudget.CBudhours) AS 'Total Hours' FROM VPM.dbo.CltBudget CltBudget, CltDue WHERE...
4
11619
by: wutang | last post by:
Create a program that displays the sum of the sales amounts made in each of four regions (North, South, East, West) during a three month period. The program should display the total sales made during the three months. 1. Complete the program by entering the C++ code that allows the user to enter four sets(one set for each region) of three sales...
5
1556
by: Sandretto | last post by:
Hey I've made an outstanding order database for the company I work for. I've created a query which calculates how many are delivered on each job number and how many are outstanding. Using tables - tblOrders ( This contains order information and assigns the job num which is primary key) tblDeliveries ( Contains all delivery information...
4
2406
by: dawn123 | last post by:
I have two tables.. I want to update one table with the count of each colume from the pervious table. I have been able to do it using the query bulider and when ran, it works but I want to take the SQL statment and copy it into my moduel however it won't work. It will not let me format the SQL statment and remove put the whole statement on one...
0
7584
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...
0
7783
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. ...
0
6128
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...
0
5143
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...
0
3546
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...
0
3535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1117
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
827
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.