Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to Select TOP nn for all codes in one pass
Message
From
06/01/2007 05:04:39
 
 
To
05/01/2007 02:49:10
Rasheed Al Rasheed
Riyadh Armed Forces Hospital
Riyadh, Saudi Arabia
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01181698
Message ID:
01183111
Views:
23
>Thanks Tamer
>
>My fault that I changed in the previous version. It is working just fine and will save me valuable processing time.
>
>appreciated.

Warning, this select is not optimizable, it is O(N²),
with many codes and dates the execution time
and intermediate file's size grow and it become unusable .

A XBase solution is best for me
clear

CREATE CURSOR Test (iCode I, dDate D, nPrice N(6,2))


DATE=DATE(2006,01,01)

FOR c=1000 TO 1050
	FOR k=0 to 100
		INSERT INTO Test VALUES (m.c , m.date+m.k, M.K)
	NEXT
NEXT
INDEX ON iCode TAG iCode 


t1=SECONDS()
SELECT B1.iCode, B1.dDate, B1.nPrice ;
  FROM Test B1 ;
    JOIN Test B2 ;
      ON B1.iCode = B2.iCode ;
      AND B1.dDate <= B2.dDate ;
  GROUP BY 1, 2, 3 ;
  HAVING CNT(*) <= 3 ;
  INTO CURSOR MostRecent

SELECT iCode, Avg(nPrice ) ;
  FROM MostRecent ;
  GROUP BY iCode;
	INTO CURSOR MostRecent
? 'SQL',SECONDS()-T1

t1=SECONDS()

SELECT iCode,nPrice avgPrice FROM Test WHERE .F. INTO CURSOR MostRecent2 READWRITE
SELECT Test
INDEX ON BINTOC(iCode)+DTOS(dDate) TAG GROUPING DESC
iCode=iCode
DO WHILE NOT EOF()
	CALCULATE NEXT 3 WHILE iCode=m.iCode AVG(nPrice) TO avgPrice
	INSERT INTO MostRecent2 VALUES (m.iCode,m.avgPrice)
	LOCATE REST FOR iCode#m.iCode
	iCode=iCode
ENDDO
DELETE TAG GROUPING
? 'xbase',SECONDS()-T1
Previous
Reply
Map
View

Click here to load this message in the networking platform