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
04/01/2007 14:12:09
 
 
To
03/01/2007 15:49:08
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:
01182420
Views:
14
>Thanks for your response. I tried but still did not get what is needed maybe because I did not explain clearly.
>
>here is an extract of the data
> Code ---- Date ---- Price
> 1010 -- 20061223 -- 63.25
> 1010 -- 20061224 -- 63.00
> 1010 -- 20061225 -- 63.75
> 1010 -- 20061226 -- 63.00
> 1010 -- 20061227 -- 64.50
>
> 1020 -- 20061223 -- 130.75
> 1020 -- 20061224 -- 130.00
> 1020 -- 20061225 -- 130.50
> 1020 -- 20061226 -- 132.00
> 1020 -- 20061227 -- 131.75
>
> 1030 -- 20061223 -- 83.00
> 1030 -- 20061224 -- 82.75
> 1030 -- 20061225 -- 83.00
> 1030 -- 20061226 -- 85.00
> 1030 -- 20061227 -- 88.50
>
> 1040 -- 20061223 -- 76.25
> 1040 -- 20061224 -- 74.75
> 1040 -- 20061225 -- 74.75
> 1040 -- 20061226 -- 74.25
> 1040 -- 20061227 -- 76.25
>
>now say i want to get the top 3 dates in descending order in
>each group(code) ie
> 1010 -- 20061225 -- 63.75 for the first group
> 1010 -- 20061226 -- 63.00
> 1010 -- 20061227 -- 64.50
>and
> 1020 -- 20061225 -- 130.50 for the second group
> 1020 -- 20061226 -- 132.00
> 1020 -- 20061227 -- 131.75
>etc..
>
>I would grateful for any suggestions.
>

Here's the test code I just wrote using my query and your data:
CREATE CURSOR Test (iCode I, dDate D, nPrice N(6,2))

INSERT INTO Test VALUES (1010 , DATE(2006,12,23 ), 63.25)
INSERT INTO Test VALUES (1010 , DATE(2006,12,24 ), 63.00)
INSERT INTO Test VALUES (1010 , DATE(2006,12,25 ), 63.75)
INSERT INTO Test VALUES (1010 , DATE(2006,12,26 ), 63.00)
INSERT INTO Test VALUES (1010 , DATE(2006,12,27 ), 64.50)

INSERT INTO Test VALUES (1020 , DATE(2006,12,23) , 130.75)
INSERT INTO Test VALUES (1020 , DATE(2006,12,24 ), 130.00)
INSERT INTO Test VALUES (1020 , DATE(2006,12,25 ), 130.50)
INSERT INTO Test VALUES (1020 , DATE(2006,12,26 ), 132.00)
INSERT INTO Test VALUES (1020 , DATE(2006,12,27 ), 131.75)

INSERT INTO Test VALUES (1030 , DATE(2006,12,23 ), 83.00)
INSERT INTO Test VALUES (1030 , DATE(2006,12,24 ), 82.75)
INSERT INTO Test VALUES (1030 , DATE(2006,12,25 ), 83.00)
INSERT INTO Test VALUES (1030 , DATE(2006,12,26 ), 85.00)
INSERT INTO Test VALUES (1030 , DATE(2006,12,27 ), 88.50)

INSERT INTO Test VALUES (1040 , DATE(2006,12,23 ), 76.25)
INSERT INTO Test VALUES (1040 , DATE(2006,12,24 ), 74.75)
INSERT INTO Test VALUES (1040 , DATE(2006,12,25 ), 74.75)
INSERT INTO Test VALUES (1040 , DATE(2006,12,26 ), 74.25)
INSERT INTO Test VALUES (1040 , DATE(2006,12,27 ), 76.25 )

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 TenMostRecent
The only change from the earlier version of the query is to choose only 3 for each code instead of 10.

Tamar
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform