datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to generate a RUNNING TOTAL with CROSS APPLY?

Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor to generate running total for the sales order amount.

-- SQL Running Total generation - sql running total column - sql server running total

-- T SQL running total - sql calculate running total - transact sql running total

-- T SQL  cross apply - cross apply table valued function - cross apply subselect

USE AdventureWorks2008;

 

DECLARE @OrderYear int = 2001, @OrderMonth int = 7

 

SELECT   SalesOrderId,

         OrderDate = convert(CHAR(10),OrderDate,111),

         TotalDue = '$'+CONVERT(varchar,TotalDue,1),

         [Running Total] = '$'+CONVERT(varchar,[Running Total],1)

FROM     Sales.SalesOrderHeader soh

         CROSS APPLY (SELECT [Running Total] = sum(TotalDue)

                      FROM   Sales.SalesOrderHeader

                      WHERE  SalesOrderID <= soh.SalesOrderID

                             AND year(OrderDate) = @OrderYear

                             AND month(OrderDate) = @OrderMonth) AS RT

WHERE    year(OrderDate) = @OrderYear

         AND month(OrderDate) = @OrderMonth

ORDER BY SalesOrderID;

 

/* Partial results

 

SalesOrderId      OrderDate   TotalDue    Running Total

43691             2001/07/01  $1,159.98   $490,808.34

43692             2001/07/01  $51,056.50  $541,864.84

43693             2001/07/01  $27,204.50  $569,069.34

43694             2001/07/01  $27,325.62  $596,394.96

43695             2001/07/01  $52,314.34  $648,709.30

43696             2001/07/01  $556.20     $649,265.50

43697             2001/07/01  $3,953.99   $653,219.49

*/

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.