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 update year-to-date sales figure?

Execute the following SQL Server T-SQL script in Management Studio Query Editor to demonstrate YTD column update which typically maybe scheduled as a night job in SQL Server Agent.


-- SQL update group by - SQL correlated subquery update
USE AdventureWorks;
UPDATE Sales.SalesPerson
SET    SalesYTD = isnull(
      (SELECT   SUM(so.SubTotal)
         FROM     Sales.SalesOrderHeader AS so
         WHERE    so.OrderDate IN
                       (SELECT OrderDate
                                FROM   Sales.SalesOrderHeader AS soh
                                WHERE  soh.SalesPersonID = so.SalesPersonID
                                          -- AND year(OrderDate) = year(getdate()))
                                          AND year(OrderDate) = 2004)
          AND Sales.SalesPerson.SalesPersonID = so.SalesPersonID
         GROUP BY so.SalesPersonID),0);
 
GO
-- (17 row(s) affected)
 
-- SQL currency formatting - money format
SELECT LastName + ', ' + FirstName            AS SalesPerson,
       '$' + CONVERT(VARCHAR,SalesQuota,1)    AS SalesQuota,
       '$' + CONVERT(VARCHAR,Bonus,1)         AS Bonus,
       '$' + CONVERT(VARCHAR,SalesYTD,1)      AS SalesYTD,
       '$' + CONVERT(VARCHAR,SalesLastYear,1) AS SalesLastYear
FROM   Sales.SalesPerson sp
       INNER JOIN HumanResources.Employee e
         ON sp.SalesPersonID = e.EmployeeID
       INNER JOIN Person.Contact c
         ON e.ContactID = c.ContactID
ORDER BY SalesPerson
GO
/* Partial result
 
SalesPerson             SalesQuota  Bonus       SalesYTD          SalesLastYear
Ansman-Wolfe, Pamela    $250,000.00 $5,000.00   $788,700.12       $1,927,059.18
Blythe, Michael         $300,000.00 $4,100.00   $1,858,626.93     $1,750,406.48
Campbell, David         $250,000.00 $3,500.00   $815,513.35       $1,371,635.32
Carson, Jillian         $250,000.00 $2,500.00   $1,651,313.92     $1,997,186.20

*/

 

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.