Solved

Need help with Access Query

Posted on 2014-07-25
6
302 Views
Last Modified: 2014-07-25
Experts, can you please help me with this. I need a query to produce the IDTPartNumber and Description from the Equipment Table. And then I need the sum of Qty from the SalesEquipment table for the IDTPartNumber. And I need to find these based on year. So SalesDate Between #1/1/2014# and #7/25/2014#.
Screen1.png
0
Comment
Question by:Basicfarmer
6 Comments
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 40219624
It looks as if there should be a many-to-many relationship here, with SalesEquipment as the junction table.  It would be best to set up this relationship, to ensure referential integrity.  Can you post the database (or a cut-down version of it)?  I would like to have the actual tables to work with, but it might be possible to just use the appropriate fields from all three tables in a query, or if that doesn't work, make a date-limited query based on the Sales table, and link that to the other two tables to get the fields you need.  Then either do a Sum on Qty in that table, or save that query as a select query, and make a totals query based on it to get the sum.
0
 
LVL 36

Accepted Solution

by:
PatHartman earned 500 total points
ID: 40219632
Use the QBE to build the query.  It is a big help when you are unfamiliar with SQL syntax or just don't want to type it all out.

Add the three tables to the QBE.
Draw the join lines as indicated.
Select the desired fields.
Press the big sigma button to make this a Totals query.
Change "group by" to Sum for the Qty field
Add Between #1/1/2014# and #7/25/2014# to the criteria cell for the SalesDate column
Change "group by"  to Where for the SalesDate column (if you don't do this, you'll get a sum for each day in the range rather than a sum for the whole range.

PS - my personal preference is to NEVER use "ID" as the name of the primary key.  I suggest always using a meaningful name.
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 40219644
SELECT
   e.IDTPartNumber
   ,MAX(e.Description) [Part Description]
   ,SUM(se.QTY) [Total Quantity]
FROM
  Equipment e
JOIN
  SalesEquioment se
         ON se.EquipmentID = e.ID
JOIN
   Sales s
       ON s.ID = se.SaleID
WHERE s. SalesDate Between #1/1/2014# and #7/25/2014#
GROUP BY
e.IDTPartNumber
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40219653
try this query

SELECT Sales.SalesDate, Equipment.IDTPartNumber
FROM (Sales INNER JOIN SalesEquipment ON Sales.ID = SalesEquipment.SaleID) INNER JOIN Equipment ON SalesEquipment.EquipmentID = Equipment.ID
WHERE Sales.SalesDate Between #1/1/2014# And #7/25/2014#
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40219657
oops, sorry i did not see the SUM part...
0
 

Author Closing Comment

by:Basicfarmer
ID: 40219759
Perfect Instructions Thanks. I got errors on the other suggested solutions.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question