Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Troublesome SQL construction
Message
From
19/12/2006 18:37:58
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
19/12/2006 17:31:58
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01179041
Message ID:
01179074
Views:
14
This message has been marked as a message which has helped to the initial question of the thread.
>I'm trying to build a cursor that shows all values from the parent cursor, along with the lookup value of the most recent child record. I'm running into difficulties because not all parent records have child records.
>
>For this set of data, where [Small], [Medium], and [Large] are values from a lookup table.
>
Customer1 has 3 orders; Oct 1 for 5000 [Small], Nov 1 for 12000 [Large], Dec 1 for 7000 [Medium]
>Customer2 has 0 orders;
>Customer3 has 2 orders; Nov 1 for 7000 [Medium], Dec 1 for 11000 [Large]
>Customer4 has 1 orders; Oct 1 for 4000 [Small]
>Customer5 has 0 orders;
>
>The result set should look like:
>
Customer1  Dec 1   [Medium]
>Customer2  {//}    []
>Customer3  Dec 1   [Large]
>Customer4  Oct 1   [Small]
>Customer5  {//}    []
>
>So far, my results have either wound up with 3 Customers(cutting out 2 and 5) or 8 Customers because of the duplicates. I know this shouldn't be that hard, but I'm not seeing what I missed.
select t1.customer, t2.date, t3.size ;
 from customer t1 ;
  left join orders t2 on t1.custID = t2.custID ;
     and t2.date = (select max(date) from orders o1 where o1.cust_id = t2.custid ) ;
  left join lookup t3 on t2.amount = t3.amount
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform