Subject Re: Group by question
Author synecticsza
Marcin,

There are two ways to do this, but one may turn out to have incorrect
results depending on your data:

1st way (potentially incorrect results):

select
r.odbiorca_id
cast(r.data_wyj - r.data_wej as integer) as o_sklad
count (r.kod_8)
sum(r.waga)
from mgp_role r
group by odbiorca_id, data_wyj,data_wej

This will run, but since (r.data_wyj - r.data_wej) may have the same
result for different field values (15 -5 is the same as 20 - 10,
but will group by into different rows) you may not get the results
you expect.

The other way is to define a view:

create view myview (odbiorca_id,o_sklad,kod_8,waga)
as
select odbiorca_id,data_wyj - data_wej ,kod_8,waga
from mgp_role

OR

create a calculated field:

alter table mgp_role add o_sklad computed by (data_wyj - data_wej );

and run the following select:

select
r.odbiorca_id
r.o_sklad
count (r.kod_8)
sum(r.waga)
from (table or view)
group by odbiorca_id,o_sklad


regards

Vince








--- In ib-support@y..., "Marcin Bury" <mbury@n...> wrote:
> Hi list
>
> I have following select statement:
>
> r.odbiorca_id
> cast(r.data_wyj - r.data_wej as integer) as o_sklad
> count (r.kod_8)
> sum(r.waga)
> from mgp_role r
>
> for correct syntax I have to add group by part, but how I can put
computed
> column there.
> One can say - create a view or add computed column to the table. Is
there
> another solution?
>
> TIA
> Marcin