每位SQL Server开发员都有自己的首选操作方法。我的方法叫做分子查询。这些是由原子查询组合起来的查询,通过它们我可以处理一个表格。将原子组合起来,可以建立分子。当然也会有限制(化学家所称的化合价),但一般来说,这个原理还是适用的。
在本文中,我将探讨这种策略的几种变化。我从最基本的内容开始(即最详细的内容),然后逐步深化。为让你了解这种方法的灵活性,我会在不同层次使用几种技巧。(警告:这并不是唯一的解决方法,我只是在讨论一些可行的选择。)
我从普遍使用的数据库Northwind开始(虽然为了保留原貌,我把它复制到Northwind_New中,实际这才是我使用的数据库。)在我的拷贝中,我做出这些重要的修改。
我删除了复合主键,增加一个叫做PK的新列,并将其设为Identity列。
我增加了一个称作ExtendedAmount的计算列。
|
以下为引用的内容:
USE [Northwind_New] GO /****** Object: Table [dbo].[OrderDetails_New] Script Date: 08/23/2006 16:15:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATETABLE [dbo].[OrderDetails_New]( [OrderDetailID] [int] IDENTITY(1,1)NOTNULL, [OrderID] [int] NOTNULL, [ProductID] [int] NOTNULL, [UnitPrice] [money] NOTNULL, [Quantity] [smallint] NOTNULL, [Discount] [real] NOTNULL, [ExtendedAmount] AS([Quantity] * [UnitPrice] *(1 - [Discount])), CONSTRAINT [PK_OrderDetails_New] PRIMARYKEYCLUSTERED ( [OrderDetailID] ASC )ON [PRIMARY] )ON [PRIMARY]
|
列表A
列表A中包含建立这个新表格的新建(Create)语句。我用这个命令将它和值从原始表格中迅速移植出来:
INSERT INTO [Northwind_New].[dbo].[OrderDetails_New]
SELECT * FROM [Northwind_New].[dbo].[Order Details]
既然我有一个多行的表格,是时候开始实验了。(一旦新列ExtendedAmount被计算,其值会自动移植。)最初,我想用OrderID计算ExtendedAmount列的总数。我建立一个视图,如列表B所示。
|
以下为引用的内容:
USE [Northwind_New] GO /****** Object:?View [dbo]. [OrderDetailsSumByOrderID_vue]Script Date: 08/23/2006 16:31:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE VIEW [dbo].[OrderDetailsSumByOrderID_vue] AS SELECT TOP 100 PERCENT OrderID, SUM(ExtendedAmount) AS TotalAmount FROM?dbo.OrderDetails_New GROUP BY OrderID ORDER BY OrderID
|
列表B
我每行得到一个OrderID,ExtendedAmount的列数得到总计。(称之为OrderAmount,将航运,税收等相加得到OrderTotal)。
我会在等式中引入一个表格UDF,如列表C所示。注意,我可以用两种方法调用函数:提交CustomerID将表格限定为那个顾客的订单,或提交一个NULL获得所有顾客订单列表。如果我提交CustomerID,那么订单列表就出现在OrderDate中;如果我提交NULL,列表就被分组并在CustomerID中由OrderDate排序。
|
以下为引用的内容:
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go
-- ============================================= -- Author: Arthur Fuller -- Create date: 23 Aug 2006 -- Description: Table UDF to return Order Totals by Customer -- Example: SELECT * FROM dbo.CustomerOrderTotals_fnt('VINET') -- Example: SELECT * FROM dbo.CustomerOrderTotals_fnt(NULL) -- Notes: This udf is designed to serve two -- purposes. Pass a CustomerID to limit the rows to -- that customer, or pass nothing to get all customers -- ============================================= ALTERFUNCTION [dbo].[CustomerOrderTotals_fnt] ( -- Add the parameters for the function here @CustomerID varchar(5)=NULL ) RETURNS TABLE AS RETURN ( -- Add the SELECT statement with parameter references here SELECTTOP 100 PERCENT dbo.Customers.CustomerID, dbo.Customers.CompanyName, dbo.Orders.OrderID, dbo.Orders.OrderDate, dbo.OrderDetailsSumByOrderID_vue.TotalAmount FROM dbo.Customers INNERJOIN dbo.Orders ON dbo.Customers.CustomerID = dbo.Orders.CustomerID INNERJOIN dbo.OrderDetailsSumByOrderID_vue ON dbo.Orders.OrderID = dbo.OrderDetailsSumByOrderID_vue.OrderID WHERE dbo.Customers.CustomerID = @CustomerID OR @CustomerID ISNULL ORDERBY dbo.Orders.OrderDate )
--try it with these: --SELECT * FROM dbo.CustomerOrderTotals_fnt('VINET') --SELECT * FROM dbo.CustomerOrderTotals_fnt(NULL) 列表C
-- ================================================ -- Template generated from Template Explorer using: -- Create Inline Function (New Menu).SQL -- -- Use the Specify Values for Template Parameters -- command (Ctrl-Shift-M) to fill in the parameter -- values below. -- -- This block of comments will not be included in -- the definition of the function. -- ================================================ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Arthur Fuller -- Create date: 23 Aug 2006 -- Description: Return total sales by Customer -- Example: -- SELECT CustomerID, CustomerTotal -- FROM DBO.CustomerGrandTotal_fnt(null) -- SELECT CustomerID, CustomerTotal -- FROM DBO.CustomerGrandTotal_fnt('VINET') -- SELECT CustomerID, CustomerTotal -- FROM DBO.CustomerGrandTotal_fnt('VINET') -- ============================================= CREATEFUNCTION CustomerGrandTotal_fnt ( @CustomerID varchar(5) ) RETURNS TABLE AS RETURN ( -- Add the SELECT statement with parameter references
|