with in sql server

Solutions on MaxInterview for with in sql server by the best coders in the world

showing results for - "with in sql server"
Clément
25 Nov 2018
1Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE or MERGE statement. This clause can also be used in a CREATE VIEW statement as part of its defining SELECT statement. A common table expression can include references to itself. This is referred to as a recursive common table expression.
2-- Define the CTE expression name and column list.  
3WITH Sales_CTE (SalesPersonID, SalesOrderID, SalesYear)  
4AS  
5-- Define the CTE query.  
6(  
7    SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear  
8    FROM Sales.SalesOrderHeader  
9    WHERE SalesPersonID IS NOT NULL  
10)  
11-- Define the outer query referencing the CTE name.  
12SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear  
13FROM Sales_CTE  
14GROUP BY SalesYear, SalesPersonID  
15ORDER BY SalesPersonID, SalesYear;