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
 Unique value from multiple/repeated records

Author  Topic 

lgarcia3
Starting Member

15 Posts

Posted - 2011-04-18 : 17:32:21
I have a table of Orders for a company (call order header). In the order header we have the number of people serve by an order. We pull information about the order along with the items sold in a especific order. Then we see in the result:
OrderNumber, Served, ItemID, ItemPrice, EmployeeID
1111, 5, 1, 23.50,15
1111, 5, 4, 3.20,15
2222, 2, 8, 9.00,15
2222, 2, 6, 12.35,15
3333, 1, 1, 0.50,15
We need to have only the addition of the "Served" column for this especific Employee (15), which would be 8 in this case. How do I do that?
Thanks!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-18 : 17:46:30
If you are asking what I think you are, then you can do it like this:

SELECT
EmployeeId,
SUM(DISTINCT Served)
FROM
YourTable
GROUP BY
EmployeeId;

But, is it possible to have two rows like this:

1111, 5, 1, 23.50,15
1111, 7, 4, 3.20,15
If so, what would be the result you are expecting?
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-18 : 17:48:39
Not sure what you are looking for but maybe

select EmployeeID, sum(Served)
from
(select distinct OrderNumber, Served, EmployeeID) a
group by EmployeeID



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -