У меня есть запрос:
SELECT
acc.usage AND EXISTS (
SELECT * FROM order_bt prev_order
WHERE sys_period @> sys_time() AND
prev_order.id = o.id AND prev_order.app_period && acc.usage_range
) as usage,
acc.usage_range,
acc.invoice,
acc.invoice_range,
o.*
FROM "order" o
left join period prd on prd.id = period_id
LEFT JOIN accounting_ready() acc ON TRUE
WHERE
( acc.usage AND EXISTS (
SELECT * FROM order_bt prev_order
WHERE sys_period @> sys_time() AND
prev_order.id = o.id AND prev_order.app_period && acc.usage_range
) OR acc.invoice )
В этом запросе следующая часть - копирование / вставка:
acc.usage AND EXISTS (
SELECT * FROM order_bt prev_order
WHERE sys_period @> sys_time() AND
prev_order.id = o.id AND prev_order.app_period && acc.usage_range
) as usage,
Можно ли написать запрос без этой операции копирования / вставки?