473,433 Members | 1,827 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,433 software developers and data experts.

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

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 14276
NeoPa
32,556 Expert Mod 16PB
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,556 Expert Mod 16PB
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
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
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...
3
by: penny111 | last post by:
Hi, I have created an MS Access database containing the following tables: Crime CrimeCommitted Criminal CriminalOrganization CriminalType HideoutType
6
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
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...
4
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...
5
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 -...
4
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...
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
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...
1
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...
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
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: 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
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.