Re: Add calculated fields from one table to other table

From: roopa perumalraja <roopabenzer(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org, Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
Subject: Re: Add calculated fields from one table to other table
Date: 2006-11-01 06:49:15
Message-ID: 20061101064915.80874.qmail@web50812.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi Richard,

Thanks a lot.

I am sending you the create statement of tables & few insert statements as well. Hope this helps to solve the problem.

CREATE TABLE ticks
(
tick_id int8 NOT NULL DEFAULT nextval(('ticks_s'::text)::regclass),
ric varchar(30) NOT NULL,
tick_date date NOT NULL,
tick_time time NOT NULL,
price float8,
volume int4,
CONSTRAINT ticks_pkey PRIMARY KEY (tick_id),
)
WITHOUT OIDS;

CREATE TABLE times
(
times_time time NOT NULL,
count int4,
CONSTRAINT times_pkey PRIMARY KEY (times_time)
)

selct statement of ticks table
ric | tick_date | tick_time | price
-----+------------+--------------+-------
A | 2006-04-04 | 00:00:55.023 | 4.05
AA | 2006-04-04 | 00:00:55.023 | 9.05
A | 2006-04-04 | 00:00:59.023 | 6.05
A | 2006-04-04 | 00:01:00.023 | 5.05
ABC | 2006-04-04 | 00:01:00.509 |12.00
ABI | 2006-04-04 | 00:01:03.511 |13.00
AA | 2006-04-04 | 00:01:08.023 | 6.05
ABT | 2006-04-04 | 00:01:08.518 | 3.06
ABT | 2006-04-04 | 00:01:09.518 | 7.06

select statement of times table
times_time
-----------
00:00:00
00:01:00
00:02:00


I want the query result to look
ric | times_time | count | avg_price
----+------------+-------+-----------
A | 00:00:00 | 2 | 5.05
AA | 00:00:00 | 1 | 9.05
ABC | 00:00:00 | 0 |
ABI | 00:00:00 | 0 |
ABT | 00:00:00 | 0 |
A | 00:01:00 | 1 | 5.05
AA | 00:01:00 | 1 | 6.05
ABC | 00:01:00 | 1 |12.00
ABI | 00:01:00 | 1 |13.00
ABT | 00:01:00 | 2 | 5.06

I am really thankful to you.

Regards
Roopa

Richard Broersma Jr <rabroersma(at)yahoo(dot)com> wrote:
> Hi Richard,
>
> Thanks a lot. I still am not able to get the result for all the rics in the ticks table but I
> am able to get the result for a particular ric.
>
> Can you help me with getting the result for all the rics in the ticks table
>
> Thanks
> Roopa

Could you send create table statements for the tables you are working on, and a few insert
statements for each table to have sample data. then show what you want the query results to look
like.

But from what you stated in your previous emails here is what I gather: maybe it might work?

select tk.ric as ric,
tm.times_time as minute, --timestamps by minutes
count(tk.*) as ,
...
from times tm
left join ticks tk
on (tm.times_time = date_trunc('minutes', tk.time))
group by ric, minute
order by minute;


---------------------------------
We have the perfect Group for you. Check out the handy changes to Yahoo! Groups.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Ezequias Rodrigues da Rocha 2006-11-01 12:06:17 Re: Round Numeric Type
Previous Message Tom Lane 2006-11-01 06:38:25 Re: [HACKERS] Case Preservation disregarding case