sql-metadata icon indicating copy to clipboard operation
sql-metadata copied to clipboard

Feature Request: Window Function Column Resolution

Open silvercrow08 opened this issue 2 years ago • 1 comments

This would be super helpful for parsing more advanced SQL select statements, like:

SELECT DATE_TRUNC('month', o.order_date) AS month, c.customer_id, c.customer_name, p.product_name, SUM(oi.quantity * oi.unit_price) AS revenue, ROW_NUMBER() OVER (PARTITION BY DATE_TRUNC('month', o.order_date), c.customer_id ORDER BY SUM(oi.quantity * oi.unit_price) DESC) AS revenue_rank FROM orders o JOIN customers c ON o.customer_id = c.customer_id JOIN order_items oi ON o.order_id = oi.order_id JOIN products p ON oi.product_id = p.product_id WHERE o.order_date >= '2022-01-01' AND o.order_date <= '2022-12-31' GROUP BY DATE_TRUNC('month', o.order_date), c.customer_id, c.customer_name, p.product_name

Having the last column auto-resolve to 'revenue_rank' would be SUPER helpful.

P.S: Does anyone know of any techniques to do this right now? I'm trying to design a solution from scratch using string indexing, but it's been extremely challenging

silvercrow08 avatar Sep 12 '23 20:09 silvercrow08

I actually just used a regex to solve this, but having it built-in to the library would be fantastic! I'd imagine it would be a pretty common use-case

silvercrow08 avatar Sep 13 '23 06:09 silvercrow08