Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help with group by ..

7 views
Skip to first unread message

Karol Bieniaszewski

unread,
Jun 23, 2008, 7:08:39 AM6/23/08
to
Hi

i have problem with group by in this scenario

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
................................................................

if i do this in this way

GROUP BY A.DT, B.DT i get mixed date sum


is this posible to do this in sql
or i must create stored procedure for this?

please help
Karol Bieniaszewski


Wayne Niddery (TeamB)

unread,
Jun 23, 2008, 10:23:09 AM6/23/08
to
"Karol Bieniaszewski" <livius...@poczta.onet.pl> wrote in message
news:485f...@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)

Karol Bieniaszewski

unread,
Jun 24, 2008, 1:29:17 AM6/24/08
to

Uzytkownik "Wayne Niddery (TeamB)" <wnid...@chaffaci.on.ca> napisal w
wiadomosci news:485fb1d8$1...@newsgroups.borland.com...
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


Karol Bieniaszewski

unread,
Jun 24, 2008, 5:09:45 AM6/24/08
to

Użytkownik "Karol Bieniaszewski" <livius...@poczta.onet.pl> napisał w
wiadomości news:48608627$1...@newsgroups.borland.com...

Views can not be used to solve this
becaouse of some empty month in one of the tables
any more concept?

Karol Bieniaszewski


Karol Bieniaszewski

unread,
Jun 24, 2008, 7:47:22 AM6/24/08
to

Użytkownik "Karol Bieniaszewski" <livius...@poczta.onet.pl> napisał w
wiadomości news:4860...@newsgroups.borland.com...

>
> Użytkownik "Karol Bieniaszewski" <livius...@poczta.onet.pl> napisał w
> wiadomości news:4860...@newsgroups.borland.com...


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

Karol Bieniaszewski


Karol Bieniaszewski

unread,
Jun 24, 2008, 7:46:11 AM6/24/08
to

Użytkownik "Karol Bieniaszewski" <livius...@poczta.onet.pl> napisał w
wiadomości news:4860...@newsgroups.borland.com...
i solved this with union and some trick with SUM

Karol Bieniaszewski


Wayne Niddery (TeamB)

unread,
Jun 24, 2008, 1:22:10 PM6/24/08
to
"Karol Bieniaszewski" <livius...@poczta.onet.pl> wrote in message
news:4860...@newsgroups.borland.com...

>
> Views can not be used to solve this
> becaouse of some empty month in one of the tables


When querying the views, try a left join instead of the inner join in my
example query.

Karol Bieniaszewski

unread,
Jun 25, 2008, 1:19:34 AM6/25/08
to

Uzytkownik "Wayne Niddery (TeamB)" <wnid...@chaffaci.on.ca> napisal w
wiadomosci news:48612d51$1...@newsgroups.borland.com...


becaouse of problem with storing plans
i change view to selectable stored procedure
and all work as expected and performance also is ok :)

Karol Bieniaszewski


0 new messages