当前位置: 数据库>oracle
本页文章导读:
▪oracle的分析函数over 及开窗函数 eg: 相关解析:表t_pi_part 字段 id code namevalue 1 222 avalue 2 222 bvalue 3 333 c给code相同的part code 添加行.........
▪Oracle 11g中CTE应用示例 关于SQL SERVER中的CTE中的CTE应用,请看这里:http://www.cnblogs.com/downmoon/archive/2009/10/23/1588405.html其实,ORACLE的CTE语法完全一样,看示例:一、创建示例数据表如下: 1 declare 2 tableExistedCount nu.........
▪oracle全托管驱动Oracle.ManagedDataAccess 终于见到了oracle官方的托管驱动,发布只需一个6M多的dll,支持EF 支持分布式事务附:Download ODP.NET, Managed Driver .zip file to a directory for staging the install.Unzip the download to expand its contents into the target.........
[1]oracle的分析函数over 及开窗函数
eg: 相关解析:
表t_pi_part
字段 id code name
value 1 222 a
value 2 222 b
value 3 333 c
给code相同的part code 添加行标,根据id 排序
select p.* ,row_number()over(partition by p.code order order by a.id desc) as row_index from t_pi_part p;
一:分析函数over
Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是
对于每个组返回多行,而聚合函数对于每个组只返回一行。
下面通过几个例子来说明其应用。
Download ODP.NET, Managed Driver .zip file to a directory for staging the install. Unzip the download to expand its contents into the target directory. Run configure.bat to GAC and configure machine.config for ODP.NET, Managed Driver. Create a new Visual Studio 2010 console application project for C#. Add Oracle.ManagedDataAccess.dll as a reference to the project. Replace the contents of Program.cs with the following C# code. Notice that the namespace of ODP.NET, Managed Driver (Oracle.ManagedDataAccess.*) is different from the namespace of ODP.NET, Unmanaged Driver (Oracle.DataAccess.*).
Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是
对于每个组返回多行,而聚合函数对于每个组只返回一行。
下面通过几个例子来说明其应用。
1:统计某商店的营业额。
date sale
1 20
2 15
3 14
4 18
5 30
规则:按天统计:每天都统计前面几天的总额
得到的结果:
DATE SALE SUM
----- -------- ------
1 20 20 --1天
2 15 35 --1天+2天
3 14 49 --1天+2天+3天
4 18 67 .
5 30 97 .
2:统计各班成绩第一名的同学信息
1 20
2 15
3 14
4 18
5 30
规则:按天统计:每天都统计前面几天的总额
得到的结果:
DATE SALE SUM
----- -------- ------
1 20 20 --1天
2 15 35 --1天+2天
3 14 49 --1天+2天+3天
4 18 67 .
5 30 97 .
2:统计各班成绩第一名的同学信息
NAME CLASS S
----- ----- ----------------------
fda 1 80
ffd 1 78
dss 1 95
cfe 2 74
gds 2 92
gf 3 99
ddd 3 99
adf 3 45
asdf 3 55
3dd 3 78
通过:
--
select * from
(
select name,class,s,rank()over(partition by class order by s desc) mm from t2
)
where mm=1
--
得到结果:
NAME CLASS S MM
----- ----- ---------------------- ----------------------
dss 1 95 1
gds 2 92 1
gf 3 99 1
ddd 3 99 1
注意:
1.在求第一名成绩的时候,不能用row_number(),因为如果同班有两个并列第一,row_number()只返回一个结果
2.rank()和dense_rank()的区别是:
&
----- ----- ----------------------
fda 1 80
ffd 1 78
dss 1 95
cfe 2 74
gds 2 92
gf 3 99
ddd 3 99
adf 3 45
asdf 3 55
3dd 3 78
通过:
--
select * from
(
select name,class,s,rank()over(partition by class order by s desc) mm from t2
)
where mm=1
--
得到结果:
NAME CLASS S MM
----- ----- ---------------------- ----------------------
dss 1 95 1
gds 2 92 1
gf 3 99 1
ddd 3 99 1
注意:
1.在求第一名成绩的时候,不能用row_number(),因为如果同班有两个并列第一,row_number()只返回一个结果
2.rank()和dense_rank()的区别是:
&
[2]Oracle 11g中CTE应用示例
关于SQL SERVER中的CTE中的CTE应用,请看这里:http://www.cnblogs.com/downmoon/archive/2009/10/23/1588405.html
其实,ORACLE的CTE语法完全一样,看示例:
一、创建示例数据表如下:
1 declare
2 tableExistedCount number;
3 begin
4 select count(1) into tableExistedCount from user_tables where TABLE_NAME ='DemoOrganization';
5 if tableExistedCount =1 then
6 execute immediate ' drop table DemoOrganization cascade constraints';
7 end if;
8 end;
9
10 /*==============================================================*/
11 /* Table: DemoOrganization */
12 /*==============================================================*/
13 create table DemoOrganization
14 (
15 OrgID NUMBER(20,0) not null,
16 OrgCode VARCHAR2(100),
17 OrgName NVARCHAR2(100),
18 OrgPath VARCHAR2(500),
19 ParentID INTEGER,
20 OLevel INTEGER default 0,
21 OrderID NUMBER(10,0),
22 CurState INTEGER default 0,
23 AddUser VARCHAR2(50),
24 AddTime DATE,
25 constraint PK_DEMOORGANIZATION primary key (OrgID)
26 );
27
28 comment on table DemoOrganization is
29 '演示组织机构';
30
31 comment on column DemoOrganization.OrgID is
32 '机构ID';
33
34 comment on column DemoOrganization.OrgCode is
35 '机构编码';
36
37 comment on column DemoOrganization.OrgName is
38 '机构名称';
39
40 comment on column DemoOrganization.OrgPath is
41 '机构路径';
42
43 comment on column DemoOrganization.ParentID is
44 '上级ID';
45
46 comment on column DemoOrganization.OLevel is
47 '级别';
48
49 comment on column DemoOrganization.OrderID is
50 '排序';
51
52 comment on column DemoOrganization.CurState is
53 '当前状态';
54
2 tableExistedCount number;
3 begin
4 select count(1) into tableExistedCount from user_tables where TABLE_NAME ='DemoOrganization';
5 if tableExistedCount =1 then
6 execute immediate ' drop table DemoOrganization cascade constraints';
7 end if;
8 end;
9
10 /*==============================================================*/
11 /* Table: DemoOrganization */
12 /*==============================================================*/
13 create table DemoOrganization
14 (
15 OrgID NUMBER(20,0) not null,
16 OrgCode VARCHAR2(100),
17 OrgName NVARCHAR2(100),
18 OrgPath VARCHAR2(500),
19 ParentID INTEGER,
20 OLevel INTEGER default 0,
21 OrderID NUMBER(10,0),
22 CurState INTEGER default 0,
23 AddUser VARCHAR2(50),
24 AddTime DATE,
25 constraint PK_DEMOORGANIZATION primary key (OrgID)
26 );
27
28 comment on table DemoOrganization is
29 '演示组织机构';
30
31 comment on column DemoOrganization.OrgID is
32 '机构ID';
33
34 comment on column DemoOrganization.OrgCode is
35 '机构编码';
36
37 comment on column DemoOrganization.OrgName is
38 '机构名称';
39
40 comment on column DemoOrganization.OrgPath is
41 '机构路径';
42
43 comment on column DemoOrganization.ParentID is
44 '上级ID';
45
46 comment on column DemoOrganization.OLevel is
47 '级别';
48
49 comment on column DemoOrganization.OrderID is
50 '排序';
51
52 comment on column DemoOrganization.CurState is
53 '当前状态';
54
[3]oracle全托管驱动Oracle.ManagedDataAccess
终于见到了oracle官方的托管驱动,发布只需一个6M多的dll,支持EF 支持分布式事务
附:
// C#
using System;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
namespace Connect
{
class Program
{
static void Main(string[] args)
{
try
{
// Please replace the connection string attribute settings
string constr = "user id=scott;password=tiger;data source=oracle";
OracleConnection con = new OracleConnection(constr);
con.Open();
Console.WriteLine("Connected to Oracle Database {0}", con.ServerVersion);
con.Dispose();
Console.WriteLine("Press RETURN to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Error : {0}", ex);
}
}
}
}
using System;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
namespace Connect
{
class Program
{
static void Main(string[] args)
{
try
{
// Please replace the connection string attribute settings
string constr = "user id=scott;password=tiger;data source=oracle";
OracleConnection con = new OracleConnection(constr);
con.Open();
Console.WriteLine("Connected to Oracle Database {0}", con.ServerVersion);
con.Dispose();
Console.WriteLine("Press RETURN to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Error : {0}", ex);
}
}
}
}
本文链接
最新技术文章:
 
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!
©2012-2021,