First page Back Continue Last page Image

Creating Multiple CTE’s

WITH summary AS

(

SELECT vendor_state, vendor_name,

SUM(invoice_total) AS sum_of_invoices

FROM vendors v JOIN invoices i

ON v.vendor_id = i.vendor_id

GROUP BY vendor_state, vendor_name

),

top_in_state AS

(

SELECT vendor_state,

MAX(sum_of_invoices) AS sum_of_invoices

FROM summary

GROUP BY vendor_state

)

If I am going to approach this like a subroutine, I will want to make several of them, so how do I create multiple CTE’s?

Here is an example of 2 CTE’s, Notice 1 WITH statement and 2 AS statements

Notice the Comma separating the 2 CTE’s

CTE Number 1

CTE Number 2