Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Select question
Message
 
 
À
28/03/2001 11:57:19
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Divers
Thread ID:
00489490
Message ID:
00489504
Vues:
9
Ken,
You can use your query as a subquery and use a concatenated expression.
select fkey,color from table3 where fkey+convert(varchar(5),sortorder) 
   in (select fkey+convert(varchar(5),max(sortorder)) as 'ord' 
   from table3 group by fkey)
HTH.

>Hi,
>
>need a way to get the field associated with a grouping/aggregate field in a select ...
>
>Example:
>
>
>fkey     color           sortorder
>-----------------------------------
>1        Red             100
>1        White           101
>1        Blue            102
>2        Black           1001
>2        Blue            1002
>3        Purple          101
>3        Yellow          102
>3        Orange          103
>3        Blue            104
Now I want a result set with a record for each fkey with the color that has the maximum sortorder for that fkey. My result set should be:
>
>fkey     color
>-----------------------------------
>1        Blue
>2        Blue
>3        Blue
>
>I can get the max sortord with no problem via
select fkey, max(sortord) from mytable group by fkey
but as soon as I try to add the color field, it has to go into the group by clause, and I just get all the records back. Any easy way to get this in one select?
>
>TIA!
Larry Miller
MCSD
LWMiller3@verizon.net

Accumulate learning by study, understand what you learn by questioning. -- Mingjiao
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform