Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Query not working right
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00726732
Message ID:
00726738
Views:
18
This message has been marked as the solution to the initial question of the thread.
Hi Nick,

Thre's no field in the second table that defines what is then last record. First, you've to add such field and than use it in the query.
* Assuming that the last record is defined by physical order of records
SELECT *, RECNO() AS rn ;
  FROM table2 ;
  INTO CURSOR crs21
* Get Last Records for each ID
SELECT id, max(rn) AS maxrn ;
  FROM crs2 ;
  GROUP BY 1 ;
  INTO CURSOR crs22

SELECT Table1.id, Table1.vname, crs21.Name ;
  FROM Table1 INNER JOIN crs21 ;
    ON Table1.id = crs21.id ;
     WHERE crs21.rn IN ( SELECT maxrn FROM crs22)


>I have a one to many table. For each entry in table1 I want to find the last entry in table2.
>
>Here is my query and and sample tables:
>
>
>SELECT Table1.id, Table1.vname, Table2.Name ;
>FROM Table1 INNER JOIN Table2 ;
>ON Table1.id = Table2.id ;
>GROUP BY 1
>
>
>
>
>
>table1:
>id   vname
>============
>100 veh1
>200 veh2
>
>
>
>table2:
>id   name
>=============
>100 "James"
>200 "tammy"
>100 "john"
>100 "cindi"
>200 "angie"
>200 "thomas"
>100 "austin"
>
>
>The Results I am trying to get is:
>
>100 veh1 "austin"
>200 veh2 "thomas"
>
>but the problem is that the query above doesn't guarantee the last record from table2. Is there a way I can rewrite this query so that I will alway get the last record from the 100 and 200 series entered in table2.
>
>Thanks
>Nick Patel
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform