Oracle数据库分析函数应用实例之查找状态全为1的ID是本文我们主要要介绍的内容,通过本文的例子让我们来一起了解一下Oracle数据库分析函数的使用吧,希望能够对您有所帮助。
实例如下:
1、表结构和测试数据插入
建表:
- create table TAB_FXHS
- (
- id VARCHAR2(32),
- zt VARCHAR2(2)
- );
-- Add comments to the table
- comment on table TAB_FXHS
- is '用于统计状态值全为正常的id值';
-- Add comments to the columns
- comment on column TAB_FXHS.id
- is '主键ID';
- comment on column TAB_FXHS.zt
- is '状态';
插入测试数据:
- prompt Importing table TAB_FXHS...
- set feedback off
- set define off
- insert into TAB_FXHS (ID, ZT)values ('10125', '0');
- insert into TAB_FXHS (ID, ZT)values ('10161', '0');
- insert into TAB_FXHS (ID, ZT)values ('10141', '0');
- insert into TAB_FXHS (ID, ZT)values ('10126', '1');
- insert into TAB_FXHS (ID, ZT)values ('10102', '0');
- insert into TAB_FXHS (ID, ZT)values ('10103', '0');
- insert into TAB_FXHS (ID, ZT)values ('10121', '0');
- insert into TAB_FXHS (ID, ZT)values ('10121', '1');
- insert into TAB_FXHS (ID, ZT)values ('10121', '0');
- insert into TAB_FXHS (ID, ZT)values ('10121', '3');
- prompt Done.
2、 功能需求说明
在表TAB_FXHS中,ID是关键字段,ZT是状态。
ID中可能会有重复的值,现在要求找出ZT全为1的所有ID值。
3、功能实现的SQL语句
- WITH ZT_PARTITION_BY_ID AS
- (SELECT ID, ZT, COUNT(ZT) OVER(PARTITION BY ID ORDER BY ID) ID_ZT
- FROM TAB_FXHS
- GROUP BY ID, ZT)
- SELECT *
- FROM ZT_PARTITION_BY_ID
- WHERE ID_ZT = 1
- AND ZT = 1;
以上就是Oracle数据库中分析函数的应用实例之实现查找状态全为1的ID的全部过程,本文就介绍到这里了,希望本次的介绍能够对您有所收获!
【编辑推荐】