Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sql update code to count records in a field
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP2
Divers
Thread ID:
01390750
Message ID:
01390981
Vues:
71
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--
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform