建个测试表:
create table t_num(id integer,num number);
insert into t_num values(1,100);
insert into t_num values(2,90);
insert into t_num values(3,0);
insert into t_num values(4,10);
insert into t_num values(5,20);
insert into t_num values(6,50);
insert into t_num values(7,0);
insert into t_num values(8,10);
insert into t_num values(9,20);
commit;
表内容如下:
SQL> select * from t_num;
ID NUM
---------- ----------
1 100
2 90
3 0
4 10
5 20
6 50
7 0
8 10
9 20
要求:记录按顺序显示,但只显示最后一个NUM为0记录后面的记录(不显示0记录)。这样一个查询SQL该如何写。数据库为Oracle数据库。一般情况下记录不多。
想了一个办法,不太好,有没有人有更好的办法?
select id,num from (
select id,num,
sum(case when num=0 then 0 else 1 end) over(order by id desc rows between unbounded preceding and 0 following)
- row_number() over (order by id desc) judge_code
from t_num
) where judge_code=0 order by id
查询SQL求解
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- iteye_7115 2012-07-26 10:46关注
select *
from t_num t
where t.id > (select max(id) from t_num tn where tn.num = 0);本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发
- ¥15 oracle 多个括号,怎么删除指定的括号及里面的内容?
- ¥15 poly函数表达式的看法
- ¥15 我想问一下有人熟悉思科packet tracer
- ¥15 小新14API2019想用bios调风扇
- ¥15 Python和易语言爬虫探讨
- ¥15 有Sakai 会吗,没做过,需要交
- ¥20 使用vba登录公司统一登录的网页
- ¥15 opensea上传人机验证不了
- ¥15 RK3568 kernel5.10