Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Records matching MAX(Date)
Message
From
16/08/2002 08:35:35
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00690278
Message ID:
00690300
Views:
15
>>>Hi
>>>
>>>Can I, in 1 SELECT statement, achieve the following:
>>>
>>>Table1 contains fields PK = Primary Key, FK = Foreign Key, Date = Date.
>>>
>>>I need to obtain the records from the table that have the highest date entry, do I need to select the dates first and then include those results in a sub-query in a 2nd Select?
>>>
>>>Any ideas?
>>>
>>>Thanks
>>>Kev
>>
>>Kevin,
>>No you don't have to do 2 SQLs. max() returns just one record so you could use that as a scalar value in 'where'. ie:
>>
>>where ddate = (select max(...) ...)
>>
>>A sample that demonstrates this (from FoxyClasses tips) :
>>
>>create cursor myGrouper (Ino i, ddate d, irec i)
>>for ix = 1 to 20
>>	insert into myGrouper ;
>>		values (int(rand()*5), date()-int(rand()*100), ix)
>>endfor
>>select * from myGrouper order by 1,2
>>
>>* This one is the tip *
>>select * from myGrouper A where ddate = ;
>>(select max(ddate) from myGrouper B where a.ino = b.ino) ;
>>order by ino
>>
>>First a cursor created. Ino represents the FK and irec represents PK. Records are inserted where ddate is random (dates for a particular ino are out of order).
>>First SQL (select * from myGrouper order by 1,2) is there just to show how would be the dates if listed in order.
>>Cetin
>
>That's a simple case. If you would have several max date records, you would have several records.
>
>But what if you need to find only one (or few) records with maximum date for each particular group, say, by Town?

If you're searching for 'Nadya' and there are several 'Nadya' you'd get all of them. If you want to search for 'Nadya Nosonovsky' then be more specific and use 'and' to include 'Nosonovsky'.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform