pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gledatelj <barney@yahoo.com>
To: pgsql-sql@postgresql.org
Subject: Re: Combine query views into one SQL string
Date: Tue, 11 Sep 2001 08:33:30 +0200
Message-ID: <9nkbg3$9db$1@bagan.srce.hr> (raw)
References: <63e57aa8.0109060835.33719290@posting.google.com>
	<3B97DC1F.94433F65@mbit.nl>
	<9na15r$ku4$1@narses.hrz.tu-chemnitz.de>
	<63e57aa8.0109091520.4f975abd@posting.google.com>

Hy Maik,

you can select it directly from the tables using following sql statement
(but it's same as using views):

select
   t1.id, t1.f1, t2.f2
from
(select id, sum(amount) f1
  from table1
  group by id) t1,
(select id, sum(amount) f2
  from table2
  group by id) t2
where
  t1.id = t2.id

but, you must also know that in this way you may not get all the records
(sum of amount of all id-s) if this is your intention, but just sum of
amount of identical id-s (contained in both tables).

Maybe this will help you.


"Eimas" <eimas@lycos.com> wrote in message
news:63e57aa8.0109091520.4f975abd@posting.google.com...
> "Maik" <K.Maik@web.de> wrote in message
news:<9na15r$ku4$1@narses.hrz.tu-chemnitz.de>...
> > Its clear, union concat the two results.
> >
> > But he can also use this join version, if its the intention.
> >
> > select t1.id, sum(t1.amount), t2.id, sum(t2.amount) from table1 as t1,
> > table2 as t2 where t1.id=t2.id;
> >
> > Ciao Maik
>
> This is not right, i wouldnt even bather you if this was the simple
> answer.
>
> t1.id=t2.id would mean in doubled or tripled aggregation,
> since t1.id and t2.id are not unique, thats why I had to aggregate
> them in first Q1, Q2 querires, and link them by ID in the last one.
>
> I don't understand what UNION got to do here. then you probably have
> to make three union links of inner, left and right joined tables.
>
> This is not that easy as it seems.
> Is there a universal SQL string "expanding" rules, like in math or so?
>
> Thanks





view thread (7+ messages)  latest in thread

Message-ID: <9nkbg3$9db$1@bagan.srce.hr>
Permalink:  ../9nkbg3$9db$1@bagan.srce.hr/
Also on:    postgresql.org/message-id/9nkbg3$9db$1@bagan.srce.hr

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: barney@yahoo.com
  Subject: Re: Combine query views into one SQL string
  In-Reply-To: <9nkbg3$9db$1@bagan.srce.hr>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox