Oracle中利用over函数分组求和,以t_name分组,以t_date排序
SELECT id,t_name,t_date,row_number()over(PARTITION BY t_name ORDER BY t_date DESC) rntime
FROM over_test
MySQL中
SELECT result.id,result.t_name,result.t_date,result.rank rtime FROM (
SELECT b.id,b.t_name,b.t_date,@rownum:=@rownum+1 ,
IF(@t_name=b.t_name,@rank:=@rank+1,@rank:=1) AS rank,
@t_name:=b.t_name
FROM (
SELECT id,t_name,t_date FROM OVER_TEST ORDER BY t_name ASC ,t_date DESC
) b,
(SELECT @rownum :=0 , @t_name := NULL ,@rank:=0) a
) result;
SELECT id,t_name,t_date,row_number()over(PARTITION BY t_name ORDER BY t_date DESC) rntime
FROM over_test
MySQL中
SELECT result.id,result.t_name,result.t_date,result.rank rtime FROM (
SELECT b.id,b.t_name,b.t_date,@rownum:=@rownum+1 ,
IF(@t_name=b.t_name,@rank:=@rank+1,@rank:=1) AS rank,
@t_name:=b.t_name
FROM (
SELECT id,t_name,t_date FROM OVER_TEST ORDER BY t_name ASC ,t_date DESC
) b,
(SELECT @rownum :=0 , @t_name := NULL ,@rank:=0) a
) result;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29320885/viewspace-1749472/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29320885/viewspace-1749472/