Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can this be done in one SQL Select?
Message
From
09/02/2005 09:50:33
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00984864
Message ID:
00985157
Views:
23
>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform