I am having serious slowness issues for something that should be speedy. Basically I want to find the highest point value(pv) for a referral(excluding pv's that are smaller for duplicate account numbers)
So if the table looked like this
ID AccntNum EmpID PointValue
1..3444.....45....60
2..3444.....45....55
3..7777.....45....40
I want this returned
AccntNum EmpID PointValue
3444.....45....60
7777.....45....40
Since the two point values 60,55 are for the same account. I only want the highest.
Pulling the maximum value for that employee on that account, but also keeping other accounts maximums.
Here is what I have so far. It works, but it is slow. Even when there are only 10 records that apply to one employee (There are about 8000 in the table)
I'm hoping that there is an easier way to write this. Its really slow for the amount of records that apply. It'd be great if someone could help.
Thanks
So if the table looked like this
ID AccntNum EmpID PointValue
1..3444.....45....60
2..3444.....45....55
3..7777.....45....40
I want this returned
AccntNum EmpID PointValue
3444.....45....60
7777.....45....40
Since the two point values 60,55 are for the same account. I only want the highest.
Pulling the maximum value for that employee on that account, but also keeping other accounts maximums.
Here is what I have so far. It works, but it is slow. Even when there are only 10 records that apply to one employee (There are about 8000 in the table)
Code:
SELECT SUM(Point_Value) AS points
FROM Referrals R
WHERE R.Num IN (
SELECT TOP 1 P.Num
FROM Referrals P
WHERE P.Account_Num=R.Account_Num AND
P.EmpID=@empid
ORDER BY Point_Value DESC)
Thanks