Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Select the highest value from each group?
Message
 
 
À
30/05/2005 16:51:42
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9
Divers
Thread ID:
01018590
Message ID:
01018596
Vues:
11
This message has been marked as a message which has helped to the initial question of the thread.
SELECT tb.cKey, tb.cValue FROM tblValues tb ;
	JOIN ( ;
		SELECT cKey, MAX(nPriority) MaxPriority ;
			FROM tblValues ;
			GROUP BY 1 ;
			) mt ;
		ON mt.cKey = tb.cKey ;
			AND mt.MaxPriority = tb.nPriority

* Another one that uses correlated subquery and works in VFP8 and earelier
SELECT tb.cKey, tb.cValue FROM tblValues tb ;
	WHERE tb.nPriority IN ;
		( SELECT MAX(nPriority) ;
			FROM tblValues tb2 ;
			WHERE  tb2.cKey = tb.cKey ) 
>Here's a puzzle for you SQL masters:
>
>I have the following table:
>
>
>create table tblValues ( ;
>  cKey       c( 32 ), ;
>  cValue     c( 16 ), ;
>  nPriority  i        ;
>)
>
>
>This table has many records with the same cKey value, but with differing values for nPriority. I would like to run a query that returns the cKey, cValue records that have the highest nPriority values for each set of matching cKey's.
>
>Example data (remove blank records when importing)
>
>
>apple, red,     8
>apple, yellow,  2
>apple, brown,   0
>
>corn,  green,   4
>corn,  yellow,  9
>corn,  brown,   2
>
>eggs,  pink,    6
>eggs,  purple,  7
>eggs,  white,   8
>
>
>Here's the result set I'm looking for:
>
>
>apple, red
>corn,  yellow
>eggs,  white
>
>
>The way I do this now is to create an index on cKey + padl( nPriority ), set order descending, then scan the source table inserting the first unique cKey record I find and insert it into a cursor.
>
>Seems to me there may be a more effective way to accomplish this?
>
>Any takers?
>
>Malcolm
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform