🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.interbase.sql
Article #427

Re: Sum two tables with HAVING

#427
From: Karol Bieniaszewski
Date: Mon, 10 Mar 2008 08:36
56 lines
2490 bytes
Uzytkownik "P.G. Willemsen" <peter@remove.this.vhep.and.this.nl> napisal w 
wiadomosci news:47d1690f$1@newsgroups.borland.com...
>I would like to get sums from two tables like this:
>
> select
>   t1.id, t1.description,
>   (select sum(t2.amount) from t2 where t2.id = t1.id) as sum1,
>   (select sum(t3.amount) from t3 where t3.id = t1.id) as sum2
> from t1
>
> with a condition to eliminate all rows with zeroes:
>
> where sum1 <> 0 and sum2 <> 0 --> doesn't work
> *or*
> having sum1 <> 0 and sum2 <> 0 --> doesn't work
>
>
> How could I prevent getting rows with zeroes?
>
>
> I also tried rewriting the query like this:
>
> select
>   t1.id, t1.description,
>   sum(t2.amount) as sum1,
>   sum(t3.amount) as sum2
> from t2
>   left join t1 on t1.id = t2.id
>   left join t3 on t3.id = t2.id
> group by
>   t1.id, t1.description
> having
>   sum(t2.amount) <> 0 and sum(t3.amount) <> 0
>
> This eliminates all rows with double zeroes, but all sums are twice as 
> high, because of the double joins, so all rows are twice in the joined set 
> that is summed.
>
> Who can help me to get the correct query?
>
> Thanks in advance!
> Peter

slow but work

select
   t1.id, t1.description,
   (select sum(t2.amount) from t2 where t2.id = t1.id) as sum1,
   (select sum(t3.amount) from t3 where t3.id = t1.id) as sum2
 from t1
where
 (select sum(t2.amount) from t2 where t2.id = t1.id) <> 0
and
(select sum(t3.amount) from t3 where t3.id = t1.id)<> 0

Karol Bieniaszewski 
Technical Details
Message-ID: <47d4f085@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <47d1690f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,1e4b593392602380
X-Google-Attributes: gid114a0e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Karol Bieniaszewski" <liviuslivius@poczta.onet.pl>
Newsgroups: borland.public.interbase.sql
References: <47d1690f$1@newsgroups.borland.com>
Subject: Re: Sum two tables with HAVING
Date: Mon, 10 Mar 2008 09:36:42 +0100
Lines: 58
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 195.205.151.58
Message-ID: <47d4f085@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1205137541 195.205.151.58 (10 Mar 2008 01:25:41 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
X-Original-Bytes: 2337