Ok, thanks.  So let me explain the query number 2 as this is the more
difficult to write.  So I have a list of stocks, this table contains the
price of all of the stocks at the open and close date.  Ok, now we have a
ratio from query (1) that returns at least a very rough index of the daily
performance of a given stock, with each ratio representing the stock's
performance in one day.  Now we need to average this with the same stock's
ratio every day, to get a total average for each stock contained in the
database.  Now I would simply like to find a ratio like this that represents
the average of every stock in the table and simply find the greatest ratio.
Sorry about the lousy explanation before, is this a bit better?

Here is an example if needed.

Say we have a stock by the name of YYY

I know, due to query 1 that stock YYY has a abs(close-open)/open price ratio
of for example, 1.3 on Dec 1 and (for simplicity let's say we only have two
dates) and Dec 2 the ratio for YYY is 1.5. So the query averages and gets
1.4.  Now it needs to do this for all of the stocks in the table and sort by
increasing ratio.

  
Thanks.


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Richard Huxton
Sent: Tuesday, June 29, 2004 3:38 AM
To: Bill
Cc: [EMAIL PROTECTED]
Subject: Re: [PERFORM] Query performance

Bill wrote:
> Ok....so here lies the output of oclh (i.e "\d oclh")  
> 
>                     Table "public.oclh"
>  Column |         Type          |           Modifiers           
> --------+-----------------------+-------------------------------
>  symbol | character varying(10) | not null default ''
>  date   | date                  | not null default '0001-01-01'
>  open   | numeric(12,2)         | not null default '0.00'
>  close  | numeric(12,2)         | not null default '0.00'
>  low    | numeric(12,2)         | not null default '0.00'
>  high   | numeric(12,2)         | not null default '0.00'
> Indexes: symbol_2_oclh_index btree (symbol, date),
>          symbol_oclh_index btree (symbol, date)

Well, I'm not sure why the two indexes on the same columns, and I'm not 
sure it makes sense to have defaults for _any_ of the columns there.

So - you want:
1. ratio = abs(closing-opening)/opening
2. average = all the ratios of each day of each stock
3. Highest average

Well, I don't know what you mean by #2, but #1 is just:

SELECT
   symbol,
   "date",
   abs(close - open)/open AS ratio
FROM
   oclh
GROUP BY
   symbol, date;

I'd probably fill in a summary table with this and use that as the basis 
for your further queries. Presumably from "yesterday" back, the 
ratios/averages won't change.

-- 
   Richard Huxton
   Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
      joining column's datatypes do not match

Reply via email to