pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: Combine query views into one SQL string
7+ messages / 6 participants
[nested] [flat]

* Re: Combine query views into one SQL string
@ 2001-09-07 08:10  Nils Zonneveld <nils@mbit.nl>
  1 sibling, 0 replies; 7+ messages in thread

From: Nils Zonneveld @ 2001-09-07 08:10 UTC (permalink / raw)
  To: pgsql-sql



Maik wrote:
> 
> With "Union" you can create one view.
> Ciao Maik

UNION wouldn't have the desired effect:

the result of a UNION SELECT would look like this:

ID AMOUNT
1  (table1.amount)
2  (table1.amount)
.   .
.   .
1  (table2.amount)
2  (table2.amount)

What he wants is:

ID TABLE1.AMOUNT TABLE2.AMOUNT
1  (amount)      (amount)
2  (amount)      (amount)
.  .             .
.  .             .
 
You can achieve that result with an inner join (and even MySQL with it's
rather restricted SQL subset supports an inner join).

Nils
  
-- 
Alles van waarde is weerloos
Lucebert



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Combine query views into one SQL string
@ 2001-09-07 08:42  Maik <K.Maik@web.de>
  1 sibling, 2 replies; 7+ messages in thread

From: Maik @ 2001-09-07 08:42 UTC (permalink / raw)
  To: pgsql-sql

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





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Combine query views into one SQL string
@ 2001-09-07 09:00  Nils Zonneveld <nils@mbit.nl>
  parent: Maik <K.Maik@web.de>
  1 sibling, 1 reply; 7+ messages in thread

From: Nils Zonneveld @ 2001-09-07 09:00 UTC (permalink / raw)
  To: pgsql-sql



Maik wrote:
> 
> 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;
> 

Yeps, thats another way to write an inner join :-)

Mazzel,

Nils

-- 
Alles van waarde is weerloos
Lucebert



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Combine query views into one SQL string
@ 2001-09-09 07:46  Tony Hunt <tonster@bigpond.net.au>
  parent: Nils Zonneveld <nils@mbit.nl>
  0 siblings, 0 replies; 7+ messages in thread

From: Tony Hunt @ 2001-09-09 07:46 UTC (permalink / raw)
  To: pgsql-sql

I don't get it?  What's the difference between an inner-join and an
equijoin?

"Nils Zonneveld" <nils@mbit.nl> wrote in message
news:3B988C82.9C4782E4@mbit.nl...
>
>
> Maik wrote:
> >
> > 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;
> >
>
> Yeps, thats another way to write an inner join :-)
>
> Mazzel,
>
> Nils
>
> --
> Alles van waarde is weerloos
> Lucebert





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Combine query views into one SQL string
@ 2001-09-09 23:20  Eimas <eimas@lycos.com>
  parent: Maik <K.Maik@web.de>
  1 sibling, 2 replies; 7+ messages in thread

From: eimas@lycos.com @ 2001-09-09 23:20 UTC (permalink / raw)
  To: pgsql-sql

"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



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Combine query views into one SQL string
@ 2001-09-11 06:33  Gledatelj <barney@yahoo.com>
  parent: Eimas <eimas@lycos.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Gledatelj @ 2001-09-11 06:33 UTC (permalink / raw)
  To: pgsql-sql

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





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Combine query views into one SQL string
@ 2001-09-12 10:18  Yoann <yaubi@yaubi.com>
  parent: Eimas <eimas@lycos.com>
  1 sibling, 0 replies; 7+ messages in thread

From: yaubi@yaubi.com @ 2001-09-12 10:18 UTC (permalink / raw)
  To: pgsql-sql

eimas@lycos.com (Eimas) 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


Hi,

I think a got it. Just try :

SELECT DISTINCT t.ID,
   (SELECT SUM(amount)
    FROM Table1
    WHERE ID = t.ID),
   (SELECT SUM(amount)
    FROM Table2
    WHERE ID = t.ID)
FROM Table1 AS t
UNION
SELECT DISTINCT t.ID,
   (SELECT SUM(amount)
    FROM Table1
    WHERE ID = t.ID),
   (SELECT SUM(amount)
    FROM Table2
    WHERE ID = t.ID)
FROM Table2 AS t;

There are in fact two queries : one scaning for the ID's from Table1
and the other from Table2 in order to calcutate the sums for ALL the
ID's.
For each scaned ID, it calculates the corresponding sum in each of the
two tables. If the ID doesn't exist in one table, the sum return a
Null value, but doesn't affect the other.

I hope this query is what you are looking for.
Yoann



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2001-09-12 10:18 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-09-07 08:10 ` Nils Zonneveld <nils@mbit.nl>
2001-09-07 08:42 ` Maik <K.Maik@web.de>
2001-09-07 09:00   ` Nils Zonneveld <nils@mbit.nl>
2001-09-09 07:46     ` Tony Hunt <tonster@bigpond.net.au>
2001-09-09 23:20   ` Eimas <eimas@lycos.com>
2001-09-11 06:33     ` Gledatelj <barney@yahoo.com>
2001-09-12 10:18     ` Yoann <yaubi@yaubi.com>

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