Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select First Row 2

Status
Not open for further replies.

foxup

Programmer
Dec 14, 2010
328
CA
Hi All,

I have DBF called (USS.DBF) with 3 fields called (City, Prov, Pcode) (no index set, but the records are already in order by 'PCODE'). Here are 7 sample records:

WESTWOOD, NJ, 07675
OLD TAPPAN, NJ, 07675
RIVER VALE, NJ, 07675
RIVERVALE, NJ, 07675
WOODCLIFF LAKE, NJ, 07677
WESTWOOD, NJ, 07677
WOODCLIFF LK, NJ, 07677

I would need to SELECT the "first" record of each DIST PCODE. So, my result would be:
WESTWOOD, NJ, 07675
WOODCLIFF LAKE, NJ, 07677

Would would I need for a "SELECT" command? Please help.


Thanks,
FOXUP!
 
As with many things needing to be done in VFP there are a number of ways to approach it.
Also note that the approach you select may vary likely depend on what you want to do with the results.

One way that I use often is to use the INDEX to find the first occurrence.
Code:
* --- Make copy of table so as to leave original table in its original form ---
SELECT *;
   FROM USS;
   INTO CURSOR Frst READWRITE

SELECT Frst
DELETE ALL
INDEX ON PCode UNIQUE TAG Uniq
RECALL ALL  && RECALL only those which match the Index Expression (those which are the First Occurrence)

* --- Now only the first occurrence of PCode will Not be Deleted ---
SELECT Frst
* --- < Do Whatever > ---

Note that if you were to use this approach you MIGHT need to pay attention to your SET DELETED ON/OFF setting
Code:
SET DELETED OFF && DELETED Records are VISIBLE
SET DELETED ON && DELETED Records are HIDDEN

Also note that others may offer their own approach which may work equally well or maybe vastly better.

Good Luck,
JRB-Bldr
 
I think this will do it. If not, it should give you the basic idea.

Code:
SELECT * ;
   FROM USS ;
     JOIN (SELECT MIN(RecNo()) AS nRec, PCode ;
            FROM USS ;
            GROUP BY PCode) csrMin ;
       ON USS.PCode = csrMin.PCode

Tamar
 
Hi JRB-Bldr & Tamar,

Both work great. Thanks to both you. Star for y'all! ;)


Thank you,
FOXUP !!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top