Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Having Trouble With a Query
Message
De
17/02/2006 13:58:07
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01097119
Message ID:
01097157
Vues:
11
Thanks Sergey! The first one didn't work (I assume because of the fact that there can be NULLs in the FK), but the second one worked fine. I needed to tweak it a little more, because I also need to join the second table to further weed out some criteria, but I figured that out easily enough. You gave me a push in the right direction, thanks!!! =)

~~Bonnie



>Bonnie,
>
>SELECT * FROM mytable mt1
>  WHERE NOT EXISTS ( SELECT * FROM mytable mt2
>        WHERE mt2.FK = mt1.FK and mt2.date > mt1.date)
>-- or
>SELECT * FROM mytable mt1
>  WHERE date = ( SELECT MAX(date) FROM mytable mt2
>        WHERE mt2.FK = mt1.FK )
>
>>I have two tables, one with a date, PK and a FK to the second table. I need to select the latest date and the two keys for every group of FK's. If my data is this:
>>
>>Date         PK          FK
>>1/1/2001     1           1
>>1/5/2002     2           1
>>2/2/2001     3           1
>>6/6/2005     4           2
>>1/1/2005     5           2
>>
>>Then I'd like my output to look like this:
>>
>>Date         PK          FK
>>1/5/2002     2           1
>>6/6/2005     4           2
>>
>>I'm having trouble coming up with a query to do this. Any ideas?
>>
>>TIA,
>>~~
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform