如何将 select 表达式的结果保存为变量?

How to save the results of a select expression as a variable?

在下面的 Postgresql sql 中,有没有办法将 mo.delivered_at - mo.created_at 保存为变量,这样我就不必重复了?

SELECT 
    to_char(mo.created_at,'MM-YYYY') AS month,
    mo.sku_key as sku,
    c.name,
    COUNT(*) as total,
    COUNT(*) FILTER (WHERE mo.delivered_at - mo.created_at < interval '3 days') as three_days,
    COUNT(*) FILTER (WHERE mo.delivered_at - mo.created_at > interval '3 days' and mo.delivered_at - mo.created_at <= interval '6 days') as six_days,
    COUNT(*) FILTER (WHERE mo.delivered_at - mo.created_at > interval '6 days' and mo.delivered_at - mo.created_at <= interval '9 days') as nine_days,
    COUNT(*) FILTER (WHERE mo.delivered_at - mo.created_at > interval '9 days') as ten_days,
    min(mo.delivered_at - mo.created_at),
    max(mo.delivered_at - mo.created_at),
    percentile_disc(0.5) within group (order by mo.delivered_at - mo.created_at) as median,
    avg(mo.delivered_at - mo.created_at) as average
FROM medication_order mo
LEFT JOIN subscription s ON s.id=mo.subscription_id
LEFT JOIN condition c on s.condition_id = c.id
WHERE 
    mo.status = 'DELIVERED' AND 
    mo.payment_preference = 'INSURANCE' AND
    mo.created_at > '2020-01-01' AND
    mo.delivered_at IS NOT null AND
    mo.sku_key != 'manual_order_sku'
GROUP BY month, mo.sku_key, c.name

从 table:

中选择时,您可以只计算子查询中的信息
SELECT 
    to_char(mo.created_at,'MM-YYYY') AS month,
    mo.sku_key as sku,
    c.name,
    COUNT(*) as total,
    COUNT(*) FILTER (WHERE mo.delivery_interval < interval '3 days') as three_days,
    COUNT(*) FILTER (WHERE mo.delivery_interval > interval '3 days' and mo.delivery_interval <= interval '6 days') as six_days,
    COUNT(*) FILTER (WHERE mo.delivery_interval > interval '6 days' and mo.delivery_interval <= interval '9 days') as nine_days,
    COUNT(*) FILTER (WHERE mo.delivery_interval > interval '9 days') as ten_days,
    min(mo.delivery_interval),
    max(mo.delivery_interval),
    percentile_disc(0.5) within group (order by mo.delivery_interval) as median,
    avg(mo.delivery_interval) as average
FROM (
    SELECT mo.*, mo.delivery_interval delivery_interval   --> here
    FROM medication_order
) mo
LEFT JOIN subscription s ON s.id=mo.subscription_id
LEFT JOIN condition c on s.condition_id = c.id
WHERE 
    mo.status = 'DELIVERED' AND 
    mo.payment_preference = 'INSURANCE' AND
    mo.created_at > '2020-01-01' AND
    mo.delivered_at IS NOT null AND
    mo.sku_key != 'manual_order_sku'
GROUP BY month, mo.sku_key, c.name

您可以按照建议在子查询或 CTE 中计算派生值。

但还有更多。这应该更快(并且正确)。也可以正确排序:

SELECT
    to_char(mo.month,'MM-YYYY') AS month,  -- optionally prettify
    mo.sku,
    s.condition_id,  -- I added this to make the result unambiguous
    (SELECT name FROM condition WHERE id = s.condition_id) AS condition_name,
    COUNT(*) AS total,
    COUNT(*) FILTER (WHERE mo.my_interval < interval '3 days') AS three_days,
    COUNT(*) FILTER (WHERE mo.my_interval > interval '3 days' AND mo.my_interval <= interval '6 days') AS six_days,
    COUNT(*) FILTER (WHERE mo.my_interval > interval '6 days' AND mo.my_interval <= interval '9 days') AS nine_days,
    COUNT(*) FILTER (WHERE mo.my_interval > interval '9 days') AS ten_days,
    min(mo.my_interval),
    max(mo.my_interval),
    percentile_disc(0.5) WITHIN GROUP (ORDER BY mo.my_interval) AS median,
    avg(mo.my_interval) AS average
FROM  (
   SELECT
       date_trunc('month', mo.created_at) AS month,         -- faster, keeps ORDER
       delivered_at - created_at          AS my_interval,   -- your core request
       sku_key                            AS sku
   FROM   medication_order mo
   WHERE  status = 'DELIVERED'                              -- filter early
   AND    payment_preference = 'INSURANCE'
   AND    created_at > '2020-01-01'
   AND    delivered_at IS NOT NULL
   AND    sku_key <> 'manual_order_sku'
   ) mo
LEFT   JOIN subscription s ON s.id = mo.subscription_id
GROUP  BY mo.month, mo.sku, s.condition_id  -- GROUP BY unique ID! Correct - and cheaper, too
ORDER  BY mo.month, mo.sku, s.condition_id; -- my addition: sorting by date works across years, 'MM-YYYY' does not

旁白:condition.name 应该是 UNIQUE。而“名字”几乎从来都不是什么好名字。