Question

I am still learning about SQL Server, and recently I encountered with issue with ranking (not sure if i should use rank).

I am trying to get the ranking to sort out as below, but i could not achieve by using row_number(over) or any ranking function in SQL server.

[Add in ] the content in table1 has Cust_Code, Week and T_Mode, the one i display below is CustA. basically i want to know CustA, he is first using Air, then Water then switch to Air again in listed weeks.

**** Required Output ******
    Week    T_Mode  Rank
    201301  Air     1
    201303  Water   2
    201305  Water   2
    201306  Water   2
    201311  Air     3



i used Row_Number but it does not give what I want.

select *
, row_number()over(partition by T_Mode order by week) as Rank
from table1

the output returned

Week    T_Mode  Rank
201301  Air     1
201303  Water   1
201305  Water   2
201306  Water   3
201311  Air     2

any advice would be most welcome. Thank you!

Was it helpful?

Solution 2

Here's a solution that works in SQL Server 2012:

select 
  week, t_mode, 
  sum(change) over (order by week rows unbounded preceding) [rank]

from (
  select *, 
  case when 
    (select top 1 t_mode 
     from table1 
     where week < t1.week 
     order by week desc) <> t1.t_mode then 1 else 0 end [change]

  from table1 t1
) x

See this SQL Fiddle

Here is a less efficient version for SQL 2008:

;with cte as (
  select *, 
  case when 
    (select top 1 t_mode 
     from table1 
     where week < t1.week 
     order by week desc) <> t1.t_mode then 1 else 0 end [change]

  from table1 t1
)

select 
  week, t_mode, 
  (select sum(change) from cte where week <= x.week) [rank]

from cte x

And here's the fiddle.

OTHER TIPS

Declare @t table(Week1 int,T_Mode varchar(20))
insert into @t values(201301,'Air'),(201303,'Water'),(201305,'Water'),(201306,'Water'),(201311,'Air')

;with cte as
(
select top 1 week1,t_mode,1 [Rank] from @t order by week1
union all
select b.week1,b.t_mode,case when a.T_Mode=b.T_Mode then a.Rank else a.Rank+1   end  [Rank]  from @t b
outer apply cte a
where b.Week1>a.week1 and b.T_Mode<>a.T_Mode 


)

select distinct * from cte
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top