473,480 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Select first row in multi group query

9 New Member
Pardon me if this question already been asked before.

I tried to sort a query to meet my desire. Here is the example:
Expand|Select|Wrap|Line Numbers
  1. Date       Customer  Supplier   Price
  2. 31-Oct-09  Charlie   Australia  $ 100
  3. 20-Oct-09  Omega     Singapore   $ 50
  4. 4-Oct-09   Charlie   Australia   $ 20
  5. 5-Oct-09   Omega     Singapore   $ 60
  6. 15-Oct-09  Charlie   China      $ 200
  7. 20-Oct-09  Charlie   China      $ 300
My goal is to get latest price for each customer and supplier. Which the result should be:
Expand|Select|Wrap|Line Numbers
  1.   Date     Customer  Supplier   Price
  2. 31-Oct-09  Charlie   Australia  $ 100
  3. 20-Oct-09  Omega     Singapore   $ 50
  4. 20-Oct-09  Charlie   China      $ 300
I tried to descending the date order and set Last on Date and Price at the Total row. But it doesn't effect a thing.

Please help me on this case.
Regards,
Eko/Indonesia
Oct 19 '09 #1
5 12956
ajalwaysus
266 Recognized Expert Contributor
This is not one of my stronger areas, but I do know of one way of getting the results you desire. It will require 2 queries.

1. Assuming the data is stored in a table called "Customer", this is the first query:
Expand|Select|Wrap|Line Numbers
  1. SELECT Max(Customer.Date) AS MaxOfDate, Customer.Customer, Customer.Supplier
  2. FROM Customer
  3. GROUP BY Customer.Customer, Customer.Supplier;
2. Assuming the first query is named "qryMaxCustomer", this is the second query, which should return the data you desire:
Expand|Select|Wrap|Line Numbers
  1. SELECT qryMaxCustomer.MaxOfDate, qryMaxCustomer.Customer, qryMaxCustomer.Supplier, Customer.Price
  2. FROM Customer INNER JOIN qryMaxCustomer ON (Customer.Supplier = qryMaxCustomer.Supplier) AND (Customer.Customer = qryMaxCustomer.Customer) AND (Customer.Date = qryMaxCustomer.MaxOfDate);
Let me know if you have any questions.

-AJ
Oct 19 '09 #2
NeoPa
32,556 Recognized Expert Moderator MVP
For that sort of query I normally use a technique where I join the fields together in a string; find the maximum; then extract the data I'm interested in from the resultant string and convert it if required.

In this case that would translate to :
Expand|Select|Wrap|Line Numbers
  1. SELECT   Max([Date]) AS MaxDate,
  2.          [Customer],
  3.          [Supplier],
  4.          Val(Mid(Max(Format([Date],'yyyymmddHHnnss') &
  5.                      [Price]),15)) AS LastPrice
  6.  
  7. FROM     [SomeTable]
  8.  
  9. GROUP BY [Customer],
  10.          [Supplier]
Oct 19 '09 #3
eko99312
9 New Member
Dear AJ,

Thank you so much for the answer.
Since I don't understand SQL, I tried to apply your method by using query design view, and it works very well. Wow using 2 queries, why didn't I think of that.

And for NeoPa, I appreciate your answer and I'll learn more about your method.

Regards,
Eko
Oct 20 '09 #4
NeoPa
32,556 Recognized Expert Moderator MVP
SQL is fun. Remember though, if you're still quite new to it, that every QueryDef (saved query in Access) has SQL associated with it. Simply select View / SQL View and View / Design View to switch between two different, but matching, views of the same QueryDef. Each view allows design changes to it. This means you can always Copy the SQL of any QueryDef to be posted for instance. It also means that SQL already copied can be Pasted into the SQL View of a query to change it completely to reflect that new SQL. Very powerful concept.
Oct 20 '09 #5
NeoPa
32,556 Recognized Expert Moderator MVP
Another point to ponder is that AJ's solution can be combined into a single query if you use a subquery (See Subqueries in SQL).

Expand|Select|Wrap|Line Numbers
  1. SELECT tC.Customer,
  2.        tC.Supplier,
  3.        tC.Date,
  4.        tC.Price
  5.  
  6. FROM   Customer AS tC INNER JOIN
  7.     (
  8.     SELECT   [Customer],
  9.              [Supplier],
  10.              Max([Date]) AS MaxDate
  11.  
  12.     FROM     [Customer]
  13.  
  14.     GROUP BY [Customer],
  15.              [Supplier]
  16.     ) AS subQ
  17.   ON   (tC.Customer=subQ.Customer)
  18.  AND   (tC.Supplier=subQ.Supplier)
  19.  AND   (tC.Date=subQ.MaxDate)
PS. I'm very glad AJ posted this technique as I've been using the other for so long I forgot about this (more standard/typical) one. I prefer my one for one or two fields, but when there are a number of them then this method wins hands down (& I forgot to use it recently - Kicks self).
Oct 20 '09 #6

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

Similar topics

5
1958
by: Steve | last post by:
I'm trying to do a multi table query that displays all the info I need in one array, and I'm having problems figuring out how to do it. The two tables are product and vendor. A vendor can be a...
10
14021
by: Randell D. | last post by:
Folks, I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no...
4
4635
by: Pasquale | last post by:
Hello, I wondering if there is a way to dynamically update a select list with javascript from a database query without having to reload the page to update the list?
14
1739
by: kiqyou_vf | last post by:
Hello all, I'm trying to figure out how to search a "key_words" column, that contain key words (obviously) for that row. An example of what one of the key_words contains is: "home interior...
8
21068
by: Evan Smith | last post by:
During a routine performance check using an event monitor, I discovered a class of query whose performance has me baffled. The monitor captured: SELECT * FROM EWM_CASE fetch first 1 rows only...
17
3776
by: Redbeard | last post by:
I am a newbie that is running Access 2003. I am trying to have a multi select listbox run a query that filter a form. I have tried several codes off the internet to try and just get the selected...
3
1612
by: lostdawg | last post by:
Hi, I am having trouble with the following query. I need to sort from a list of contacts the last date each was contacted. This is to be represented in days so for instance: 0-42 days...
1
1336
by: Cyprus106 | last post by:
So I've got about a million records that I'm sifting through with a SELECT statement. Right now I've been saying SELECT * FROM MYTABLE WHERE MYFIELD='GUIDE'... But I only want the first record it...
3
2740
by: IAuditor | last post by:
Access 2003, XP - I have a multi-table query that is losing data. 1 table is a lookup (Hierarchy), and the other 3 are virtual duplicates (all with a one-to-many relationship with the lookup) except...
0
7040
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
6905
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
7041
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,...
0
7080
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
6908
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
5331
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
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
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 ...
0
178
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...

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.