🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: help with group by ..

#254
From: Wayne Niddery (TeamB)
Date: Mon, 23 Jun 2008 14:23
53 lines
2684 bytes
"Karol Bieniaszewski" <liviuslivius@poczta.onet.pl> wrote in message 
news:485f842b@newsgroups.borland.com...
>
> sumary by day in one row per day
>
> tableA
> ID Integer Primary key
> DT DATE NOT NULL,
> QU NUMERIC(18, 4),
> PRICE NUMERIC(18, 2)
>
> tableB
> ID Integer Primary key
> DT DATE NOT NULL,
> QU NUMERIC(18, 4),
> PRICE NUMERIC(18, 2)
>
> i need some like
> select
> {A.DT or B.DT}, sum(A.QU), SUM(A.QU * A.PRICE), SUM(B.QU), SUM(B.QU * 
> A.PRICE)
> FROM
> tableA
> tableB
>
> GROUP BY {A.DT or B.DT ... ????}
>
> result should by like
>
> DT                 SUMA       WARTA    SUMB   WARTB
> 2008-01-01   19               590            14          890
> 2008-01-02   12               789             11         654

Maybe I'm not understanding what you want, but I think this should be doable 
with an inner join linked on the date

select a.dt, sum(a.qu), sum(a.qu * a.price), sum(b.qu), sum(b.qu * b.price)
from tablea a
inner join tableb b on b.dt = a.dt
group by a.dt

However, this assumes there is the same number of records in both tables for 
a given date. If there are different numbers of records then this will give 
you wrong totals. In that case you would need to use subqueries which will 
probably not perform well.

You could create a view on each table that gets the correct sums for that 
table by date, and then you can do the above query on those views to get 
correct totals.

-- 
Wayne Niddery - TeamB (www.teamb.com)
Winwright, Inc. (www.winwright.ca) 
Technical Details
Message-ID: <485fb1d8$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <485f842b@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,8abde91de922829b
X-Google-Attributes: gid114a0e,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Wayne Niddery \(TeamB\)" <wniddery@chaffaci.on.ca>
Newsgroups: borland.public.interbase.sql
References: <485f842b@newsgroups.borland.com>
In-Reply-To: <485f842b@newsgroups.borland.com>
Subject: Re: help with group by ..
Date: Mon, 23 Jun 2008 10:23:09 -0400
Lines: 54
MIME-Version: 1.0
Content-Type: text/plain;
 format=flowed;
 charset="iso-8859-1";
 reply-type=response
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Windows Mail 6.0.6001.18000
X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6001.18000
NNTP-Posting-Host: 24.137.214.213
Message-ID: <485fb1d8$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1214231000 24.137.214.213 (23 Jun 2008 07:23:20 -0700)
X-Authenticated-User: wniddery
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail