Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
UPDATE SQL, How to use with correlated query.
Message
De
06/05/2005 18:07:22
 
 
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
Database:
Visual FoxPro
Divers
Thread ID:
01011797
Message ID:
01011835
Vues:
16
>Thanks Fabio, how should be the cAgent cursor to use the AutoJoin procedure?
>
The big problem is the speed,
with this Update schema, VFP is not able to use the tables's indexes
CREATE CURSOR cMovs (iid int autoinc, iAgentID int, yAmount y, dDate d)

RAND(-1)
FOR lnCounter=1 TO 100
  INSERT INTO cMovs (iAgentID, yAmount, dDate) ;
     VALUES ((RAND()*10)-1, RAND()*1000000,date(2005,01,01)+ (RAND()*45))
ENDFOR
INDEX ON iAgentID TAG AGENT_RK


CREATE CURSOR cAgent (iid int autoinc, yMonth y, nMonth int,  nYear int)
FOR lnCounter=1 TO 10
  INSERT INTO cAgent (yMonth, nMonth, nYear) ;
   VALUES (0,1,2005)
ENDFOR

INDEX ON iid TAG AGENT_PK

CLEAR
SYS(3054,11)
?
* with inefficient solution
UPDATE A ;
  SET yMonth = cMovs.yAmount ;
  FROM cAgent A;
    JOIN (SELECT iAgentID , MONTH(dDate) nMonth , YEAR(dDate) nYear , SUM(yAmount) yAmount ;
                  FROM cMovs group by 1,2,3) cMovs;
         ON  cMovs.iAgentID = A.iID;
         AND cMovs.nMonth   = A.nMonth ;
         AND cMovs.nYear    = A.nYear;
  WHERE A.nMonth = 1 AND A.nYear=2005

BROWSE
?

* OR YOU OPTIMIZE IT WITH a literal correlation
UPDATE A ;
  SET yMonth = cMovs.yAmount ;
  FROM (SELECT iAgentID, SUM(yAmount) yAmount ;
                  FROM cMovs ;
                  WHERE MONTH(dDate)=1 AND YEAR(dDate)=2005;
                  group by 1) cMovs;
  		JOIN cAgent A;
        ON  cMovs.iAgentID = A.iID;
 		WHERE  A.nMonth = 1 AND A.nYear=2005
 
BROWSE 

?
* or use a subquery with a autojoin
 UPDATE A ;
  SET yMonth = cMovs.yAmount ;
  FROM (SELECT iAgentID, SUM(yAmount) yAmount ;
                  FROM cMovs JOIN cAgent ;
                  ON cMovs.iAgentID = cAgent.iID ;
                    AND MONTH(dDate)= cAgent.nMonth ;
                    AND YEAR(dDate) = cAgent.nYear;
                    WHERE cAgent.nMonth = 1 AND cAgent.nYear=2005 ;
                  group by iAgentID) cMovs;
  		JOIN cAgent A;
        ON  cMovs.iAgentID = A.iID
 BROWSE
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform