今天朋友发来一段Oracle的代码,要求改成mysql。代码里面有row number() OVER(PARTITION BY)和DECODE这些不通用的函数。
decode是翻译函数,这个用case when 很好解决,开窗函数就有点麻烦了。
源代码:select t.*, ROW_NUMBER() OVER( PARTITION BY t.ticket_type ORDER BY t.open_ticket_time DESC ) as rn
from
term t
where
t.ticket_type in(
6, 5, 0, 1, **, *, **, **, *2, 2, 3, 4, 8, 7
)
and t.term_status >= 5
这个代码的意思是根据open_ticket_time倒序的顺序将ticket_type 是( 6, 5, 0, 1, **, *, **, **, *2, 2, 3, 4, 8, 7)还有term_status >= 5的数据对ticket_type 进行分组后排名ROW_NUMBER() 。