Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Help with SQL Statement
Message
 
 
To
01/05/2002 19:23:24
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00651662
Message ID:
00651677
Views:
18
>Hi,
>
>Table A is parent-> child with table B
>A has PKey and MyName fields
>Table B has a PKey, FKey (to A) and MyDate fields
>
>in VFP I can do
select A.pkey, B.Pkey, max(MyDate) from a join b on b.fkey = a.pkey group by a.pkey
>and get the record in B with the latest date.


You're mistaken about that. The B.Pkey value comes from the last record in the group not from the record with the MAX MyDate. You'll get correct result only if the records in the b.fkey group are ordered physicaly by MyDate in the B table.

>in SQL Server, however, I must group on both A.PKey, and B.Pkey - so question - how can I get from SQL Server a result set like
>A.Pkey B.Pkey where the second field is the key of the record related to A with the latest date?

Try
select A.pkey, B.Pkey, b.MyDate 
     from a join b on b.fkey = a.pkey 
         WHERE b.mydate = ( SELECT max(MyDate) FROM b bb 
             WHERE bb.fkey = a.pkey)  
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform