ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.63k stars 245 forks source link

Feature Request: Adding Support for CTEs #682

Open frodo821 opened 1 year ago

frodo821 commented 1 year ago

Hi, folks. I want support for CTEs (common table expressions) as a new feature of PonyORM.

Motivation

Some queries require using CTEs. For instance, a query using window functions as a filter condition needs CTE like this:

WITH q(`id`, `sum_price`) AS (
  SELECT `id`, sum(`price`) OVER (order by `last_updated` desc) AS `count` FROM `items`
)
SELECT `id` FROM `q`
WHERE `sum_price` < 20;

As a limitation over window functions of standard SQL, it never allows using window functions in WHERE/HAVING clause. Because of this reason, CTE is inevitable for this query.