|
|
注意下,上面的不能用下面的语句替换。
[PHP] 和数据有关
SQL> With x As
2 (
3 Select '002' eba002,'001' eba083,'100' eba084 From dual
4 Union
5 Select '002','101','200' From dual
6 Union
7 Select '002','201','250' From dual
8 Union
9 Select '002','301','400' From dual
10 Union
11 Select '003','001','200' From dual
12 Union
13 Select '003','300','400' From dual
14 )
15 Select eba002,Min(eba083),Max(eba084) From (
16 Select eba002,eba083,eba084,
17 eba083 - lag(eba084,1,eba083-1) over (Partition By eba002 Order By eba083) a
18 From x
19 ) Group By eba002,a
20 /
EBA002 MIN(EBA083) MAX(EBA084)
------ ----------- -----------
002 001 250
002 301 400
003 001 200
003 300 400
可以得到正确的结果,可是添加一条信息后,就不对了。
SQL> With x As
2 (
3 Select '002' eba002,'001' eba083,'100' eba084 From dual
4 Union
5 Select '002','101','200' From dual
6 Union
7 Select '002','201','250' From dual
8 Union
9 Select '002','301','400' From dual
10 Union
11 Select '003','001','200' From dual
12 Union
13 Select '003','300','400' From dual
14 Union
15 Select '003','500','600' From dual
16 )
17 Select eba002,Min(eba083),Max(eba084) From (
18 Select eba002,eba083,eba084,
19 eba083 - lag(eba084,1,eba083-1) over (Partition By eba002 Order By eba083) a
20 From x
21 ) Group By eba002,a
22 /
EBA002 MIN(EBA083) MAX(EBA084)
------ ----------- -----------
002 001 250
002 301 400
003 001 200
003 300 600
大家可以看到最后一条是不对的,而LS的语句就可以避免这样的情况
SQL> With x As
2 (
3 Select '002' eba002,'001' eba083,'100' eba084 From dual
4 Union
5 Select '002','101','200' From dual
6 Union
7 Select '002','201','250' From dual
8 Union
9 Select '002','301','400' From dual
10 Union
11 Select '003','001','200' From dual
12 Union
13 Select '003','300','400' From dual
14 Union
15 Select '003','500','600' From dual
16 )
17 Select Distinct eba002,decode(a,1,Min(eba083) over (Partition By eba002,a),eba083),
18 decode(a,1,max(eba084) over (Partition By eba002,a),eba084) From (
19 Select eba002,eba083,eba084,
20 eba083 - lag(eba084,1,eba083-1) over (Partition By eba002 Order By eba083) a
21 From x
22 )
23 /
EBA002 DECODE(A,1,MIN(EBA083)OVER(PAR DECODE(A,1,MAX(EBA084)OVER(PAR
------ ------------------------------ ------------------------------
002 001 250
002 301 400
003 001 200
003 300 400
003 500 600
SQL>
[/PHP] |
|