Subject | group by in the right way... |
---|---|
Author | james_027 |
Post date | 2003-10-16T03:30:52Z |
I have no problem when doing some group by statement if only one
table is involve ... but with two or more table involve I feel like
Iam doing the wrong way...
for example I have a simple invoice and customer table which has
the following fields ...
invoice table
----------------
inv_id
cust_id
inv_date
total_amount
customer table
-------------------
cust_id
cust_name
cust_type
I have a view like this ...
create view invoice_with_cust_details
(cust_id,
cust_type,
total_amount)
as
select m.cust_id, d.cust_name, d.cust_type, sum(m.total_amount)
from invoice m left join customer d on m.cust_id = d.cust_id
group by m.cust_id, d.cust_name, d.cust_type
I want to place the m.cust_id in the group by clause because I want
to total_amount of invoice for each customer, but since in the view
I want to be able to display the cust_name and cust_type I was force
to include the d.cust_name and d.cust_type in the group by clause.
My question is I am not sure if what iam doing is right ... I feel
this is not the right way of doing it. What if I have other field in
the customer table I want to view like customer credit limit,
customer category and etc, does it mean that I have to put all that
in the group by clause...
please advice.
thanks
table is involve ... but with two or more table involve I feel like
Iam doing the wrong way...
for example I have a simple invoice and customer table which has
the following fields ...
invoice table
----------------
inv_id
cust_id
inv_date
total_amount
customer table
-------------------
cust_id
cust_name
cust_type
I have a view like this ...
create view invoice_with_cust_details
(cust_id,
cust_type,
total_amount)
as
select m.cust_id, d.cust_name, d.cust_type, sum(m.total_amount)
from invoice m left join customer d on m.cust_id = d.cust_id
group by m.cust_id, d.cust_name, d.cust_type
I want to place the m.cust_id in the group by clause because I want
to total_amount of invoice for each customer, but since in the view
I want to be able to display the cust_name and cust_type I was force
to include the d.cust_name and d.cust_type in the group by clause.
My question is I am not sure if what iam doing is right ... I feel
this is not the right way of doing it. What if I have other field in
the customer table I want to view like customer credit limit,
customer category and etc, does it mean that I have to put all that
in the group by clause...
please advice.
thanks