-1

If I put this in one query file how will pg promise handle it?

WITH regional_sales AS (
        SELECT region, SUM(amount) AS total_sales
        FROM orders
        GROUP BY region
     ), top_regions AS (
        SELECT region
        FROM regional_sales
        WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)
     )
SELECT region,
       product,
       SUM(quantity) AS product_units,
       SUM(amount) AS product_sales
FROM orders
WHERE region IN (SELECT region FROM top_regions)
GROUP BY region, product;

Is there a need to use WITH at all? I want to have the following use case,

SELECT * FROM balance WHERE bank_id = 1 FOR UPDATE (force all bank id 1 record to be locked)

SELECT * FROM balance WHERE bank_id = 1 AND amount = 500 (just get rows with bank id 1, but still making sure all bank id records 1 are locked`

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
Zanko
  • 4,298
  • 4
  • 31
  • 54

1 Answers1

0

pg-promise doesn't care what your SQL is made of, simply sending it into the server as is.

...which makes your question irrelevant/invalid.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • Thanks for the response, I jsut cant find any example of people using WITH clause with pg promise. In your opinion, can WITH clause be totally avoided? – Zanko Sep 13 '17 at 05:32
  • @Zanko Your question has no relevance to `pg-promise` whatsoever. And there are lots of examples that can be found on GitHub where developers execute `WITH` queries via `pg-promise`, which again is of no relation to each other. – vitaly-t Sep 13 '17 at 05:42