mysql排名

1.排名

hospital_id, vcc_id, fee_type相同的情况下,create_time升序排名

select * FROM  (
SELECT  @rownum:=@rownum+1 AS rownum,
       IF(@x=concat(uo.hospital_id,uo.vcc_id,uo.fee_type),@rank:=@rank+1,@rank:=1) rank,
       @x:=concat(uo.hospital_id,uo.vcc_id,uo.fee_type),
       hospital_id, vcc_id, fee_type,create_time,update_time,id
from
(
select *
  from hospital_vaccine_inventory
 where(hospital_id, vcc_id, fee_type) IN(
select hospital_id, vcc_id, fee_type
  from(
select hospital_id, vcc_id, fee_type, count(*)
  from tb_vaccine_inventory
 GROUP BY hospital_id, vcc_id, fee_type
HAVING COUNT(*)> 1) t)
) uo,(SELECT @rownum:=0,@rank:=0) init
order by hospital_id, vcc_id, fee_type, create_time asc
    )  result
where hospital_id=3490

posted @ 2019-10-15 16:42  slnngk  阅读(166)  评论(0编辑  收藏  举报