- 浏览: 1768333 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (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、每组中的TOP n问题
CREATE UNIQUE INDEX idx_eid_od_oid_i_cid_rd
ON dbo.Orders(EmployeeID, OrderDate, OrderID)
INCLUDE(CustomerID, RequiredDate);
CREATE UNIQUE INDEX idx_oid_qtyd_pid
ON dbo.[Order Details](OrderID, Quantity DESC, ProductID);
GO
-- 解决方案一: Solution 1 to the Most Recent Order for each Employee Problem
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate
FROM dbo.Orders AS O1
WHERE OrderID =
(SELECT TOP(1) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = O1.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC);
-- 解决方案二: Solution 1 to the n Most Recent Orders for each Employee Problem
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate
FROM dbo.Orders AS O1
WHERE OrderID IN
(SELECT TOP(3) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = O1.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC);
-- 解决方案三: Solution 2 to the Most Recent Order for each Employee Problem
SELECT O.OrderID, CustomerID, O.EmployeeID, OrderDate, RequiredDate
FROM (SELECT EmployeeID,
(SELECT TOP(1) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = E.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC) AS TopOrder
FROM dbo.Employees AS E) AS EO
JOIN dbo.Orders AS O
ON O.OrderID = EO.TopOrder;
-- 解决方案四: Solution 2 to the n Most Recent Orders for each Employee Problem
SELECT OrderID, CustomerID, E.EmployeeID, OrderDate, RequiredDate
FROM dbo.Employees AS E
JOIN dbo.Orders AS O1
ON OrderID IN
(SELECT TOP(3) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = E.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC);
-- 解决方案五: Solution 3 to the n Most Recent Orders for each Employee Problem
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate
FROM dbo.Employees AS E
CROSS APPLY
(SELECT TOP(3) OrderID, CustomerID, OrderDate, RequiredDate
FROM dbo.Orders AS O
WHERE O.EmployeeID = E.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC) AS A;
GO
-- Creade optimal index for next solution
CREATE UNIQUE INDEX idx_eid_odD_oidD_i_cid_rd
ON dbo.Orders(EmployeeID, OrderDate DESC, OrderID DESC)
INCLUDE(CustomerID, RequiredDate);
GO
-- 解决方案六: Solution 4 to the n Most Recent Orders for each Employee Problem
SELECT OrderID, CustomerID, OrderDate, RequiredDate
FROM (SELECT OrderID, CustomerID, OrderDate, RequiredDate,
ROW_NUMBER() OVER(PARTITION BY EmployeeID
ORDER BY OrderDate DESC, OrderID DESC) AS RowNum
FROM dbo.Orders) AS D
WHERE RowNum <= 3;
2、匹配当前值和前一个值
-- 解决方案一: Query Solution 1 Matching Current and Previous Occurrences
SELECT Cur.EmployeeID,
Cur.OrderID AS CurOrderID, Prv.OrderID AS PrvOrderID,
Cur.OrderDate AS CurOrderDate, Prv.OrderDate AS PrvOrderDate,
Cur.RequiredDate AS CurReqDate, Prv.RequiredDate AS PrvReqDate
FROM dbo.Orders AS Cur
LEFT OUTER JOIN dbo.Orders AS Prv
ON Prv.OrderID =
(SELECT TOP(1) OrderID
FROM dbo.Orders AS O
WHERE O.EmployeeID = Cur.EmployeeID
AND (O.OrderDate < Cur.OrderDate
OR (O.OrderDate = Cur.OrderDate
AND O.OrderID < Cur.OrderID))
ORDER BY OrderDate DESC, OrderID DESC)
ORDER BY Cur.EmployeeID, Cur.OrderDate, Cur.OrderID;
-- 解决方案二: Query Solution 2 Matching Current and Previous Occurrences
SELECT Cur.EmployeeID,
Cur.OrderID AS CurOrderID, Prv.OrderID AS PrvOrderID,
Cur.OrderDate AS CurOrderDate, Prv.OrderDate AS PrvOrderDate,
Cur.RequiredDate AS CurReqDate, Prv.RequiredDate AS PrvReqDate
FROM (SELECT EmployeeID, OrderID, OrderDate, RequiredDate,
(SELECT TOP(1) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = O1.EmployeeID
AND (O2.OrderDate < O1.OrderDate
OR O2.OrderDate = O1.OrderDate
AND O2.OrderID < O1.OrderID)
ORDER BY OrderDate DESC, OrderID DESC) AS PrvOrderID
FROM dbo.Orders AS O1) AS Cur
LEFT OUTER JOIN dbo.Orders AS Prv
ON Cur.PrvOrderID = Prv.OrderID
ORDER BY Cur.EmployeeID, Cur.OrderDate, Cur.OrderID;
-- 解决方案三: Query Solution 3 Matching Current and Previous Occurrences
SELECT Cur.EmployeeID,
Cur.OrderID AS CurOrderID, Prv.OrderID AS PrvOrderID,
Cur.OrderDate AS CurOrderDate, Prv.OrderDate AS PrvOrderDate,
Cur.RequiredDate AS CurReqDate, Prv.RequiredDate AS PrvReqDate
FROM dbo.Orders AS Cur
OUTER APPLY
(SELECT TOP(1) OrderID, OrderDate, RequiredDate
FROM dbo.Orders AS O
WHERE O.EmployeeID = Cur.EmployeeID
AND (O.OrderDate < Cur.OrderDate
OR (O.OrderDate = Cur.OrderDate
AND O.OrderID < Cur.OrderID))
ORDER BY OrderDate DESC, OrderID DESC) AS Prv
ORDER BY Cur.EmployeeID, Cur.OrderDate, Cur.OrderID;
-- 解决方案四: Query Solution 4 Matching Current and Previous Occurrences
WITH OrdersRN AS
(
SELECT EmployeeID, OrderID, OrderDate, RequiredDate,
ROW_NUMBER() OVER(PARTITION BY EmployeeID
ORDER BY OrderDate, OrderID) AS rn
FROM dbo.Orders
)
SELECT Cur.EmployeeID,
Cur.OrderID AS CurOrderID, Prv.OrderID AS PrvOrderID,
Cur.OrderDate AS CurOrderDate, Prv.OrderDate AS PrvOrderDate,
Cur.RequiredDate AS CurReqDate, Prv.RequiredDate AS PrvReqDate
FROM OrdersRN AS Cur
LEFT OUTER JOIN OrdersRN AS Prv
ON Cur.EmployeeID = Prv.EmployeeID
AND Cur.rn = Prv.rn + 1
ORDER BY Cur.EmployeeID, Cur.OrderDate, Cur.OrderID;
GO
CREATE UNIQUE INDEX idx_eid_od_oid_i_cid_rd
ON dbo.Orders(EmployeeID, OrderDate, OrderID)
INCLUDE(CustomerID, RequiredDate);
CREATE UNIQUE INDEX idx_oid_qtyd_pid
ON dbo.[Order Details](OrderID, Quantity DESC, ProductID);
GO
-- 解决方案一: Solution 1 to the Most Recent Order for each Employee Problem
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate
FROM dbo.Orders AS O1
WHERE OrderID =
(SELECT TOP(1) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = O1.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC);
-- 解决方案二: Solution 1 to the n Most Recent Orders for each Employee Problem
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate
FROM dbo.Orders AS O1
WHERE OrderID IN
(SELECT TOP(3) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = O1.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC);
-- 解决方案三: Solution 2 to the Most Recent Order for each Employee Problem
SELECT O.OrderID, CustomerID, O.EmployeeID, OrderDate, RequiredDate
FROM (SELECT EmployeeID,
(SELECT TOP(1) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = E.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC) AS TopOrder
FROM dbo.Employees AS E) AS EO
JOIN dbo.Orders AS O
ON O.OrderID = EO.TopOrder;
-- 解决方案四: Solution 2 to the n Most Recent Orders for each Employee Problem
SELECT OrderID, CustomerID, E.EmployeeID, OrderDate, RequiredDate
FROM dbo.Employees AS E
JOIN dbo.Orders AS O1
ON OrderID IN
(SELECT TOP(3) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = E.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC);
-- 解决方案五: Solution 3 to the n Most Recent Orders for each Employee Problem
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate
FROM dbo.Employees AS E
CROSS APPLY
(SELECT TOP(3) OrderID, CustomerID, OrderDate, RequiredDate
FROM dbo.Orders AS O
WHERE O.EmployeeID = E.EmployeeID
ORDER BY OrderDate DESC, OrderID DESC) AS A;
GO
-- Creade optimal index for next solution
CREATE UNIQUE INDEX idx_eid_odD_oidD_i_cid_rd
ON dbo.Orders(EmployeeID, OrderDate DESC, OrderID DESC)
INCLUDE(CustomerID, RequiredDate);
GO
-- 解决方案六: Solution 4 to the n Most Recent Orders for each Employee Problem
SELECT OrderID, CustomerID, OrderDate, RequiredDate
FROM (SELECT OrderID, CustomerID, OrderDate, RequiredDate,
ROW_NUMBER() OVER(PARTITION BY EmployeeID
ORDER BY OrderDate DESC, OrderID DESC) AS RowNum
FROM dbo.Orders) AS D
WHERE RowNum <= 3;
2、匹配当前值和前一个值
-- 解决方案一: Query Solution 1 Matching Current and Previous Occurrences
SELECT Cur.EmployeeID,
Cur.OrderID AS CurOrderID, Prv.OrderID AS PrvOrderID,
Cur.OrderDate AS CurOrderDate, Prv.OrderDate AS PrvOrderDate,
Cur.RequiredDate AS CurReqDate, Prv.RequiredDate AS PrvReqDate
FROM dbo.Orders AS Cur
LEFT OUTER JOIN dbo.Orders AS Prv
ON Prv.OrderID =
(SELECT TOP(1) OrderID
FROM dbo.Orders AS O
WHERE O.EmployeeID = Cur.EmployeeID
AND (O.OrderDate < Cur.OrderDate
OR (O.OrderDate = Cur.OrderDate
AND O.OrderID < Cur.OrderID))
ORDER BY OrderDate DESC, OrderID DESC)
ORDER BY Cur.EmployeeID, Cur.OrderDate, Cur.OrderID;
-- 解决方案二: Query Solution 2 Matching Current and Previous Occurrences
SELECT Cur.EmployeeID,
Cur.OrderID AS CurOrderID, Prv.OrderID AS PrvOrderID,
Cur.OrderDate AS CurOrderDate, Prv.OrderDate AS PrvOrderDate,
Cur.RequiredDate AS CurReqDate, Prv.RequiredDate AS PrvReqDate
FROM (SELECT EmployeeID, OrderID, OrderDate, RequiredDate,
(SELECT TOP(1) OrderID
FROM dbo.Orders AS O2
WHERE O2.EmployeeID = O1.EmployeeID
AND (O2.OrderDate < O1.OrderDate
OR O2.OrderDate = O1.OrderDate
AND O2.OrderID < O1.OrderID)
ORDER BY OrderDate DESC, OrderID DESC) AS PrvOrderID
FROM dbo.Orders AS O1) AS Cur
LEFT OUTER JOIN dbo.Orders AS Prv
ON Cur.PrvOrderID = Prv.OrderID
ORDER BY Cur.EmployeeID, Cur.OrderDate, Cur.OrderID;
-- 解决方案三: Query Solution 3 Matching Current and Previous Occurrences
SELECT Cur.EmployeeID,
Cur.OrderID AS CurOrderID, Prv.OrderID AS PrvOrderID,
Cur.OrderDate AS CurOrderDate, Prv.OrderDate AS PrvOrderDate,
Cur.RequiredDate AS CurReqDate, Prv.RequiredDate AS PrvReqDate
FROM dbo.Orders AS Cur
OUTER APPLY
(SELECT TOP(1) OrderID, OrderDate, RequiredDate
FROM dbo.Orders AS O
WHERE O.EmployeeID = Cur.EmployeeID
AND (O.OrderDate < Cur.OrderDate
OR (O.OrderDate = Cur.OrderDate
AND O.OrderID < Cur.OrderID))
ORDER BY OrderDate DESC, OrderID DESC) AS Prv
ORDER BY Cur.EmployeeID, Cur.OrderDate, Cur.OrderID;
-- 解决方案四: Query Solution 4 Matching Current and Previous Occurrences
WITH OrdersRN AS
(
SELECT EmployeeID, OrderID, OrderDate, RequiredDate,
ROW_NUMBER() OVER(PARTITION BY EmployeeID
ORDER BY OrderDate, OrderID) AS rn
FROM dbo.Orders
)
SELECT Cur.EmployeeID,
Cur.OrderID AS CurOrderID, Prv.OrderID AS PrvOrderID,
Cur.OrderDate AS CurOrderDate, Prv.OrderDate AS PrvOrderDate,
Cur.RequiredDate AS CurReqDate, Prv.RequiredDate AS PrvReqDate
FROM OrdersRN AS Cur
LEFT OUTER JOIN OrdersRN AS Prv
ON Cur.EmployeeID = Prv.EmployeeID
AND Cur.rn = Prv.rn + 1
ORDER BY Cur.EmployeeID, Cur.OrderDate, Cur.OrderID;
GO
发表评论
-
SQL Server 2005中处理表分区问题
2008-08-28 11:01 2012数据库性能调优是每一 ... -
SQL LIKE 通配符随笔
2008-07-04 09:26 1604通配符 说明 _ 与任意单字符匹配 % 与包含一 ... -
五种提高 SQL 性能的方法
2008-07-02 12:10 1128有时, 为了让应用程序运行得更快,所做的全部工作就是在这里或那 ... -
SQL操作全集
2008-07-02 12:01 1113SQL分类: DDL—数据定义语言(CREATE,ALTER, ... -
不同服务器数据库之间的数据操作
2008-07-02 11:56 1915--创建链接服务器 exec sp_addlinkedser ... -
远程连接操作
2008-07-02 11:52 1288--远程连接操作 /******************** ... -
合并分拆表
2008-07-02 11:46 1452--合并分拆表 /********************** ... -
行列互转
2008-07-02 11:45 1389--行列互转 /*********************** ... -
T-SQL查询学习笔记——求下属和祖先的算法
2008-04-10 14:08 1900构建试验环境: CREATE TABLE dbo.Employ ... -
T-SQL查询学习笔记——数据修改
2008-04-10 11:45 2802一、插入数据 包括:select into、insert e ... -
T-SQL查询学习笔记——TOP子句
2008-04-09 13:59 3204在select查询或表表达式中,top结合order by子句 ... -
T-SQL查询学习笔记——分组因子的使用示例
2008-04-08 17:27 1653IF OBJECT_ID('Stocks') IS NOT N ... -
T-SQL查询学习笔记——求中值的几种方法
2008-04-08 11:28 2770中值有两种定义: 1、当组中包含奇数个元素时,我们将直接返回中 ... -
Over 字句
2008-04-07 17:34 1531功能:确定在应用关联的窗口函数之前,行集的分区和排序。 适用 ... -
sql 的随机函数newID()和RAND()
2008-03-20 11:05 3058SELECT * FROM Northwind..Orders ... -
T-SQL查询学习笔记——已有范围和缺失范围示例代码
2008-03-19 15:30 1892USE SqlTest;GOIF OBJECT_ID('db ... -
T-SQL查询学习笔记——快速生成数字辅助表的几种方法示例代码
2008-03-19 14:17 2303------------------------------- ... -
SELECT 与 SET 对变量赋值的区别
2008-03-17 15:29 2825SQL Server 中对已经定义的变量赋值的方式用两种,分别 ... -
SQL逻辑查询处理步骤
2008-03-14 17:22 17601、执行笛卡尔乘积(交叉联接) 2、应用ON筛选器(联接条件) ... -
Server 2005 中集合操作(UNION、EXCEPT、INTERSECT)
2008-03-14 16:32 1439集合操作在两个输入中比较全部行。 Union:返回 ...
相关推荐
主要内容包括SQL的基础理论、查询优化、查询算法及复杂度,以及在使用子查询、表表达式、排名函数、数据聚合和透视转换、TOP和APPLY、数据修改、分区表、特殊数据结构等实际应用时会遇到的各种高级查询问题和解决...
它详细介绍了T-SQL的内部体系结构,包含了非常全面的编程参考,提供了使用Transact-SQL(T-SQL)的专家级指导,囊括了非常全面的编程参考,揭示了基于集合的查询的强大威力,并包含大量来自专家们的参考和建议。...
通过阅读和实践,读者可以逐步掌握SQL Server 2008中的T-SQL查询技巧,并能解决实际工作中的各种问题。 总的来说,《SQL2008技术内幕——T-SQL查询》是一本全面且深入的教程,适合SQL Server初学者和进阶者学习,有...
《SQL Server 2012 T-SQL基础教程——源码与示例数据库》 本教程专注于Microsoft SQL Server 2012中的Transact-SQL...通过系统学习和动手实践,你将能够编写出高效的T-SQL查询,管理和操作SQL Server 2012中的数据。
本书及其续篇——《Microsoft SQL Server 2005技术内幕:T-SQL程序设计》介绍了SQL Server 2005中高级T-SQL查询、查询优化及编程相关的知识。这两本书侧重于解决实践中的常见问题,并讨论了解决这些问题的方法。它们...
《Microsoft SQL Server 2008技术内幕:T-SQL语言基础》适合须要学习T-SQL的各级程序员和数据库专业人员,是他们快速掌握T-SQL的必备参考图书。 致谢 引言 第1章 T-SQL查询和编程基础 1.1 理论背景 1.2 SQL ...
SQL 的基础理论 、查询优化、查询 事法及复杂度,以及在使用子 查询、表表达式、排名函数、数据聚合和透视转换、TOP 和 APPLY、数据修改、分区表、特殊数据结构等实际 应用时会遇到的各种高级查询问题和解决方案.
Microsoft SQL Server 2005技术内幕:T-SQL查询 pdf 中文版 第二部分 第一部分地址:http://download.csdn.net/source/2684220
学习sql server 和sql 的两本经典的著作: 《sql server 2005 技术内幕 T-SQL查询》 《sql server 2005 技术内幕 T-SQL程序设计》 网上大多的资源都是英文的,好容易找到中文的了,上传上来和大家分享。 这两本书都...
SQL Server 2005版本对T-SQL进行了显著的增强,引入了如PIVOT、UNPIVOT、APPLY以及OVER子句等新特性,这些增强功能极大地丰富了数据处理的能力和灵活性。然而,本文主要聚焦于T-SQL的核心组成部分——逻辑查询处理...
- **TOP 和 APPLY**:使用TOP限制结果集大小,APPLY进行表间的连接操作。 - **数据修改**:如何使用INSERT、UPDATE、DELETE语句有效地修改数据。 - **分区表**:介绍如何使用分区来优化大数据量表的查询性能。 - **...
"学习笔记——sql.zip"这个压缩包文件很可能包含了关于SQL的学习资料,如教程、笔记、示例代码等,旨在帮助用户掌握SQL的基本概念、语法和高级特性。 首先,SQL的基础知识包括数据类型,如整型(INT)、浮点型...
该书解释并比较了SQL Server 2000和SQL Server 2005在数据库开发相关问题上的解决方案,深入讨论了SQL Server 2005中新增的T-SQL编程特性,包含了大量的代码示例、表示例和逻辑难题以帮助数据库开发人员和管理员理解...
为了解决这些问题,文章提出了一种替代方案——直接在T-SQL存储过程中嵌入脚本,通过COM+对象库访问外部数据源。这种方法利用了SQL Server的存储过程可以程序化地导入数据的特点,使用紧凑通用的代码,便于扩展和...
《SQL Server 2005技术内幕:T-SQL程序设计》是一本深入探讨SQL Server数据库编程的专业书籍,尤其在T-SQL语言方面提供了丰富的知识和技术。SQL Server 2005作为微软的一个重要数据库管理系统,它引入了许多新特性和...
5. **简捷的语法结构**:T-SQL的语法简洁明了,易于学习和使用。 6. **支持三级模式结构**:符合数据库管理系统的标准模式,即外模式、模式和内模式。 8.1.1 T-SQL语言的特点还包括对数据定义(DDL)、数据操纵...
《Transact-SQL权威指南》是一本深入探讨SQL在数据库管理中的应用的书籍,主要针对Transact-SQL,这是Microsoft SQL ...书中的内容丰富多样,涵盖了从基础概念到高级应用的广泛知识,是学习和提升T-SQL技能的宝贵资源。
Mybatis 学习笔记——原生DAO实现数据增删改查SQL:https://blog.csdn.net/qq_24598601/article/details/83037252
《信息系统开发教程——使用C#和SQL Server 2005》是一本深入探讨如何利用C#编程语言和SQL Server 2005数据库管理系统来构建高效、稳定的信息系统的教材。该教程涵盖了从系统分析、设计到实现的全过程,旨在帮助...