Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sql update code to count records in a field
Message
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP2
Miscellaneous
Thread ID:
01390750
Message ID:
01390981
Views:
63
Here's an example how to update nIncrement w/o SCAN
* Preparing test data
CREATE CURSOR Test (cName C(20), nIncrement I, nCount I)

INSERT INTO Test (cName) VALUES ("One")


INSERT INTO Test (cName) VALUES ("Four")
INSERT INTO Test (cName) VALUES ("Four")
INSERT INTO Test (cName) VALUES ("Four")
INSERT INTO Test (cName) VALUES ("Four")


INSERT INTO Test (cName) VALUES ("Three")
INSERT INTO Test (cName) VALUES ("Three")
INSERT INTO Test (cName) VALUES ("Three")

INSERT INTO Test (cName) VALUES ("Other")

INSERT INTO Test (cName) VALUES ("Five")
INSERT INTO Test (cName) VALUES ("Five")
INSERT INTO Test (cName) VALUES ("Five")
INSERT INTO Test (cName) VALUES ("Five")
INSERT INTO Test (cName) VALUES ("Five")

INSERT INTO Test (cName) VALUES ("Two")
INSERT INTO Test (cName) VALUES ("Two")

*INDEX ON cName TAG cName
* Test data is ready

* Create a temp cursor and calculate nIncrement 
SELECT *, CAST(RECNO() AS i) AS rnOrig, CAST(0 AS I) AS rn, CAST(0 AS I) AS rnmin ;
	FROM Test ;
	ORDER BY cName, rnOrig ;
	INTO CURSOR crsTemp READWRITE

REPLACE ALL rn WITH RECNO()
	
UPDATE crsTemp ;
		SET rnMin = (SELECT MIN(rn) FROM crsTemp t2 WHERE t2.cName = crsTemp.cName)

REPLACE ALL nIncrement WITH RECNO() - rnmin + 1

* Write nIncrement back into original table
SELECT Test
SET ORDER TO 

SELECT crsTemp
* Set relataion on RECNO()
SET RELATION TO rnOrig INTO Test
REPLACE Test.nIncrement WITH nIncrement FOR FOUND("Test")

* View result
SELECT Test
BROWSE LAST NOWAIT 
>
>My god how fast!
>You drink milk fox!
>
>
>Ok i tryng with scan and replace
>
>Big thank Sergey
>
>
>>You can do ncount with SQL Update . For 'increment' you can use SCAN+REPLACE
>>
>>UPDATE mytable SET ncount = dt1.ncount
>>	FROM mytable 
>>	JOIN ( SELECT cnomres, COUNT(*) AS ncount FROM mytable GROUP BY cnomres) dt1
>>	ON dt1.cnomres = cnomres
>>
>>>
>>>Thank for help.
>>>
>>>if i want in the same command increment ( cf attachment nincrement.jpg ) ..
>>>it 's possible ?
>>>
--sb--
Previous
Reply
Map
View

Click here to load this message in the networking platform