473,406 Members | 2,745 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,406 software developers and data experts.

Filtering to only show last balance

Hello,
I have two tables that have a relationship based on a customer ID. The first table Customer has:

Customer ID
Name
Address
etc

Accounts Table has:
Customer ID
Sale
Payment
Balance
Date
Notes
etc

The Accounts table has all the transactions for all sales. So for customer 116, I could have 5 entries for sale, payment and balance.


I need to build a query for a report that looks at the table and does the following
Sort by customer name
Show the last balance for the customer based on the last or most recent date to the current date.

I know how to link tables, but I would need guidance on where to put this code if I can do this.

Thanks
Oct 23 '06 #1
2 1830
MMcCarthy
14,534 Expert Mod 8TB
Open a new query in design view. Don't add any tables then switch the view to sql. Copy and paste in the code below. Check the table names as I assumed Customer and Accounts. Also checkthe field names are correct. Then save and run the query.


SELECT C.Name, A.Sale, A.Payment, A.Balance, Max(A.[Date]) As LastDate FROM Customer C INNER JOIN Accounts A ON C.CustomerID=A.CustomerID GROUP BY C.Name, A.Sale, A.Payment, A.Balance;

Once this query is done you will be able to view it in access.
Oct 23 '06 #2
NeoPa
32,556 Expert Mod 16PB
If you want the items associated with the latest date (Payment; Balance; etc) you will need a subquery (at least that's the only way I can think of that you can).
Subqueries always look complicated if done within SQL but they needn't be if the basic structure is understood.
The simplest form of subquery in SQL is :-
Expand|Select|Wrap|Line Numbers
  1. SELECT subQ.* FROM (SELECT * FROM DataSet) AS subQ
Which is equivalent to
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM DataSet
Anyway, another easier way to do it is to create another query, saved in the database like any other. I always cheat anyway and design it in the graphic window then copy / paste it into the SQL where it's required.

For this problem I think you need a subquery going something like :-
Expand|Select|Wrap|Line Numbers
  1. SELECT [Customer ID], Sale, Max([Date]) AS LastDate
  2. FROM Accounts
  3. GROUP BY [Customer ID], Sale
The main query should have an INNER JOIN link from Accounts to this subquery to select only those records which match the last date.
Oct 24 '06 #3

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

Similar topics

15
by: Ridimz | last post by:
When use ifstream, how do I ignore the last line of a file if it doesn't contain any information? Thanks in advance, Ridimz
3
by: Alex Ayzin | last post by:
Hi, I have a problem that might be easy to solve(possibly, I've just overlooked an easy solution). Here we go: I have a dataset with 2 datatables in it. Now, I need to do the following: if...
3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
5
by: Richard | last post by:
Hi, I have a form that take some time to load due to many comboboxes and at least 8 subforms. When I filter or sort the main form I get an error message and then Access shuts down. They ask if...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
1
by: samotek | last post by:
Filter the last date only I am trying t0 filter only the last date of the query,but i cannot manage that. My query is the following : SELECT Sum(.liters) AS SumOfLiters, orders.invoicedate...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
0
by: mkarki2 | last post by:
I am doing a likned list program in C. I am not really good at it and just started. But I am stuck how to pass structure in the function correctly. this is the driver file #ifndef DRIVER_C...
3
by: panjap | last post by:
hi, i was wondering could anyone help me with a query i have created. i have sucessfully worked out the total amount od prodcuts sold in a shop. However i want a query to show the total sold of one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...

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.