TL;DR: nested WITH statements are supported in all modern data warehouses, lakehouses, and database engines except for Fabric & T-SQL products. This makes the usage of SQL templating engines unnecessarily complex.
Fabric supports regular WITH statements like the following:
with customers as ( select * from lakehouseone.dbo.customers ), orders as ( select * from lakehousetwo.dbo.orders ) select * from orders o join customers c on o.customer_id = c.id
However, the following nested version is currently not supported:
with customers_with_addresses as ( customers as ( select * from lakehouseone.dbo.customers ), addresses as ( select * from lakehousetwo.dbo.addresses ), final as ( select c.id, c.name, a.line_1, a.zip, a.country from customers c join addresses a on c.address_id = a.id ) select * from final ), orders as ( select * from lakehousetwo.dbo.orders ) select * from orders o join customers_with_addresses c on o.customer_id = c.id
You can see how working with CTEs allows for a very clean approach to SQL and enables the use of modular blocks of SQL.
This is especially popular in SQL templating engines like dbt.
Nested WITH statements allow for easy query injection. Wrapping a SELECT statement and inserting it into a query as a CTE only works if that inserted statement doesn't contain any CTEs.
More users have complained about the lack of nested WITH statements here and here.
5 Comments
- Vijay_Nagandla
Administrator
Status changed:PlannedtoCompleted - xiaoyulNew Member
The support of nested CTE has been added to Warehouse and SQL Analytics Endpoint in Microsoft Fabric. Please check this article for more details. Nested Common Table Expression (CTE) - SQL Server | Microsoft Learn
- fbcideas_migusrNew MemberStatus added:Planned
- sebgrosNew Member
Frameworks like DBT using jinja templating used nested WITH but Fabric Warehouse don’t support
this syntax and it prevents ephemeral materialization with DBT. Most of concurrent warehouses as welll
Spark SQL already support this syntax.
It would be great to support it
— Does not work in fabric T-SQL
WITH outer AS(
WITH inner AS(
SÉLECT * FROM t
)
SELECT * FROM inner
)
SELECT * FROM outer
- xiaoyuli
Microsoft Employee
Thanks for your feedback. We are working on adding the support of nested CTE to Fabric Warehousing.
Recent ideas
Allow setting default scroll position in Table and Matrix visuals (e.g., start from rightmost column
Currently, Power BI Table and Matrix visuals always open scrolled to the leftmost column by default, which is inconvenient for reports where the most important data is on the right side. Please add ...MGryciv10 minutes agoNew MemberNew396Views3likes1CommentWorkspace-Level Control for Export to Excel in Power BI/Fabric
Scenario In enterprise environments, organizations often require: Workspace-level governance for data export. Security group-based control for export permissions. Different export policies depe...vvargasv1 hour agoMicrosoft EmployeeNew85Views7likes1CommentRestore "no-reply" option for Power BI Subscriptions
Please add a setting in the Power BI Subscription pane to choose the sender identity. With the recent update the platform forces emails to route through the subscription owner's personal Exchange On...LenaFid4 hours agoNew MemberNew23Views3likes1Comment