3,499
社区成员
发帖
与我相关
我的任务
分享--如果还需要求数量,则:
select m.* , n.cnt from
(
--以下两个任意一个
select t.* from t1 t where time2 = (select max(time2) from t1 where pc = t.pc and tname = t.tname and tstatus = 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)
) m,
(
select pc, tname ,tstatus , count(1) cnt from t1 group by pc ,tname ,tstatus
) n
where m.pc = n.pc and m.tname = n.tname , m.tstatus = n.tstatus
order by m.pc ,m.tname ,m.tstatus
create table t1(
id varchar2(20) primary key,
pc varchar2(20),
tname varchar2(20),
tver varchar2(20),
tstatus varchar2(20),
time1 date,
time2 date
);
insert into t1 select '002','p1','tn1','tv1','oooo',sysdate,to_date('2011-09-21 10:22:33','yyyy-MM-dd HH24:mi:ss') from dual;
insert into t1 select '003','p1','tn1','tv1','off',sysdate,to_date('2011-09-23 10:22:33','yyyy-MM-dd HH24:mi:ss') from dual;
insert into t1 select '004','p1','tn1','tv1','on',sysdate,to_date('2011-09-22 10:22:33','yyyy-MM-dd HH24:mi:ss') from dual;
insert into t1 select '005','p1','tn1','tv1','on',sysdate,to_date('2011-09-20 10:22:33','yyyy-MM-dd HH24:mi:ss') from dual;
insert into t1 select '006','p3','tn1','tv1','on',sysdate,to_date('2011-09-23 10:22:33','yyyy-MM-dd HH24:mi:ss') from dual;
insert into t1 select '007','p2','tn1','tv1','off',sysdate,to_date('2011-08-23 10:22:33','yyyy-MM-dd HH24:mi:ss') from dual;
insert into t1 select '008','p1','tn1','tv1','off',sysdate,to_date('2011-09-23 10:22:33','yyyy-MM-dd HH24:mi:ss') from dual;
insert into t1 select '009','p1','tn1','tv1','off',sysdate,to_date('2011-09-21 10:22:33','yyyy-MM-dd HH24:mi:ss') from dual;
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