Using Table Expressions
Using Table Expressions
Using Table Expressions
Using Views
Using Inline TVFs
Using Derived Tables
Using CTEs
Lesson 1: Using Views
SELECT <column_list>
FROM (
<derived_table_definition>
) AS <derived_table_alias>;
To create a CTE:
Define the table expression in a WITH clause
Assign column aliases (inline or external)
Pass arguments if desired
Reference the CTE in the outer query
WITH CTE_year AS
(
SELECT YEAR(orderdate) AS orderyear, custid
FROM Sales.Orders
)
SELECT orderyear, COUNT(DISTINCT custid) AS cust_count
FROM CTE_year
GROUP BY orderyear;