# Re: Query performance

 От: Bill Re: Query performance 29 июня 2004 г. , 18:31:50 200406291733.i5THXkfL031311@math.uchicago.edu (см: обсуждение, исходный текст) Re: Query performance  (Richard Huxton) Re: Query performance  (Richard Huxton) Re: Query performance  (Bruno Wolff III) pgsql-performance

Скрыть дерево обсуждения

Query performance  ("Bill", )
Re: Query performance  (Richard Huxton, )
Re: Query performance  ("Bill", )
Re: Query performance  (Richard Huxton, )
Re: Query performance  ("Bill", )
Re: Query performance  (Richard Huxton, )
Re: Query performance  (Bruno Wolff III, )
Re: Query performance  ("Bill", )
Re: Query performance  (Rod Taylor, )
Re: Query performance  ("Mischa Sandberg", )

```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:
[mailto:] On Behalf Of Richard Huxton
Sent: Tuesday, June 29, 2004 3:38 AM
To: Bill
Cc:
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