DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to use CROSS APPLY on a derived table (subselect)?

Execute the following Microsoft SQL Server T-SQL example script in Management Studio Query Editor to demonstrate the use of the CROSS APPLY operator. CROSS APPLY returns only rows from the outer table query that produce a result set from the the inner derived query ( subselect, correlated subquery or table-valued function ).

-- SQL cross apply with derived table - cross apply with subselect

-- SQL Server format currency

USE AdventureWorks2008;

 

SELECT   TOP(3)

         c.AccountNumber,

         OrderAmount = '$' + convert(VARCHAR,convert(MONEY,amt.OrderTotal),1)

FROM     Sales.Customer AS c

         CROSS APPLY (SELECT   soh.CustomerId,

                               sum(sod.LineTotal) AS OrderTotal

                      FROM     sales.salesOrderHeader AS soh

                               JOIN sales.salesOrderDetail AS sod

                                 ON sod.salesOrderId = soh.salesOrderId

                      WHERE    soh.CustomerId = c.CustomerId

                      GROUP BY soh.CustomerId) AS amt

WHERE    OrderTotal > 10000

ORDER BY OrderTotal DESC

GO

 

/*

AccountNumber     OrderAmount

AW00029818        $877,107.19

AW00029715        $853,849.18

AW00029722        $841,908.77

*/

------------

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE