diff --git a/manifest b/manifest
index f847eb0805..c08ca1d77c 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sminor\sproblems\scaused\sby\sadding\sthe\srowid\sto\sthe\srecords\sin\sstat4.
-D 2013-08-12T09:29:04.530
+C Fix\sa\sbug\sin\scalculating\sthe\saverage\snumber\sof\sentries\sfor\skeys\snot\spresent\sin\sthe\ssqlite_stat4\stable.
+D 2013-08-12T11:21:10.969
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -157,7 +157,7 @@ F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
 F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc
 F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad
 F src/alter.c 2af0330bb1b601af7a7789bf7229675fd772a083
-F src/analyze.c 49729c117665fc927280c0bcd10bffa838ff29d1
+F src/analyze.c fd1bcb9bc4ca29cd36f60c620cc501c933048c28
 F src/attach.c 1816f5a9eea8d2010fc2b22b44f0f63eb3a62704
 F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34
 F src/backup.c 43b348822db3e4cef48b2ae5a445fbeb6c73a165
@@ -1106,7 +1106,7 @@ F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P 3a5e8ab7ddbe1d943b35ef329fe4e5a1bfdb0d9d
-R 61c9bc8ff2fa4dfd3e5854cb245baf85
+P 088d1ff94890ada50d43e6a366a58167ec5a8e96
+R 4b6821682980828caa2dcfc6f067a6e7
 U dan
-Z 74fdc1c4224404aea1a5d8ea05036c15
+Z 150d3ee65bd4be58d3670346ad74798d
diff --git a/manifest.uuid b/manifest.uuid
index 4bb020c7db..4e199eec40 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-088d1ff94890ada50d43e6a366a58167ec5a8e96
\ No newline at end of file
+ec3ffb174844406a6186c3dcc41b76d0331b502c
\ No newline at end of file
diff --git a/src/analyze.c b/src/analyze.c
index 07e2cbc20f..641d416aa1 100644
--- a/src/analyze.c
+++ b/src/analyze.c
@@ -1282,7 +1282,6 @@ static int loadStat4(sqlite3 *db, const char *zDb){
     char *zIndex;   /* Index name */
     Index *pIdx;    /* Pointer to the index object */
     int i;          /* Loop counter */
-    tRowcnt sumEq;  /* Sum of the nEq values */
     int nCol;       /* Number of columns in index */
 
     zIndex = (char *)sqlite3_column_text(pStmt, 0);
@@ -1304,13 +1303,27 @@ static int loadStat4(sqlite3 *db, const char *zDb){
     decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
 
     if( idx==pIdx->nSample-1 ){
+      IndexSample *aSample = pIdx->aSample;
       int iCol;
       for(iCol=0; iCol<pIdx->nColumn; iCol++){
+        tRowcnt sumEq = 0;        /* Sum of the nEq values */
+        int nSum = 0;             /* Number of terms contributing to sumEq */
         tRowcnt avgEq = 0;
         tRowcnt nDLt = pSample->anDLt[iCol];
-        if( nDLt>idx ){
-          for(i=0, sumEq=0; i<idx; i++) sumEq += pIdx->aSample[i].anEq[iCol];
-          avgEq = (pSample->anLt[iCol] - sumEq)/(nDLt - idx);
+
+        /* Set nSum to the number of distinct (iCol+1) field prefixes that
+        ** occur in the stat4 table for this index before pSample. Set
+        ** sumEq to the sum of the nEq values for column iCol for the same
+        ** set (adding the value only once where there exist dupicate 
+        ** prefixes).  */
+        for(i=0; i<(pIdx->nSample-1); i++){
+          if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
+            sumEq += aSample[i].anEq[iCol];
+            nSum++;
+          }
+        }
+        if( nDLt>nSum ){
+          avgEq = (pSample->anLt[iCol] - sumEq)/(nDLt - nSum);
         }
         if( avgEq==0 ) avgEq = 1;
         pIdx->aAvgEq[iCol] = avgEq;