UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Sorting with an aggregate function    
 
   
gimboid13
post Nov 21 2005, 10:24 PM
Post #1

UtterAccess Member
Posts: 35
From: Australia



My table has name and date fields. Both are text fields. There can be multiple dates for a name. I need to write a query to return the most recent date for each name, with any null value date counting as the highest date.

I managed to sort the data in the table by date, with null values first, using 2 queries:

SELECT * from myTable;
SELECT * FROM myQuery ORDER BY IIf([Date] Is Null,99999,Val([Date])) DESC;

Can I do something similar to get just the most recent date for each name, with any null values as the most recent date, using an aggregate or other function?
Go to the top of the page
 
+
Peter46
post Nov 22 2005, 06:11 AM
Post #2

UtterAccess VIP
Posts: 7,399
From: Oadby Leics, UK



I don't see what your first query adds to the process.

This query gives you the initial answer.
SELECT namefield, max( IIf([Date] Is Null,99999,clng([Date]))) as Seq FROM mytable Group BY namefield
Go to the top of the page
 
+
truittb
post Nov 22 2005, 07:38 AM
Post #3

Retired Moderator
Posts: 13,544
From: Texas (Is there anywhere else?)



Further refine the query by using the Nz() function instead of the IIf(), and don't use Date as the name for a field it is a Reserved word and will cause you troubles.

SELECT namefield, Max(Nz(dtmDate,Date()) as Seq FROM mytable Group BY namefield
Go to the top of the page
 
+
gimboid13
post Nov 22 2005, 06:46 PM
Post #4

UtterAccess Member
Posts: 35
From: Australia



Thank you both.

(IMG:http://www.utteraccess.com/forum/style_emoticons/default/thanks.gif)
Go to the top of the page
 
+
truittb
post Nov 22 2005, 06:51 PM
Post #5

Retired Moderator
Posts: 13,544
From: Texas (Is there anywhere else?)



You are welcome.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 25th May 2013 - 06:40 PM