sql 分组求数量,并取最新一条数据

来源:互联网 发布:贰贰网络 编辑:程序博客网 时间:2024/05/18 02:20

sql 分组求数量,并取最新一条数据


-- 需求: 分组求数量,并显示最新一条数据-- 先按md5进行分组,得出重复记录数量,并取最大id-- 接着去连接自已表,找到相关想要取的数据select t2.id,t2.log_md5,t2.num,t3.dt,t3.flag,t3.log from (select  t1.log_md5,count(*) num,max(t1.id) id from x8_crash_log as t1   where t1.dt>='2017-03-07' and t1.dt<='2017-03-13 23:59:59' GROUP BY t1.log_md5 ) as t2 left join x8_crash_log as t3 on t2.id=t3.id;-- 这是取巧么,反正结果是一至的,看上去简单点,效率就不知道了....select  max(id) id,log_md5,count(*) num,max(dt) dt,max(flag) fla,max(log) log from x8_crash_log   where dt>='2017-03-07' and dt<='2017-03-13 23:59:59' GROUP BY log_md5


数据显示一样的


要是没有id,就拿时间去连吧,要是时间一样那再想想吧.....


-- OVER(partition by 找出这个列重复的数字  非我的需求select id,flag,dt,log,log_md5,count(log_md5)OVER(partition by log_md5) num from x8_crash_log where dt>='2017-03-07' and dt<='2017-03-13 23:59:59' order by dt desc


1 0