Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting Missing Numbers Between Two Values in a Table
Message
From
11/11/2017 01:44:47
 
 
To
10/11/2017 14:00:01
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01655491
Message ID:
01655495
Views:
62
>Hi,
>Kindly Guide.
>
>I have a cursor with field fObjectno N(20) which contains Numbers Like 1,4,14,15,12,17,2,3,7,10,13 etc on Random basis.
>
>The lowest is 1 and Highest is 17 in the above case. I need to create a table of Missing Numbers from the above series. I.e The Missing Numbers between the Lowest and Highest in the Table.
>
>Is there any Simple way or SQL to do it in speed.
>
>The Range may be Between 1 to 10000 and it is not necessary that 1 will in the top or 10000 will be at the bottom of the cursor.
>
>I need Missing Figures in a Cursor (Difference in Numbers=1)

There is a way to do it via SQL - maybe not the fastest, but relatively simple and maintainable:
* Assume your cursor with random values has an alias RandomVals
LOCAL ;
	lnMinVal ;
	, lnMaxVal ;
	, lnIx

* Get lowest and highest values:
* Option 1: index RandomVals:
SELECT RandomVals
INDEX ON fObjectNo TAG fObjectNo

GO TOP
m.lnMinVal = RandomVals.fObjectNo
GO BOTTOM
m.lnMaxVal = RandomVals.fObjectNo

* Option 2: don't want to index RandomVals:
*!*	CALCULATE ;
*!*		MIN( fObjectNo ) ;
*!*		, MAX( fObjectNo ) ;
*!*		TO ;
*!*			m.lnMinVal ;
*!*			, m.lnMaxVal ;
*!*		IN RandomVals

* Create another cursor with all possible values, both present and missing:
CREATE CURSOR AllVals ( iVal I )

* Populate the cursor with all possible values between m.lnMinVal and m.lnMaxVal
FOR m.lnIx = m.lnMinVal TO m.lnMaxVal STEP 1
	INSERT INTO AllVals ( iVal ) VALUES ( m.lnIx )

ENDFOR

* Optional - index AllVals to help speed up the final SELECT - SQL:
SELECT AllVals
INDEX ON iVal TAG iVal

* Get missing values:
SELECT ;
	iVal ;
	FROM AllVals ;
	WHERE iVal NOT IN ;
		( SELECT fObjectNo FROM RandomVals ) ;
	INTO CURSOR MissingVals ;
	ORDER BY iVal

* Cleanup:
USE IN SELECT( "AllVals" )
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform