KDBS: Writing Without CTEs and Window Functions
KDBS has not built CTEs or window functions. The rejection code is NOT_IMPLEMENTED, which means the design accepts the feature but it is not built yet (it may arrive in a later version). This lesson shows how to solve, on KDBS, the questions from other SQL lessons: WITH patterns, Window functions, and Groupwise maximum.
KDBS · WITH
WITH t AS (SELECT cust_id FROM orders WHERE ord_id BETWEEN 1 AND 10)
SELECT * FROM t
KDBS · OVER
SELECT ord_id, ROW_NUMBER() OVER (ORDER BY ord_id) FROM orders WHERE ord_id BETWEEN 1 AND 10
Derived tables instead of CTEs
Move a CTE used only once into a derived table (FROM (SELECT …) AS x). The query layer answers derived tables, and the rows the inner statement fetches must stay within 1 million. A derived table cannot be correlated with the outer query.
KDBS · Join of two derived tables
SELECT a.status, a.cnt_101, b.cnt_102
FROM (SELECT status, COUNT(*) AS cnt_101 FROM orders WHERE cust_id = 101 GROUP BY status) AS a
JOIN (SELECT status, COUNT(*) AS cnt_102 FROM orders WHERE cust_id = 102 GROUP BY status) AS b
ON b.status = a.status
There is no syntax to replace recursive CTEs (hierarchical queries). If the depth is fixed, self-join that many times; otherwise, iterate in the application.
Instead of window functions
Top-1 per group: correlated subquery
"Latest order per customer" is commonly written as ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY ord_dt DESC) = 1. On KDBS, get each customer's latest order time with a comparison subquery.
KDBS · Latest order per customer
SELECT o.cust_id, o.ord_id, o.ord_dt, o.amt
FROM orders AS o
WHERE o.cust_id BETWEEN 101 AND 105
AND o.ord_dt = (SELECT MAX(x.ord_dt) FROM orders AS x
WHERE x.cust_id = o.cust_id AND x.cust_id BETWEEN 101 AND 105)
- The query layer answers aggregates inside subqueries. The layer also fetches the subquery's
orders, so you must repeat a condition that refers only to that table inside the subquery, such asx.cust_id BETWEEN 101 AND 105. Without it, the layer would have to hold 2 million rows and fails. - If two orders have the same time, both are returned (unlike
ROW_NUMBER, same asRANK).
A single Top-N: ORDER BY … LIMIT
KDBS · Latest 3 orders of one customer
SELECT ord_id, ord_dt, amt
FROM orders
WHERE cust_id = 101
ORDER BY ord_dt DESC
LIMIT 3
Running total: self-join + GROUP BY
SUM(amt) OVER (ORDER BY ord_id) is replaced by joining the preceding rows and adding them. With n rows, the join produces n(n+1)/2 rows, so use it only on small ranges.
KDBS · Running total
SELECT a.ord_id, SUM(b.amt)
FROM orders AS a
JOIN orders AS b ON b.cust_id = a.cust_id
WHERE a.cust_id = 5001 AND b.cust_id = 5001 AND b.ord_id <= a.ord_id
GROUP BY a.ord_id
Ratio to the total: comparison subquery twice
KDBS · Ratio by status (customer 101)
SELECT status, COUNT(*), (SELECT COUNT(*) FROM orders WHERE cust_id = 101) AS total
FROM orders
WHERE cust_id = 101
GROUP BY status
- The query layer answers subqueries in the select list. The ratio is computed on the receiving side (the application).
Summary
| On other DBs | On KDBS |
|---|---|
WITH t AS (…) | Derived table FROM (…) AS t. No recursion |
ROW_NUMBER() … = 1 | col = (SELECT MAX(col) … correlated) |
ROW_NUMBER() <= N (one group) | ORDER BY … LIMIT N |
SUM() OVER (ORDER BY …) | Self-join + GROUP BY (small range) |
COUNT(*) OVER () | Scalar subquery |