Sie sind auf Seite 1von 5

Home | Weblogs | Forums | SQL Server Links

Search:

Go

Active Fo rum To pics | Po pular Articles | All Articles by Tag | SQL Server Bo o ks | Abo ut

Site Sponsored By : TraceTune.com - T he online version of ClearTrace. Quickly identif y which SQL statements use the most CPU and disk.

i re Dg nis U
By Gart h We lls o n 18 No ve m be r 20 0 1 | 3 Co mments | Tags: SELECT

Subscribe to SQLTeam.com
Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our whit e p ap e r wit h p e rf o rmance t ip s f o r d e ve lo p e rs . Subs c ribe

Calculating aggregate values can be simplified by using derived tables. In this article I show you how to use derived tables to calculate two independent aggregate values in a single SELECT statement. Derived Table Basics A derived table is one that is created on- the- fly using the SELECT statement, and referenced just like a regular table or view. Derived tables exist in memory and can only be referenced by the outer SELECT in which they are created. A simple use of a derived table is shown here.

SQLTeam.com Articles via RSS SQLTeam.com Weblog via RSS

- Ad ve r ti s e m e n t -

PDFmyURL.com

SELECT * FROM (SELECT * FROM Sales) AS a The inner SELECT produces a derived table and replaces a regular table or view. The key thing to remember when using derived tables is that you must always use an alias (e.g., AS a). The following shows the error produced when the alias is omitted. SELECT * FROM (SELECT * FROM Sales) -- Results -Server: Msg 170, Level 15, State 1, Line 3 Line 3: Incorrect syntax near ')'. Ref erencing Mult iple Derived Tables You can add as many derived tables as needed to a single outer SELECT to produce the desired resultset. The code starts to get a little convoluted after the second inner SELECT is added, but if you focus on the columns returned by each SELECT it all makes sense. The following script creates Stores/Sales information and shows how to calculate current month and year- todate sales count and amounts by Store. USE tempdb go SET NOCOUNT ON CREATE TABLE Stores ( Sto_ID smallint IDENTITY PRIMARY KEY NOT NULL, Sto_Name varchar(20) NOT NULL ) go INSERT Stores VALUES ('Store 1') INSERT Stores VALUES ('Store 2')
PDFmyURL.com

Resources
SQL Server Resources Advertise on SQLTeam.com SQL Server Books SQLTeam.com Newsletter Contact Us About the Site

INSERT Stores VALUES ('Store 3') go CREATE TABLE Sales ( Sal_ID int IDENTITY NOT NULL PRIMARY KEY, Sto_ID smallint NOT NULL FOREIGN KEY REFERENCES Stores(Sto_ID), Sal_Date smalldatetime NOT NULL, Sal_Amt money NOT NULL ) go INSERT Sales VALUES (1,'5/15/01 14:00:00',500) INSERT Sales VALUES (1,'5/16/01 11:00:00',200) INSERT Sales VALUES (1,'6/15/01 14:00:00',200) INSERT Sales VALUES (1,'7/15/01 08:00:00',500) INSERT Sales VALUES (1,'8/16/01 10:00:00',100) INSERT Sales VALUES (1,'8/17/01 10:00:00',125) INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT go Sales Sales Sales Sales Sales Sales Sales Sales VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES (2,'5/1/01 08:00:00',100) (2,'6/16/01 14:00:00',200) (2,'7/16/01 09:00:00',500) (2,'8/17/01 10:00:00',100) (3,'5/25/01 (3,'6/17/01 (3,'7/16/01 (3,'8/18/01 15:00:00',250) 14:00:00',100) 09:00:00',600) 16:00:00',150)

DECLARE @RunDate datetime Set @RunDate = '9/01/01' SELECT a1.Sto_Name, CM_Count, CM_Sales, YTD_Count, YTD_Sales FROM (SELECT b.Sto_Name, COUNT(*) AS CM_Count,
PDFmyURL.com

SUM(Sal_Amt) AS CM_Sales FROM Sales a JOIN Stores b ON a.Sto_ID = b.Sto_ID WHERE DATEPART(yy,Sal_Date) = DATEPART(yy,@RunDate) AND DATEPART(mm,Sal_Date) = (DATEPART(mm,@RunDate)-1) GROUP BY b.Sto_Name) AS a1 JOIN (SELECT Sto_Name, COUNT(*) AS YTD_Count, SUM(Sal_Amt) AS YTD_Sales FROM Sales a JOIN Stores b ON a.Sto_ID = b.Sto_ID WHERE DATEPART(yy,Sal_Date) = DATEPART(yy,@RunDate) GROUP BY b.Sto_Name) AS b1 ON a1.Sto_Name = b1.Sto_Name GO -- Results -Sto_Name -------------------Store 1 Store 2 Store 3 CM_Count ----------2 1 1 CM_Sales --------------------225.0000 100.0000 150.0000 YTD_Count ----------6 4 4 YTD_Sales ------------1625.0000 900.0000 1100.0000

The first inner SELECT calculates August's sales by comparing the month and year of @RunDate to the month and year of Sal_Date. Notice that I subtracted one month from @RunDate, because the assumption is the code is executed after the previous month has closed. You should also notice that the outer SELECT uses "a1." to specify which Sto_Name is returned. The column needs to be qualified because it exists in both the a1 and b1 derived tables. The second inner SELECT calculates year- to- date totals by comparing the year of @RunDate to the year of Sal_Date. Once this table has been derived it can be joined to a1 on Sto_Name so the two independent aggregate values can be combined in the same resultset. Garth www.SQLBook.com

Discuss this article: 3 Comments so f ar. Print this Article.

PDFmyURL.com

If you like this article you can sign up for our weekly newslet t er . There's an opt- out link at the bottom of each newsletter so it's easy to unsubscribe at any time. Email Address: Subscribe

84 Delicious 0

14 0

Related Articles
Joining to the Next Sequential Row (2 April 2008) Writing Outer Joins in T- SQL (11 February 2008) How to Use GROUP BY with Distinct Aggregates and Derived tables (31 July 2007) How to Use GROUP BY in SQL Server (30 July 2007) SQL Server 2005: Using OVER() with Aggregate Functions (21 May 2007) Server Side Paging using SQL Server 2005 (4 January 2007) Using XQuery, New Large DataTypes, and More (9 May 2006) Counting Parents and Children with Count Distinct (10 January 2006)

Other Recent Forum Posts


How to split this comma seperated and show as rows (15 Replies) Remove comma in first latter (1 Reply) Count query taking too long to execute. (6 Replies) how to use pivot in this scenario???? (7 Replies) Need Help in creasting a procedure with cursor (1 Reply) Left join cause duplicate rows (2 Replies) Date Help (4 Replies) HELP... setting variable in RS (2 Replies)

20 0 0 -20 13 SQLTeam Publishing, LLC | Privacy Po licy

PDFmyURL.com

Das könnte Ihnen auch gefallen