Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 group by and sum

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-04-15 : 10:49:50
hi,

i have following sample data:

create table orders
(orders int
,product char(1)
,order_value float
,product_values float)

insert into orders values (223, 'A', 11, 11)
insert into orders values (224, 'A', 11, 11)
insert into orders values (225, 'A', 13, 11)
insert into orders values (225, 'B', 13, 2)


and when i run this:

select
sum(order_value) as order_value
,sum(product_values) as product_value
from orders

i get:

order_value product_value
---------------------- ----------------------
48 35

(1 row(s) affected)


but i should get order value to be the same as product_value, because i want to count orders only once (those which have two or more rows)

I've tried with derived table but can't get sum.

thank you in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-15 : 11:17:23
[code]select sum(order_value) as order_value,sum(product_value) as product_value
from
(select
orders,max(order_value) as order_value
,sum(product_values) as product_value
from orders
group by orders)t
[/code]
Go to Top of Page
   

- Advertisement -