sql server - Using a 'rank' to return 'top' row in SQL -


i have data set returns clients services, table below:

person_id   service_category   service_rank 1234        blow dry           3   1234        cut , colour     2 1234        restyle            1 4321        blow dry           3 4321        cut , colour     2 4321        fringe trim        1 

(no idea why used haircutting, sensitive data , cant use itself)

so need run report of services above, want bring highest service_rank (1-10 lets say, 1 being important)

this bring whichever highest was, if rank 3, class of importance.

in instance, expect return

person_id   service_category   service_rank  1234        restyle            1 4321        fringe trim        1 

i hope makes sense. @ moment view, using variety of tables bring data through, , ideally form part of select statement.

unfortunately, learning sql , such need as can get!

thanks in advance (i couldnt find addressed issue)

nikki

;with cte (   select      person_id, service_category, service_rank,      row_number() on (partition person_id order service_rank) rn       yourtable ) select    person_id, service_category, service_rank cte rn = 1 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -