首页 > 数据库 > Oracle > 常用sql书写示例2
检索出第M至N的记录。
select b.seq,b.dept_name
from (select rownum
seq,a.*
from t_dept a
where rownum<=100
) b
where seq>97 and seq<=100
结果为
SEQ DEPT_NAME
98 广州天河区二部六组
99 广州天河区十一部九组
100 广州天河区十二部十组
其中内层rownum<=100是必须的。
例1、
select count(*)
from t_dept
where organ_id='101'
and rownum < 2; --该句省略时,若表内记录数很多则效率很低
例2、
假设t_dept.status非空
select min(status) status
from t_dept
where dept_code='1';
结果为空表示无记录,否则表示该记录的状态。
若仅用来执行UPDATE,可设置INSERT为必违反约束的语句。
merge into t_customer a
using ( select *
from
t_proposal_customer m
where m.gender='M'
) b
on (a.customer_id=b.customer_id)
when matched then
update set
a.real_name=b.real_name,
a.birthday=b.birthday
when not matched then
insert(customer_id,real_name,birthday)
values(s_customer__customer_id.nextval,b.real_name,b.birthday)
窗口函数使在明细查询时能以更高的效率执行聚类查询。
select item_id,policy_id,period_prem,
sum(period_prem) over(partition
by policy_id) policy_prem,
sum(period_prem) over(partition
by policy_id order by item_id) accu_prem
from t_policy_product
where policy_id in (73,74)
结果为
ITEM_ID POLICY_ID PERIOD_PREM POLICY_PREM ACCU_PREM
114 73 1115.00 1526 1115
115 73 321.00 1526 1436
116 73 90.00 1526 1526
117 74 3336.00 3572 3336
118 74 236.00 3572 3572
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3618/viewspace-1032901/ ,如需转载,请注明出处,否则将追究法律责任。