- 浏览: 1763377 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (520)
- Oracle (10)
- Oracle错误集 (8)
- Oracle安装升级 (15)
- Oracle日常管理 (51)
- Oracle字符集 (7)
- Oracle备份恢复 (24)
- Oracle优化 (4)
- Oracle编程 (52)
- Oracle导入导出 (19)
- Oracle体系结构 (15)
- Oracle网络 (2)
- Oracle安全 (2)
- Oracle权限 (3)
- Oracle数据字典和性能视图 (2)
- Oracle常用地址 (5)
- SQLPLUS专栏 (7)
- SqlServer (13)
- SqlServer2005编程 (27)
- SqlServer2005管理 (15)
- MySQL (20)
- Dorado应用 (1)
- C# (24)
- Arcgis Server开发 (20)
- ArcSDE技术 (19)
- UML学习 (2)
- 设计模式 (2)
- JAVA EE (4)
- JavaScript (3)
- OFBIZ (27)
- JAVA WEB开发 (22)
- Linux&Unix (34)
- SHELL编程 (14)
- C语言 (11)
- 网络协议 (14)
- FREEMARKER (2)
- GROOVY (2)
- JAVA语言 (3)
- 防火墙 (0)
- PHP (2)
- Apache (2)
- Loader Runner (1)
- Nginx (3)
- 数据库理论 (2)
- maven (1)
最新评论
-
怼怼怼怼:
oracle的timestamp类型使用 -
怼怼怼怼:
oracle的timestamp类型使用 -
怼怼怼怼:
oracle的timestamp类型使用 -
pg_guo:
感谢
oracle中查看用户权限 -
xu234234:
5、MapResourceManager控件中添加了两个服务, ...
北京ArcGis Server应用基础培训笔记1
1、MAX数据类型,结合Update.write方法进行更新(If the target LOB is NULL,an update that uses WRITE will fail)
CREATE TABLE test
(
tid INT NOT NULL PRIMARY KEY,
tname VARCHAR(MAX) NULL
)
INSERT INTO test(tid,tname) VALUES(102, 'Customer 102 text data')
select * from test
UPDATE test SET tname.WRITE('one hundred and two', 9, 3) WHERE tid = 102
select * from test
2、Top增强
--create a table and insert some data
use demo
go
CREATE TABLE toptest (column1 VARCHAR(150))
go
INSERT INTO toptest VALUES('t1')
INSERT INTO toptest VALUES('t2')
INSERT INTO toptest VALUES('t3')
INSERT INTO toptest VALUES('t4')
INSERT INTO toptest VALUES('t5')
INSERT INTO toptest VALUES('t6')
INSERT INTO toptest VALUES('t7')
INSERT INTO toptest VALUES('t8')
select * from toptest
go
CREATE TABLE toptest2 (column2 VARCHAR(150))
go
INSERT INTO toptest2 VALUES('c1')
INSERT INTO toptest2 VALUES('c2')
--declare 3 variables
DECLARE @a INT
DECLARE @b INT
DECLARE @c INT
--set values
SET @a = 10
SET @b = 5
SELECT @c = @a/@b
--use the calculated expression
SELECT TOP(@c) * FROM toptest
--use a SELECT statement as expression
SELECT TOP(SELECT COUNT(*) FROM toptest2) *
FROM toptest
--DML top
DELETE TOP(2) toptest where column1>'t6'
--this sets 't1' and 't2' to 'hi'
UPDATE TOP(2) toptest SET column1 = 'hi' where column1<='t2'
SELECT * FROM toptest
3、Output语句
--create table and insert data
use demo
go
CREATE TABLE tt
(id INT IDENTITY, c1 VARCHAR(15))
go
INSERT INTO tt VALUES ('r1')
INSERT INTO tt VALUES ('r2')
INSERT INTO tt VALUES ('r5')
INSERT INTO tt VALUES ('r6')
INSERT INTO tt VALUES ('r7')
INSERT INTO tt VALUES ('r8')
INSERT INTO tt VALUES ('r9')
INSERT INTO tt VALUES ('r10')
--make a table variable to hold the results of the OUTPUT clause
DECLARE @del AS TABLE (deletedId INT, deletedValue VARCHAR(15))
DELETE tt
OUTPUT DELETED.id, DELETED.c1 INTO @del
WHERE id < 3
SELECT * FROM @del
GO
4、Try and Catch 功能
USE demo
GO
--creating worktables
CREATE TABLE student
(
stuid int NOT NULL PRIMARY KEY,
stuname varchar(50)
)
CREATE TABLE score
(
stuid int NOT NULL REFERENCES student(stuid),
score int
)
GO
INSERT INTO student VALUES (101,'zhangsan')
INSERT INTO student VALUES (102,'wangwu')
INSERT INTO student VALUES (103,'lishi')
INSERT INTO student VALUES (104,'maliu')
--Invoking a run-time error
SET XACT_ABORT OFF
BEGIN TRAN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107, 76) /* Foreign Key Error */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
COMMIT TRAN
go
select * from student
select * from score
--Using the try..catch.. construct and invoking a run-time error
SET XACT_ABORT OFF
BEGIN TRY
BEGIN TRAN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107, 76) /* Foreign Key Error */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
COMMIT TRAN
PRINT 'Transaction committed'
END TRY
BEGIN CATCH
ROLLBACK
PRINT 'Transaction rolled back'
SELECT ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_MESSAGE() as ErrorMessage;
END CATCH
GO
SELECT * FROM score
GO
5、Apply语句
使用 APPLY 运算符可以为实现查询操作的外部表表达式返回的每个行调用表值函数。表值函数作为右输入,外部表表达式作为左输入。通过对右输入求值来获得左输入每一行的计算结果,生成的行被组合起来作为最终输出。APPLY 运算符生成的列的列表是左输入中的列集,后跟右输入返回的列的列表。
APPLY 有两种形式: CROSS APPLY 和 OUTER APPLY。CROSS APPLY 仅返回外部表中通过表值函数生成结果集的行。OUTER APPLY 既返回生成结果集的行,也返回不生成结果集的行,其中表值函数生成的列中的值为 NULL。
例子:
use demo
go
CREATE TABLE Arrays
(
aid INT NOT NULL IDENTITY PRIMARY KEY,
array VARCHAR(7999) NOT NULL
)
go
INSERT INTO Arrays VALUES('')
INSERT INTO Arrays VALUES('10')
INSERT INTO Arrays VALUES('20,40,30')
INSERT INTO Arrays VALUES('-1,-3,-5')
GO
CREATE FUNCTION function1(@arr AS VARCHAR(7999))
RETURNS @t TABLE(pos INT NOT NULL, value INT NOT NULL)
AS
BEGIN
DECLARE @end AS INT, @start AS INT, @pos AS INT
SELECT @arr = @arr + ',', @pos = 1,
@start = 1, @end = CHARINDEX(',', @arr, @start)
WHILE @end > 1
BEGIN
INSERT INTO @t VALUES(@pos, SUBSTRING(@arr, @start, @end - @start))
SELECT @pos = @pos + 1,
@start = @end + 1, @end = CHARINDEX(',', @arr, @start)
END
RETURN
END
--test
select * from function1('200,400,300')
go
SELECT A.aid, F.*
FROM Arrays AS A
CROSS APPLY function1(array) AS F
go
SELECT A.aid, F.*
FROM Arrays AS A
OUTER APPLY function1(array) AS F
GO
例子2 :为每个消费者返回最新的两个订单
select c.customerid,city,orderid from customers c outer apply (select top 2 orderid,customerid from orders as o where o.customerid=c.customerid) as CA
6、数据库级触发器
use demo
go
CREATE TRIGGER prevent_drop_table ON DATABASE FOR DROP_TABLE
AS
RAISERROR('Not allowed to drop tables.', 10, 1)
PRINT 'DROP TABLE attempt in database ' + DB_NAME() + '.'
PRINT CONVERT (nvarchar (1000),EventData())
ROLLBACK
GO
-- test
CREATE TABLE TestDROP(col1 INT)
go
INSERT INTO TestDROP VALUES(1)
drop talbe testdrop
-- Server
CREATE TRIGGER audit_ddl_logins ON ALL SERVER
FOR CREATE_LOGIN, ALTER_LOGIN, DROP_LOGIN
AS
PRINT 'DDL LOGIN took place.'
PRINT CONVERT (nvarchar (1000),EventData())
GO
-- test
CREATE LOGIN login1 WITH PASSWORD = '123'
ALTER LOGIN login1 WITH PASSWORD = 'xyz'
DROP LOGIN login1
7、ROW_NUMBER()、RANK()、DENSE_RANK()函数
ROW_NUMBER:返回结果集分区内行的序列号,每个分区的第一行从 1 开始。
RANK:返回结果集的分区内每行的排名。行的排名是相关行之前的排名数加一。
DENSE_RANK:返回结果集分区中行的排名,在排名中没有任何间断。行的排名等于所讨论行之前的所有排名数加一。
NTILE(integer_expression):将有序分区中的行分发到指定数目的组中。各个组有编号,编号从一开始。对于每一个行,NTILE 将返回此行所属的组的编号。
例子:
use demo
go
create table rankorder
(orderid int,
qty int
)
go
insert rankorder values(30001,10)
insert rankorder values(10001,10)
insert rankorder values(10006,10)
insert rankorder values(40005,10)
insert rankorder values(30003,15)
insert rankorder values(30004,20)
insert rankorder values(20002,20)
insert rankorder values(20001,20)
insert rankorder values(10005,30)
insert rankorder values(30007,30)
insert rankorder values(40001,40)
go
SELECT orderid,qty,
ROW_NUMBER() OVER(ORDER BY qty) AS rownumber,
RANK() OVER(ORDER BY qty) AS rank,
DENSE_RANK() OVER(ORDER BY qty) AS denserank ,
ntile(2) over(order by qty) as ntile1
FROM rankorder
ORDER BY qty
8、快照隔离
create database demo2
go
use demo2
alter database demo2 set allow_snapshot_isolation on
create table test
( tid int not null primary key,
tname varchar(50) not null
)
insert into test values(1,'version1')
insert into test values(2,'version2')
--connection 1
use demo2
begin tran
update test set tname='version3' where tid=2
select * from test
--connection 2
use demo2
set transaction isolation level snapshot
select * from test
--it will ok, you can see it
9、Top字句功能增强
--create a table and insert some data
use demo
go
CREATE TABLE toptest (column1 VARCHAR(150))
go
INSERT INTO toptest VALUES('t1')
INSERT INTO toptest VALUES('t2')
INSERT INTO toptest VALUES('t3')
INSERT INTO toptest VALUES('t4')
INSERT INTO toptest VALUES('t5')
INSERT INTO toptest VALUES('t6')
INSERT INTO toptest VALUES('t7')
INSERT INTO toptest VALUES('t8')
select * from toptest
go
CREATE TABLE toptest2 (column2 VARCHAR(150))
go
INSERT INTO toptest2 VALUES('c1')
INSERT INTO toptest2 VALUES('c2')
--declare 3 variables
DECLARE @a INT
DECLARE @b INT
DECLARE @c INT
--set values
SET @a = 10
SET @b = 5
SELECT @c = @a/@b
--在 TOP 中使用变量
SELECT TOP(@c) * FROM toptest
--在 TOP 中select 表达式
SELECT TOP(SELECT COUNT(*) FROM toptest2) *
FROM toptest
--DML top
DELETE TOP(2) toptest where column1>'t6'
--this sets 't1' and 't2' to 'hi'
UPDATE TOP(2) toptest SET column1 = 'hi' where column1<='t2'
--在 TOP 中使用 PERCENT 和 WITH TIES
以下示例获取所有雇员中薪水最高的 10% 的员工,并根据基本薪金按降序返回。
指定 WITH TIES 可确保结果集中同时包含其薪水与返回的最低薪水相同的所有雇员,
即使这样返回的雇员数会超过总数的 10% 也是这样。
USE AdventureWorks ;
GO
SELECT TOP(10) PERCENT WITH TIES
EmployeeID, Title, DepartmentID, Gender, BaseRate
FROM HumanResources.Employee
ORDER BY BaseRate DESC;
10、行列转换 Pivot Unpivot
use demo
go
create table orders
(Customer varchar(10) not null,
product varchar(20) not null,
quantity int not null)
go
insert orders values('Mike', 'Bike',3)
insert orders values('Mike','Chain',2)
insert orders values('Mike','Bike',5)
insert orders values('Lisa','Bike',3)
insert orders values('Lisa','Chain',3)
insert orders values('Lisa','Chain',4)
insert orders values('Lisa','Bike',2)
select * from orders
select * from orders
pivot (sum(quantity) for product in ([Bike],[Chain])) as a
use demo
go
CREATE TABLE SALES1
(
[Year] INT,
Quarter CHAR(2),
Amount FLOAT
)
GO
INSERT INTO SALES1 VALUES (2001, 'Q1', 80)
INSERT INTO SALES1 VALUES (2001, 'Q2', 70)
INSERT INTO SALES1 VALUES (2001, 'Q3', 55)
INSERT INTO SALES1 VALUES (2001, 'Q3', 110)
INSERT INTO SALES1 VALUES (2001, 'Q4', 90)
INSERT INTO SALES1 VALUES (2002, 'Q1', 200)
INSERT INTO SALES1 VALUES (2002, 'Q2', 150)
INSERT INTO SALES1 VALUES (2002, 'Q2', 40)
INSERT INTO SALES1 VALUES (2002, 'Q2', 60)
INSERT INTO SALES1 VALUES (2002, 'Q3', 120)
INSERT INTO SALES1 VALUES (2002, 'Q3', 110)
INSERT INTO SALES1 VALUES (2002, 'Q4', 180)
GO
SELECT * FROM SALES1
PIVOT
(SUM (Amount) --Aggregate the Amount column using SUM
FOR [Quarter] --Pivot the Quarter column into column headings
IN (Q1, Q2, Q3, Q4)) --use these quarters
AS P
GO
select * into temp1 from orders
pivot (sum(quantity) for product in ([Bike],[Chain])) as a --in 里面的用[],而不是用''
select * from temp1
select customer, product,quantity
froam temp1
unpivot(quantity for product in ([Bike],[Chain])) as a
CREATE TABLE test
(
tid INT NOT NULL PRIMARY KEY,
tname VARCHAR(MAX) NULL
)
INSERT INTO test(tid,tname) VALUES(102, 'Customer 102 text data')
select * from test
UPDATE test SET tname.WRITE('one hundred and two', 9, 3) WHERE tid = 102
select * from test
2、Top增强
--create a table and insert some data
use demo
go
CREATE TABLE toptest (column1 VARCHAR(150))
go
INSERT INTO toptest VALUES('t1')
INSERT INTO toptest VALUES('t2')
INSERT INTO toptest VALUES('t3')
INSERT INTO toptest VALUES('t4')
INSERT INTO toptest VALUES('t5')
INSERT INTO toptest VALUES('t6')
INSERT INTO toptest VALUES('t7')
INSERT INTO toptest VALUES('t8')
select * from toptest
go
CREATE TABLE toptest2 (column2 VARCHAR(150))
go
INSERT INTO toptest2 VALUES('c1')
INSERT INTO toptest2 VALUES('c2')
--declare 3 variables
DECLARE @a INT
DECLARE @b INT
DECLARE @c INT
--set values
SET @a = 10
SET @b = 5
SELECT @c = @a/@b
--use the calculated expression
SELECT TOP(@c) * FROM toptest
--use a SELECT statement as expression
SELECT TOP(SELECT COUNT(*) FROM toptest2) *
FROM toptest
--DML top
DELETE TOP(2) toptest where column1>'t6'
--this sets 't1' and 't2' to 'hi'
UPDATE TOP(2) toptest SET column1 = 'hi' where column1<='t2'
SELECT * FROM toptest
3、Output语句
--create table and insert data
use demo
go
CREATE TABLE tt
(id INT IDENTITY, c1 VARCHAR(15))
go
INSERT INTO tt VALUES ('r1')
INSERT INTO tt VALUES ('r2')
INSERT INTO tt VALUES ('r5')
INSERT INTO tt VALUES ('r6')
INSERT INTO tt VALUES ('r7')
INSERT INTO tt VALUES ('r8')
INSERT INTO tt VALUES ('r9')
INSERT INTO tt VALUES ('r10')
--make a table variable to hold the results of the OUTPUT clause
DECLARE @del AS TABLE (deletedId INT, deletedValue VARCHAR(15))
DELETE tt
OUTPUT DELETED.id, DELETED.c1 INTO @del
WHERE id < 3
SELECT * FROM @del
GO
4、Try and Catch 功能
USE demo
GO
--creating worktables
CREATE TABLE student
(
stuid int NOT NULL PRIMARY KEY,
stuname varchar(50)
)
CREATE TABLE score
(
stuid int NOT NULL REFERENCES student(stuid),
score int
)
GO
INSERT INTO student VALUES (101,'zhangsan')
INSERT INTO student VALUES (102,'wangwu')
INSERT INTO student VALUES (103,'lishi')
INSERT INTO student VALUES (104,'maliu')
--Invoking a run-time error
SET XACT_ABORT OFF
BEGIN TRAN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107, 76) /* Foreign Key Error */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
COMMIT TRAN
go
select * from student
select * from score
--Using the try..catch.. construct and invoking a run-time error
SET XACT_ABORT OFF
BEGIN TRY
BEGIN TRAN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107, 76) /* Foreign Key Error */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
COMMIT TRAN
PRINT 'Transaction committed'
END TRY
BEGIN CATCH
ROLLBACK
PRINT 'Transaction rolled back'
SELECT ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_MESSAGE() as ErrorMessage;
END CATCH
GO
SELECT * FROM score
GO
5、Apply语句
使用 APPLY 运算符可以为实现查询操作的外部表表达式返回的每个行调用表值函数。表值函数作为右输入,外部表表达式作为左输入。通过对右输入求值来获得左输入每一行的计算结果,生成的行被组合起来作为最终输出。APPLY 运算符生成的列的列表是左输入中的列集,后跟右输入返回的列的列表。
APPLY 有两种形式: CROSS APPLY 和 OUTER APPLY。CROSS APPLY 仅返回外部表中通过表值函数生成结果集的行。OUTER APPLY 既返回生成结果集的行,也返回不生成结果集的行,其中表值函数生成的列中的值为 NULL。
例子:
use demo
go
CREATE TABLE Arrays
(
aid INT NOT NULL IDENTITY PRIMARY KEY,
array VARCHAR(7999) NOT NULL
)
go
INSERT INTO Arrays VALUES('')
INSERT INTO Arrays VALUES('10')
INSERT INTO Arrays VALUES('20,40,30')
INSERT INTO Arrays VALUES('-1,-3,-5')
GO
CREATE FUNCTION function1(@arr AS VARCHAR(7999))
RETURNS @t TABLE(pos INT NOT NULL, value INT NOT NULL)
AS
BEGIN
DECLARE @end AS INT, @start AS INT, @pos AS INT
SELECT @arr = @arr + ',', @pos = 1,
@start = 1, @end = CHARINDEX(',', @arr, @start)
WHILE @end > 1
BEGIN
INSERT INTO @t VALUES(@pos, SUBSTRING(@arr, @start, @end - @start))
SELECT @pos = @pos + 1,
@start = @end + 1, @end = CHARINDEX(',', @arr, @start)
END
RETURN
END
--test
select * from function1('200,400,300')
go
SELECT A.aid, F.*
FROM Arrays AS A
CROSS APPLY function1(array) AS F
go
SELECT A.aid, F.*
FROM Arrays AS A
OUTER APPLY function1(array) AS F
GO
例子2 :为每个消费者返回最新的两个订单
select c.customerid,city,orderid from customers c outer apply (select top 2 orderid,customerid from orders as o where o.customerid=c.customerid) as CA
6、数据库级触发器
use demo
go
CREATE TRIGGER prevent_drop_table ON DATABASE FOR DROP_TABLE
AS
RAISERROR('Not allowed to drop tables.', 10, 1)
PRINT 'DROP TABLE attempt in database ' + DB_NAME() + '.'
PRINT CONVERT (nvarchar (1000),EventData())
ROLLBACK
GO
-- test
CREATE TABLE TestDROP(col1 INT)
go
INSERT INTO TestDROP VALUES(1)
drop talbe testdrop
-- Server
CREATE TRIGGER audit_ddl_logins ON ALL SERVER
FOR CREATE_LOGIN, ALTER_LOGIN, DROP_LOGIN
AS
PRINT 'DDL LOGIN took place.'
PRINT CONVERT (nvarchar (1000),EventData())
GO
-- test
CREATE LOGIN login1 WITH PASSWORD = '123'
ALTER LOGIN login1 WITH PASSWORD = 'xyz'
DROP LOGIN login1
7、ROW_NUMBER()、RANK()、DENSE_RANK()函数
ROW_NUMBER:返回结果集分区内行的序列号,每个分区的第一行从 1 开始。
RANK:返回结果集的分区内每行的排名。行的排名是相关行之前的排名数加一。
DENSE_RANK:返回结果集分区中行的排名,在排名中没有任何间断。行的排名等于所讨论行之前的所有排名数加一。
NTILE(integer_expression):将有序分区中的行分发到指定数目的组中。各个组有编号,编号从一开始。对于每一个行,NTILE 将返回此行所属的组的编号。
例子:
use demo
go
create table rankorder
(orderid int,
qty int
)
go
insert rankorder values(30001,10)
insert rankorder values(10001,10)
insert rankorder values(10006,10)
insert rankorder values(40005,10)
insert rankorder values(30003,15)
insert rankorder values(30004,20)
insert rankorder values(20002,20)
insert rankorder values(20001,20)
insert rankorder values(10005,30)
insert rankorder values(30007,30)
insert rankorder values(40001,40)
go
SELECT orderid,qty,
ROW_NUMBER() OVER(ORDER BY qty) AS rownumber,
RANK() OVER(ORDER BY qty) AS rank,
DENSE_RANK() OVER(ORDER BY qty) AS denserank ,
ntile(2) over(order by qty) as ntile1
FROM rankorder
ORDER BY qty
8、快照隔离
create database demo2
go
use demo2
alter database demo2 set allow_snapshot_isolation on
create table test
( tid int not null primary key,
tname varchar(50) not null
)
insert into test values(1,'version1')
insert into test values(2,'version2')
--connection 1
use demo2
begin tran
update test set tname='version3' where tid=2
select * from test
--connection 2
use demo2
set transaction isolation level snapshot
select * from test
--it will ok, you can see it
9、Top字句功能增强
--create a table and insert some data
use demo
go
CREATE TABLE toptest (column1 VARCHAR(150))
go
INSERT INTO toptest VALUES('t1')
INSERT INTO toptest VALUES('t2')
INSERT INTO toptest VALUES('t3')
INSERT INTO toptest VALUES('t4')
INSERT INTO toptest VALUES('t5')
INSERT INTO toptest VALUES('t6')
INSERT INTO toptest VALUES('t7')
INSERT INTO toptest VALUES('t8')
select * from toptest
go
CREATE TABLE toptest2 (column2 VARCHAR(150))
go
INSERT INTO toptest2 VALUES('c1')
INSERT INTO toptest2 VALUES('c2')
--declare 3 variables
DECLARE @a INT
DECLARE @b INT
DECLARE @c INT
--set values
SET @a = 10
SET @b = 5
SELECT @c = @a/@b
--在 TOP 中使用变量
SELECT TOP(@c) * FROM toptest
--在 TOP 中select 表达式
SELECT TOP(SELECT COUNT(*) FROM toptest2) *
FROM toptest
--DML top
DELETE TOP(2) toptest where column1>'t6'
--this sets 't1' and 't2' to 'hi'
UPDATE TOP(2) toptest SET column1 = 'hi' where column1<='t2'
--在 TOP 中使用 PERCENT 和 WITH TIES
以下示例获取所有雇员中薪水最高的 10% 的员工,并根据基本薪金按降序返回。
指定 WITH TIES 可确保结果集中同时包含其薪水与返回的最低薪水相同的所有雇员,
即使这样返回的雇员数会超过总数的 10% 也是这样。
USE AdventureWorks ;
GO
SELECT TOP(10) PERCENT WITH TIES
EmployeeID, Title, DepartmentID, Gender, BaseRate
FROM HumanResources.Employee
ORDER BY BaseRate DESC;
10、行列转换 Pivot Unpivot
use demo
go
create table orders
(Customer varchar(10) not null,
product varchar(20) not null,
quantity int not null)
go
insert orders values('Mike', 'Bike',3)
insert orders values('Mike','Chain',2)
insert orders values('Mike','Bike',5)
insert orders values('Lisa','Bike',3)
insert orders values('Lisa','Chain',3)
insert orders values('Lisa','Chain',4)
insert orders values('Lisa','Bike',2)
select * from orders
select * from orders
pivot (sum(quantity) for product in ([Bike],[Chain])) as a
use demo
go
CREATE TABLE SALES1
(
[Year] INT,
Quarter CHAR(2),
Amount FLOAT
)
GO
INSERT INTO SALES1 VALUES (2001, 'Q1', 80)
INSERT INTO SALES1 VALUES (2001, 'Q2', 70)
INSERT INTO SALES1 VALUES (2001, 'Q3', 55)
INSERT INTO SALES1 VALUES (2001, 'Q3', 110)
INSERT INTO SALES1 VALUES (2001, 'Q4', 90)
INSERT INTO SALES1 VALUES (2002, 'Q1', 200)
INSERT INTO SALES1 VALUES (2002, 'Q2', 150)
INSERT INTO SALES1 VALUES (2002, 'Q2', 40)
INSERT INTO SALES1 VALUES (2002, 'Q2', 60)
INSERT INTO SALES1 VALUES (2002, 'Q3', 120)
INSERT INTO SALES1 VALUES (2002, 'Q3', 110)
INSERT INTO SALES1 VALUES (2002, 'Q4', 180)
GO
SELECT * FROM SALES1
PIVOT
(SUM (Amount) --Aggregate the Amount column using SUM
FOR [Quarter] --Pivot the Quarter column into column headings
IN (Q1, Q2, Q3, Q4)) --use these quarters
AS P
GO
select * into temp1 from orders
pivot (sum(quantity) for product in ([Bike],[Chain])) as a --in 里面的用[],而不是用''
select * from temp1
select customer, product,quantity
froam temp1
unpivot(quantity for product in ([Bike],[Chain])) as a
发表评论
-
SQL Server 2005中处理表分区问题
2008-08-28 11:01 2006数据库性能调优是每一 ... -
SQL LIKE 通配符随笔
2008-07-04 09:26 1601通配符 说明 _ 与任意单字符匹配 % 与包含一 ... -
五种提高 SQL 性能的方法
2008-07-02 12:10 1122有时, 为了让应用程序运行得更快,所做的全部工作就是在这里或那 ... -
SQL操作全集
2008-07-02 12:01 1107SQL分类: DDL—数据定义语言(CREATE,ALTER, ... -
不同服务器数据库之间的数据操作
2008-07-02 11:56 1911--创建链接服务器 exec sp_addlinkedser ... -
远程连接操作
2008-07-02 11:52 1278--远程连接操作 /******************** ... -
合并分拆表
2008-07-02 11:46 1446--合并分拆表 /********************** ... -
行列互转
2008-07-02 11:45 1381--行列互转 /*********************** ... -
T-SQL查询学习笔记——求下属和祖先的算法
2008-04-10 14:08 1891构建试验环境: CREATE TABLE dbo.Employ ... -
T-SQL查询学习笔记——数据修改
2008-04-10 11:45 2791一、插入数据 包括:select into、insert e ... -
T-SQL查询学习笔记——使用TOP和APPLY解决常见问题
2008-04-09 14:45 20681、每组中的TOP n问题 CREATE UNIQUE IND ... -
T-SQL查询学习笔记——TOP子句
2008-04-09 13:59 3202在select查询或表表达式中,top结合order by子句 ... -
T-SQL查询学习笔记——分组因子的使用示例
2008-04-08 17:27 1650IF OBJECT_ID('Stocks') IS NOT N ... -
T-SQL查询学习笔记——求中值的几种方法
2008-04-08 11:28 2755中值有两种定义: 1、当组中包含奇数个元素时,我们将直接返回中 ... -
Over 字句
2008-04-07 17:34 1519功能:确定在应用关联的窗口函数之前,行集的分区和排序。 适用 ... -
sql 的随机函数newID()和RAND()
2008-03-20 11:05 3053SELECT * FROM Northwind..Orders ... -
T-SQL查询学习笔记——已有范围和缺失范围示例代码
2008-03-19 15:30 1886USE SqlTest;GOIF OBJECT_ID('db ... -
T-SQL查询学习笔记——快速生成数字辅助表的几种方法示例代码
2008-03-19 14:17 2296------------------------------- ... -
SELECT 与 SET 对变量赋值的区别
2008-03-17 15:29 2816SQL Server 中对已经定义的变量赋值的方式用两种,分别 ... -
SQL逻辑查询处理步骤
2008-03-14 17:22 17531、执行笛卡尔乘积(交叉联接) 2、应用ON筛选器(联接条件) ...
相关推荐
《SQL Server 2012 T-SQL基础教程——源码与示例数据库》 本教程专注于Microsoft SQL Server 2012中的Transact-SQL(T-SQL)语言,这是SQL Server的主要查询语言,用于数据操作、查询、存储过程和数据库对象的编程...
### SQL Server 2005 T-SQL Recipes: A Problem-Solution Approach #### 简介 《SQL Server 2005 T-SQL Recipes: A Problem-Solution Approach》是一本针对SQL Server 2005数据库管理和开发的专业书籍。本书以实际...
《Microsoft SQL Server 2005技术内幕:T-SQL查询》是一本深入解析SQL Server 2005中T-SQL查询语言的专业书籍。T-SQL(Transact-SQL)是微软对标准SQL的扩展,是SQL Server的核心组件,用于数据库管理、数据查询和...
SQLServer2005技术内幕T-SQL查询的代码示例
- **详尽的代码示例**:书中包含了大量实际可行的T-SQL代码示例,这些示例覆盖了SQL Server 2005中常见的各种操作场景。 - **实用性与参考性**:本书不仅适合初学者学习T-SQL的基础知识,也适用于有经验的开发者作为...
《Microsoft SQL Server 2005技术内幕:T-SQL查询的源代码》是一本深入探讨SQL Server 2005中T-SQL查询技术的专业书籍。T-SQL(Transact-SQL)是Microsoft SQL Server数据库管理系统中用于数据操作、查询、存储过程...
该书解释并比较了sql server 2000和sql server 2005在数据库开发相关问题上的解决方案,深入讨论了sql server 2005中新增的t-sql编程特性,包含了大量的代码示例、表示例和逻辑难题以帮助数据库开发人员和管理员理解...
《Microsoft SQL Server 2008 T-SQL Fundamentals》是一本专为SQL Server初学者设计的教程,它深入浅出地介绍了Transact-SQL(T-SQL)的基础知识。T-SQL是SQL Server中用于数据查询、更新、操作和编程的主要语言。...
T-sql基本语法示例 1.建库建表,建约束 2.查询 3.视图 4.存储过程 5.触发器
该书解释并比较了SQL Server 2000和SQL Server 2005在数据库开发相关问题上的解决方案,深入讨论了SQL Server 2005中新增的T-SQL编程特性,包含了大量的代码示例、表示例和逻辑难题以帮助数据库开发人员和管理员理解...
该书解释并比较了SQL Server 2000和SQL Server 2005在数据库开发相关问题上的解决方案,深入讨论了SQL Server 2005中新增的T-SQL编程特性,包含了大量的代码示例、表示例和逻辑难题以帮助数据库开发人员和管理员理解...
### 关于《深入微软SQL Server 2008 T-SQL编程》的知识点解析 #### 一、T-SQL编程概述 《Inside Microsoft SQL Server 2008 T-SQL Programming》是由Itzik Ben-Gan等多位专家共同编著的一本书,主要针对的是SQL ...
Microsoft SQL Server 2008技术内幕 T-SQL 查询 一书中,第四章,索引优化章节的示例数据库脚本。
本书及其续篇——《Microsoft SQL Server 2005技术内幕:T-SQL程序设计》介绍了SQL Server 2005中高级T-SQL查询、查询优化及编程相关的知识。这两本书侧重于解决实践中的常见问题,并讨论了解决这些问题的方法。它们...
本文将深入探讨SQL Server 2005的T-SQL(Transact-SQL)学习、性能调试以及作业管理等核心知识点。 首先,T-SQL是SQL Server的扩展,它在标准SQL的基础上添加了更多的编程元素,如流程控制、变量定义和错误处理,...
该书解释并比较了sql server 2000和sql server 2005在数据库开发相关问题上的解决方案,深入讨论了sql server 2005中新增的t-sql编程特性,包含了大量的代码示例、表示例和逻辑难题以帮助数据库开发人员和管理员理解...
### 关于《Inside Microsoft SQL Server 2008 - T-SQL Querying》的知识点解析 #### 一、概述 《Inside Microsoft SQL Server 2008 - T-SQL Querying》是一本深入探讨Microsoft SQL Server 2008中T-SQL查询技术的...