🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: help with group by ..

#158
From: Karol Bieniaszewski
Date: Tue, 24 Jun 2008 11:47
77 lines
3638 bytes
U�ytkownik "Karol Bieniaszewski" <liviuslivius@poczta.onet.pl> napisa� w 
wiadomo�ci news:4860de77@newsgroups.borland.com...
>
> U�ytkownik "Karol Bieniaszewski" <liviuslivius@poczta.onet.pl> napisa� w 
> wiadomo�ci news:4860b9cd@newsgroups.borland.com...
>>
>> U�ytkownik "Karol Bieniaszewski" <liviuslivius@poczta.onet.pl> napisa� w 
>> wiadomo�ci news:48608627$1@newsgroups.borland.com...
>>>
>>> Uzytkownik "Wayne Niddery (TeamB)" <wniddery@chaffaci.on.ca> napisal w 
>>> wiadomosci news:485fb1d8$1@newsgroups.borland.com...
>>>> "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.
>>>>
>>> thanks Wayne i'll try views for this
>>> in tables are not the same number of records
>>> and some of thises tables in some month can be empty :/
>>>
>>> Karol Bieniasszewski
>>
>> Views can not be used to solve this
>> becaouse of some empty month in one of the tables
>> any more concept?


> i solved this with union and Views and some trick with SUM

Karol Bieniaszewski 
Technical Details
Message-ID: <4860debd$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <485f842b@newsgroups.borland.com> <485fb1d8$1@newsgroups.borland.com> <48608627$1@newsgroups.borland.com> <4860b9cd@newsgroups.borland.com> <4860de77@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,8abde91de922829b
X-Google-Attributes: gid114a0e,domainid0,public,usenet
X-Google-Language: POLISH,Latin2
From: "Karol Bieniaszewski" <liviuslivius@poczta.onet.pl>
Newsgroups: borland.public.interbase.sql
References: <485f842b@newsgroups.borland.com> <485fb1d8$1@newsgroups.borland.com> <48608627$1@newsgroups.borland.com> <4860b9cd@newsgroups.borland.com> <4860de77@newsgroups.borland.com>
Subject: Re: help with group by ..
Date: Tue, 24 Jun 2008 13:47:22 +0200
Lines: 79
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 195.205.151.58
Message-ID: <4860debd$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1214308029 195.205.151.58 (24 Jun 2008 04:47:09 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail