dbTalk Databases Forums  

VFP 6,7 - row number in SQL SELECT

comp.databases.xbase.fox comp.databases.xbase.fox


Discuss VFP 6,7 - row number in SQL SELECT in the comp.databases.xbase.fox forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Iks
 
Posts: n/a

Default VFP 6,7 - row number in SQL SELECT - 01-13-2004 , 10:36 AM






Hi,

I would like to produce calculated (in SQL SELECT) column with output
row number.

Please look on following code:

**
private counter
counter=0

SELECT myIncrement(@counter) as No, * from some_table


function myIncrement
parameters tnCounter
tnCouter=tnCouter+1
return tnCouter

Query result looks like this:

No Field
--------
2 .....
3 .....
4 .....
5 .....
... .....

'No' always starts from 2 (in cases I tested). Problem is that I do not know
is this a rule that function is called n+1 times and I can correct this by
SELECT myIncrement(@counter)-1 as No - or should I try to find other
solution.


Best Regards,
Iks





Reply With Quote
  #2  
Old   
Rick Bean
 
Posts: n/a

Default Re: VFP 6,7 - row number in SQL SELECT - 01-13-2004 , 12:33 PM






Iks,

VFP always runs the query for the first record to determine the field types and sizes to create the cursor properly (unless it's strictly a filtered "view"), before it does the real SELECT. About all you can do in this case is to start "counter" at -1!

Rick

"Iks" <oopsik (AT) polbox (DOT) com> wrote

Quote:
Hi,

I would like to produce calculated (in SQL SELECT) column with output
row number.

Please look on following code:

**
private counter
counter=0

SELECT myIncrement(@counter) as No, * from some_table


function myIncrement
parameters tnCounter
tnCouter=tnCouter+1
return tnCouter

Query result looks like this:

No Field
--------
2 .....
3 .....
4 .....
5 .....
.. .....

'No' always starts from 2 (in cases I tested). Problem is that I do not know
is this a rule that function is called n+1 times and I can correct this by
SELECT myIncrement(@counter)-1 as No - or should I try to find other
solution.


Best Regards,
Iks





Reply With Quote
  #3  
Old   
Stefan Wuebbe
 
Posts: n/a

Default Re: VFP 6,7 - row number in SQL SELECT - 01-14-2004 , 03:24 AM




"Iks" <oopsik (AT) polbox (DOT) com> schrieb im Newsbeitrag
news:bu16q2$24bl$1 (AT) news2 (DOT) ipartners.pl...
Quote:
Hi,

I would like to produce calculated (in SQL SELECT) column with output
row number.
Hello,
Alternatively you can get it by doing a self-join, to
avoid the custom function (or is it a "cross join"?)

hth
-Stefan

* sql_dynamic_rank.prg
CREATE CURSOR testtable (cName C(10), nNumber Int)
INSERT INTO testtable VALUES ("James",19)
INSERT INTO testtable VALUES ("Adam",5)
INSERT INTO testtable VALUES ("Richard",1)

SELECT count(*) as rank, ;
T1.cName, T1.nNumber ;
FROM testtable T1 ;
JOIN testtable T2 ;
on T1.nNumber >= T2.nNumber ;
GROUP BY 2, 3 ;
ORDER BY 1 ;
INTO CURSOR resultByNum
BROWSE LAST NOWAIT

SELECT count(*) as rank, ;
T1.cName, T1.nNumber ;
FROM testtable T1 ;
JOIN testtable T2 ;
on T1.cName >= T2.cName ;
GROUP BY 2, 3 ;
ORDER BY 1 ;
INTO CURSOR resultByChar
BROWSE LAST NOWAIT
* eop



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.