Optimal query suggestion needed

Поиск
Список
Период
Сортировка
Искать
От
Interzone
Тема
Optimal query suggestion needed
Дата
Msg-id
40D1AEB0.1010604@interzone.gr
Список
Дерево обсуждения
Optimal query suggestion needed Interzone <lists@interzone.gr>
Re: Optimal query suggestion needed Bruno Wolff III <bruno@wolff.to>
Re: Optimal query suggestion needed InterZone <lists@interzone.gr>
Re: Optimal query suggestion needed Bruno Wolff III <bruno@wolff.to>
Re: Optimal query suggestion needed Richard Huxton <dev@archonet.com>
Re: Optimal query suggestion needed InterZone <lists@interzone.gr>
Hi all,

I'm trying to create a view for a client, but I'm failing miserably so I 
thought I'ld ask for some help. The initial analysis was quite 
complicated, and that specific need never came up until recently, and 
unfortunately, changing the tables is probably not an option :(


Given the tables :

create table t0  (   code integer,   address varchar,   mun integer
)

create table t1 (   code integer,   pname varchar
);

create table t2  (   code integer,   t0_fk integer,   t1_fk integer,   avail bool
);


I want to create a view that will have:
from table t0 the elements "code", "address" and "mun"
from table t1 the elements "code" and "pname"
from table t2 the total number of elements, and the total number of 
elements where avail = true, for every value t0_fk (foreign key to t0) 
and t1_fk (foreigh key to t1).

After several attempts and changes as the requirements changed, I finaly 
came up with that :

select t0.code, t0.address, t0.mun, t1.code as t1code, t1.pname 
count(t2.code) as t2total, (select count(t2.code) as t2avail from t2 
where t2.avail = true and t2.t0_fk=t0.code and t2.t1_fk = t1.code) as 
t2avail from t0, t1, t2 where t2.t0_fk = t0.code and t2.t1_fk=t1.code 
group by t0.code, t0.address, t0.mun, t1.code, t1.pname

but it gives wrong results...
I tried several other queries that are extremely slow.

Any help will be greatly appreciated.

Thanks in advance


В списке pgsql-sql по дате отправления
От: Hannes Korte
Дата:
От: Richard Huxton
Дата:
FAQ