Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can this be done in one SQL Select?
Message
De
09/02/2005 09:50:33
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00984864
Message ID:
00985157
Vues:
25
>I need help in selecting records from the following table (simplified):
>
>CUST_ID
>ORD_DATE
>ORD_TIME
>
>For a given date (ORD_DATE), I need to select one order record per customer (CUST_ID) based on the time of order (ORD_TIME). For example, the Order Time should be the latest of the day.
>

While you can do this with one query in VFP 8 and earlier using a subquery, in VFP 9, it's much easier:
SELECT Orders.Cust_ID, Orders.Ord_Date, Orders.Ord_Time, ;
       <other fields> 
  FROM Orders ;
    JOIN (SELECT Cust_ID, Ord_Date, ;
          MAX(Ord_Time) AS Max_Time;
            FROM Orders OrdLast ;
            GROUP BY 1, 2) LastOrder ;
      ON Orders.Cust_ID = Last_Order.Cust_ID ;
      AND Orders.Ord_Date = Last_Order.Ord_Date ;
      AND Orders.Ord_Time = Last_Order.Max_Time
My testing shows this approach to be faster than any of the others.

Tamar
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform