Groups | Blog | Home
all groups > sql server (microsoft) > february 2006 >

sql server (microsoft) : Query



Rich Mogy
2/25/2006 4:19:18 AM
Hi All,
I have a table that has records for commission splits on products -- one
record for each person involved in the product and his split

Product Person Split
123 100 50
123 101 50
124 102 72
124 101 28

I need to product a report and pick out the person with the largest split,
so for product 124 its a no brainer. But for product 123, I just need an
arbitrary selection, but only one, and I don't care which one. I'm joining
this table with my product master file on product id.

Thanks in advance.

Richard Mogy

VC
2/27/2006 2:07:01 PM
Rich - There are a couple ways to do this, but I like the following approach
(you could also do a self-join):

select top 1 y.product, y.person, y.split
from yourTable y
where split = (select max(y2.split) from yourTable y2 where y2.product =
y.product)


[quoted text, click to view]

Rich Mogy
2/28/2006 12:10:55 AM
Thanks for the input. I'll try it.
[quoted text, click to view]

Rich Mogy
2/28/2006 5:06:27 PM
Thanks -- it is far more elegant than what I did....
[quoted text, click to view]

Rich Mogy
2/28/2006 10:30:12 PM
I spoke too soon -- the SQL, as written only produces one row -- I need one
row for each product.
[quoted text, click to view]

VC
3/1/2006 9:06:45 AM
select y.product, y.person, y.split
from testing1 y
where
y.split = (select max(y2.split) from testing1 y2 where y2.product =
y.product)
and y.person = (select top 1 person from testing1 y3 where y3.split =
y.split)

[quoted text, click to view]

Rich Mogy
3/1/2006 6:10:42 PM
Still only one line.
[quoted text, click to view]

AddThis Social Bookmark Button