Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding values in one table between those in another 1

Status
Not open for further replies.

xplo5iv

Technical User
Joined
Jan 5, 2005
Messages
75
Location
GB
Hi
(I'm not quite sure what you call this, so I've had trouble searching)

I have two tables
Salaries
NINO
Salary

ScalePoints
SCP
AnnualPay

I need a query which will tell me the nearest SCP for each NINO.
(Salary is actual amount earned, SCP is point on the pay scale, e.g. if SCP 5 is £10k and SCP 6 is £12k, then if a person earns £10.5k the query should return a 5 against their NINO)

Can anyone point me in the right direction?

Thanks for your help.

Jonathan
 
This seems to work for your specific test case
Code:
Select NINO, Salary

     , (Select TOP 1 P.SCP
        From ScalePoints P
        GROUP BY SCP
        ORDER BY MIN(Abs(P.AnnualPay - S.Salary))) As SCP

From Salaries S
 
Thanks Golom, really useful
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top