id bill_id amt
1 1 100
1 2 200
1 3 300
1 5 400
1 7 500
2 3 100
2 4 100
3 1 100需求:找出每一员工所做的订单号连续的订单中的第一条记录,应该输出:id bill_no amt
1 1 100
1 5 400
1 7 500
2 3 100
3 1 100
1 1 100
1 2 200
1 3 300
1 5 400
1 7 500
2 3 100
2 4 100
3 1 100需求:找出每一员工所做的订单号连续的订单中的第一条记录,应该输出:id bill_no amt
1 1 100
1 5 400
1 7 500
2 3 100
3 1 100
解决方案 »
- SQL问题 求救
- 存储过程的导出路径?
- 请教PLSQL的问题 ,
- to null 錯誤碼
- 在linux下向oracle数据库中存中文字符的问题
- 列表分区问题
- 在Oracle中哪里可以设置timeout的时间? 即用户端连接Oracle在一定的时间内如果没有任何对数据库的操作就自动断开连接?
- 一个比较弱的问题,oracle的字符集和数据库的字符集有关系么?
- 触发器运行不能,各位大哥指导一下
- 关于在RedHat2.1As上安装Oracle9i(9.0.1)的错误问题,帮帮我吧,火烧眉毛了!
- --求大虾帮忙:将SQL Server存储过程转换为Oracle的!---
- 用JBuilder怎么连接Oracle数据库啊??
union all select 1,2,200 from dual
union all select 1,3,300 from dual
union all select 1,5,400 from dual
union all select 1,7,500 from dual
union all select 2,3,100 from dual
union all select 2,4,100 from dual
union all select 3,1,100 from dual)select id,min(bill_id),min(amt)keep(dense_rank first order by bill_id)amt
from(
select tt.*,row_number()over(partition by id order by bill_id)rn
from tt)
group by id,bill_id-rn
select 1 id ,1 bill_id, 100 amt from dual
union all
select 1 ,2, 200 from dual
union all
select 1 ,3, 300 from dual
union all
select 1 ,5 ,400 from dual
union all
select 1 ,7 ,500 from dual
union all
select 2, 3 ,100 from dual
union all
select 2 ,4 ,100 from dual
)
--取出后面连续的记录的第一条
select id,BILL_ID,AMT from(
SELECT B.ID,B.BILL_ID,B.AMT,lead (b.bill_id) over (partition by b.id order by b.id) nextid FROM BILL B
) c
where c.nextid-bill_id <>1
union all
select * from bill where rownum <2
1 1 100
1 2 200
1 3 300
1 5 400
1 7 500
2 3 100
2 4 100运行上面语句后,结果
ID BILL_ID AMT
1 3 300
1 5 400
1 1 100
你先试试吧,里面有进行排序,没有规律不要紧
SELECT B.ID,B.BILL_ID,B.AMT,nvl(lag (b.bill_id) over (partition by b.id order by b.id),-1) nextid FROM BILL B
) c
where bill_id-c.nextid <> 1这个正解了:
ID BILL_ID AMT
1 1 100
1 5 400
1 7 500
2 3 100
select 1 id,1 bill_id,100 amt from dual
union all select 1,2,200 from dual
union all select 1,3,300 from dual
union all select 1,5,400 from dual
union all select 1,7,500 from dual
union all select 2,3,100 from dual
union all select 2,4,100 from dual
union all select 3,1,100 from dual
)
select id,bill_id,amt from(
select id,bill_id,amt, nvl(lag (bill_id) over (partition by id order by id),-1) lastrn from temp
) where bill_id - lastrn <> 1