<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Is there a better way to write "common table expressions " (with and with recursive) than:</span><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px"><div>(def-sql-op :with (&rest args)</div><div> (let ((x (butlast args)) (y (last args))) </div><div> `("WITH " ,@(sql-expand-list x) ,@(sql-expand (car y)))))</div>
</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><div>(def-sql-op :with-recursive (&rest args)</div><div> (let ((x (butlast args)) (y (last args))) </div>
<div> `("WITH RECURSIVE " ,@(sql-expand-list x) ,@(sql-expand (car y)))))</div></div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">They work for me, but I have the feeling there is a better way to do it.</div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">Actual use would be something like the following (using the example from the postgresql website):</div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><div>(defun test2 ()</div><div> (query (:with-recursive</div><div> (:as 'regional-sales</div>
<div> (:select 'region (:as (:sum 'amount) 'total-sales)</div><div> :from 'orders </div><div> :group-by 'region))</div><div> (:as 'top-regions </div>
<div> (:select 'region</div><div> :from 'regional-sales</div><div> :where (:> 'total-sales</div><div> (:select (:/ (:sum</div>
<div> 'total-sales) 10)</div><div> :from 'regional-sales))))</div><div> (:select 'region 'product (:as (:sum 'quantity) 'product-units)</div>
<div> (:as (:sum 'amount) 'product-sales)</div><div> :from 'orders</div><div> :where (:in 'region (:select 'region :from 'top-regions))</div>
<div> :group-by 'region 'product))))</div><div><br></div><div>Sabra<br></div></div></div>