Using Temporary Tables
Temporary tables store intermediate results within a session and are automatically dropped afterward.
CREATE TEMPORARY TABLE temp_sales AS SELECT * FROM sales WHERE sale_date > '2024-01-01';
WINDOW FRAME allows fine-grained control over the rows included in a calculation within a window function.
SELECT order_id, order_date, SUM(amount) OVER (ORDER BY order_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS rolling_sum FROM orders;
Window Functions perform calculations across a subset of rows (called a "window") that are defined relative to the current row. They allow you to perform operations like ranking, running totals, and differences between rows, all while retaining individual rows in the result set.
SELECT customer_id, purchase_date, amount, SUM(amount) OVER (PARTITION BY customer_id ORDER BY purchase_date) AS running_total FROM purchase_data;