oracle 分组+去重 sql请教
时间:2011-09-23
来源:互联网
求sql语句
现有表
create table t1{
id varcher2(20) primary key,
pc varcher2(20),
tname varcher2(20),
tver varcher2(20),
tstatus varchar2(20),
time1 date,
time2 date
}
实例t1数据
insert into t1 select '001','p1','tn1','tv1','on',sysdate,'2011-09-23 10:22:33' from dual;
insert into t1 select '002','p1','tn1','tv1','oooo',sysdate,'2011-09-21 10:22:33' from dual;
insert into t1 select '003','p1','tn1','tv1','off',sysdate,'2011-09-23 10:22:33' from dual;
insert into t1 select '004','p1','tn1','tv1','on',sysdate,'2011-09-22 10:22:33' from dual;
insert into t1 select '005','p1','tn1','tv1','on',sysdate,'2011-09-20 10:22:33' from dual;
insert into t1 select '006','p3','tn1','tv1','on',sysdate,'2011-09-23 10:22:33' from dual;
insert into t1 select '007','p2','tn1','tv1','off',sysdate,'2011-08-23 10:22:33' from dual;
insert into t1 select '008','p1','tn1','tv1','off',sysdate,'2011-09-23 10:22:33' from dual;
insert into t1 select '009','p1','tn1','tv1','off',sysdate,'2011-09-21 10:22:33' from dual;
需求:根据字段 pc tname tstatus 分组获取数量,并关联到time2最大的那条记录
要求获取如下结果
id pc tname,tver,tstatus ,count,time2
001 p1 tn1 tv1 on 3 '2011-09-23 10:22:33'
001 p1 tn1 tv1 off 2 '2011-09-23 10:22:33'
001 p2 tn1 tv1 off 1 '2011-08-23 10:22:33'
001 p3 tn1 tv1 on 1 '2011-09-23 10:22:33'
需要效率高点的sql,因为t1表数据已经比较大,30w多数据,字段也比较多
自己写了个,有点长,但是感觉效率不好
现有表
create table t1{
id varcher2(20) primary key,
pc varcher2(20),
tname varcher2(20),
tver varcher2(20),
tstatus varchar2(20),
time1 date,
time2 date
}
实例t1数据
insert into t1 select '001','p1','tn1','tv1','on',sysdate,'2011-09-23 10:22:33' from dual;
insert into t1 select '002','p1','tn1','tv1','oooo',sysdate,'2011-09-21 10:22:33' from dual;
insert into t1 select '003','p1','tn1','tv1','off',sysdate,'2011-09-23 10:22:33' from dual;
insert into t1 select '004','p1','tn1','tv1','on',sysdate,'2011-09-22 10:22:33' from dual;
insert into t1 select '005','p1','tn1','tv1','on',sysdate,'2011-09-20 10:22:33' from dual;
insert into t1 select '006','p3','tn1','tv1','on',sysdate,'2011-09-23 10:22:33' from dual;
insert into t1 select '007','p2','tn1','tv1','off',sysdate,'2011-08-23 10:22:33' from dual;
insert into t1 select '008','p1','tn1','tv1','off',sysdate,'2011-09-23 10:22:33' from dual;
insert into t1 select '009','p1','tn1','tv1','off',sysdate,'2011-09-21 10:22:33' from dual;
需求:根据字段 pc tname tstatus 分组获取数量,并关联到time2最大的那条记录
要求获取如下结果
id pc tname,tver,tstatus ,count,time2
001 p1 tn1 tv1 on 3 '2011-09-23 10:22:33'
001 p1 tn1 tv1 off 2 '2011-09-23 10:22:33'
001 p2 tn1 tv1 off 1 '2011-08-23 10:22:33'
001 p3 tn1 tv1 on 1 '2011-09-23 10:22:33'
需要效率高点的sql,因为t1表数据已经比较大,30w多数据,字段也比较多
自己写了个,有点长,但是感觉效率不好
作者: wasp520 发布时间: 2011-09-23
SQL code
select t.* from t1 t where time2 = (select max(time2) from t1 where pc = t.pc and tname = t.tname and tstatus = t.tstatus) order by t.pc ,t.tname ,t.tstatus select t.* from t1 t where not exists (select 1 from t1 where pc = t.pc and tname = t.tname and tstatus = t.tstatus and time2 > t.time2) order by t.pc ,t.tname ,t.tstatus select 除了px字段外的其他字段 from ( select m.* , row_number() over(partition by m.pc ,m.tname ,m.tstatus order by m.time2) px from t1 m ) n where px = 1 order by n.pc ,n.tname ,n.tstatus
作者: dawugui 发布时间: 2011-09-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28