ZDSXH LDSXH ZDSXH2
10 10
20 30
30 50
40 80
50 80 select ZDSXH, LDSXH,LDSXH-val ZDSXH2 from
(select t.ZDSXH,t.LDSXH,row_number() over(partition by t.LDSXH order by t.ZDSXH desc) val from test_table t)
order by ZDSXH;
使用以上语句可以使ZDSXH2得到如果下结果
9
29
49
78
79 如何才能使以上语句按照线路名称依次执行,得出ZDSXH2的值呢?举例如下:
XLMC ZDSXH LDSXH ZDSXH2
01路 10 10 9
01路 20 20 19
01路 30 30 28
01路 40 30 29
02路 10 10 9
02路 20 30 29
02路 30 50 48
02路 40 50 49
03路 10 20 19
03路 20 30 27
03路 30 30 28
03路 40 30 29…………
10 10
20 30
30 50
40 80
50 80 select ZDSXH, LDSXH,LDSXH-val ZDSXH2 from
(select t.ZDSXH,t.LDSXH,row_number() over(partition by t.LDSXH order by t.ZDSXH desc) val from test_table t)
order by ZDSXH;
使用以上语句可以使ZDSXH2得到如果下结果
9
29
49
78
79 如何才能使以上语句按照线路名称依次执行,得出ZDSXH2的值呢?举例如下:
XLMC ZDSXH LDSXH ZDSXH2
01路 10 10 9
01路 20 20 19
01路 30 30 28
01路 40 30 29
02路 10 10 9
02路 20 30 29
02路 30 50 48
02路 40 50 49
03路 10 20 19
03路 20 30 27
03路 30 30 28
03路 40 30 29…………
partition by t.xlmc,t.LDSXH order by t.ZDSXH desc
这个可以得到么,里面用的是row_number哟,怎么能得到你下面的数据。 你这里的
9
01路 20 20 19
01路 30 30 28
01路 40 30 29
02路 10 10 9
02路 20 30 29
02路 30 50 48 是怎么来的呀。
create table a(xlmc varchar2(10),zdsxh int,ldsxh int,zdsxh2 int);
insert into a values('01路', 10 , 10 , 9);
insert into a values('01路', 20 , 20 , 19 );
insert into a values('01路', 30 , 30 , 28);
insert into a values('01路', 40 , 30 , 29);
insert into a values('02路', 10 , 10 , 9 );
insert into a values('02路', 20 , 30 , 29 );
insert into a values('02路', 30 , 50 , 48 );
insert into a values('02路', 40 , 50 , 49 );
insert into a values('03路', 10 , 20 , 19 );
insert into a values('03路', 20 , 30 , 27 );
insert into a values('03路', 30 , 30 , 28 );
insert into a values('03路', 40 , 30 , 29 );select xlmc,zdsxh,ldsxh,ldsxh-rn zdsxh2
from(
select xlmc,zdsxh,ldsxh,zdsxh2,count(*) over(partition by xlmc,ldsxh order by zdsxh desc) rn
from a)
order by xlmc,zdsxh,ldsxh