Re: [SQL] sub-select with aggregate

2003-01-15 Thread Tom Lane
[ followup on a gripe from October ] Tomasz Myrta <[EMAIL PROTECTED]> writes: > I want to perform query looking like this: > select > user_id, > a/sum_a as percent_a, > b/sum_b as percent_b > from > users join > (select > group_id, > sum(a) as sum_a, > sum(b) as sum_b >f

Re: [SQL] sub-select with aggregate

2002-10-24 Thread Tom Lane
Stephan Szabo <[EMAIL PROTECTED]> writes: > Basically, as I understand it, > select * from a,b where a.a=b.a and a.a=3; > isn't going to realize that b.a=3 and act as if > you typed that. We have talked about adding code to make that deduction. The issue is how to do so without expending a lot o

Re: [SQL] sub-select with aggregate

2002-10-24 Thread Tomasz Myrta
Sorry, In second query is: drop view v; create view v as select B.id_biletu... Tomasz Myrta ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]

Re: [SQL] sub-select with aggregate

2002-10-24 Thread Tomasz Myrta
Uz.ytkownik Stephan Szabo napisa?: I see these two queries that in 7.3 push the clause into the subquery and I believe should have the same output: create view v as select group_id, a/sum_a as percent_a, b/sum_b as percent_b from (select group_id, sum(a) as sum_a, sum(b) as su

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Tomasz Myrta
> The other forms only move the filtering clauses around. There's > still only a filter on the outer group_id equaling the inner > group_id and a filter on group_id=3. It's just a question of > whether it's: > > Scan users in subselect from group_id=3, group and aggregate them > and join wit

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Tomasz Myrta
> On Wed, 23 Oct 2002, Tomasz Myrta wrote: > > > Uz.ytkownik Stephan Szabo napisa?: > Without group_id in the select list you couldn't do a where > group_id = if the select was a view. I know - it was just example of query. > Did you see the other two queries I gave? On 7.3, both of those queri

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Stephan Szabo
On Wed, 23 Oct 2002, Tomasz Myrta wrote: > > On Wed, 23 Oct 2002, Tomasz Myrta wrote: > > > > > Uz.ytkownik Stephan Szabo napisa?: > > Without group_id in the select list you couldn't do a where > > group_id = if the select was a view. > I know - it was just example of query. > > > Did you see t

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Stephan Szabo
On Wed, 23 Oct 2002, Stephan Szabo wrote: > Did you see the other two queries I gave? On 7.3, both of those queries > appear (according to explain output) to do the limiting of group_id > inside the subquery rather than doing the subquery with all rows. > The explanation above was why I believe

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Stephan Szabo
On Wed, 23 Oct 2002, Tomasz Myrta wrote: > Uz.ytkownik Stephan Szabo napisa?: > > But you can't do that anyway, because you don't expose group_id > > in the original query. I assume user_id was a mistake then and was > > meant to be group_id or that both were meant to be in the > > select list. >

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Tomasz Myrta
Uz.ytkownik Stephan Szabo napisa?: But you can't do that anyway, because you don't expose group_id in the original query. I assume user_id was a mistake then and was meant to be group_id or that both were meant to be in the select list. Yes, I meant group_id, but in orginal query I didn't have t

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Stephan Szabo
On Wed, 23 Oct 2002, Tomasz Myrta wrote: > Uz.ytkownik Stephan Szabo napisa?: > > Does using X.group_id=3 in the where clause work better? > It works better, but not if you want to create a view and make > "select * from some_view where group_id=3" :-( But you can't do that anyway, because you do

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Tomasz Myrta
Uz.ytkownik Stephan Szabo napisa?: Does using X.group_id=3 in the where clause work better? It works better, but not if you want to create a view and make "select * from some_view where group_id=3" :-( On 7.3 with no statistics for the table, that appears to move the filter into the subquery pl

Re: [SQL] sub-select with aggregate

2002-10-23 Thread Stephan Szabo
On Wed, 23 Oct 2002, Tomasz Myrta wrote: > Hi > I want to perform query looking like this: > > select > user_id, > a/sum_a as percent_a, > b/sum_b as percent_b > from > users join > (select > group_id, > sum(a) as sum_a, > sum(b) as sum_b >from users group by group_id) X

[SQL] sub-select with aggregate

2002-10-23 Thread Tomasz Myrta
Hi I want to perform query looking like this: select user_id, a/sum_a as percent_a, b/sum_b as percent_b from users join (select group_id, sum(a) as sum_a, sum(b) as sum_b from users group by group_id) X using (group_id) where group_id=3; This query works, but very slow. Subquery