如何将查询值添加到查询

How to add a Queried Valued to a query

我有一个查询需要使用另一个查询的结果值来计算日期。

以下代码的结果是 21。当我在主查询中输入 21 代替它时,它起作用了。

SELECT value from ir_config_parameter where key = 'xes.mrp.production.default.kit.complete.to.far.offset.days'

下面的查询导致了这个错误:

ERROR: operator does not exist: text * interval LINE 6: (mo.date_planned_start::date + (leadtime.value) * INTERVAL... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

    SELECT
    mo.name,
    CASE WHEN mo.x_far_confirmed::date is not null then mo.x_far_confirmed::date else
             CASE WHEN rtw.produce_delay != 0 THEN (mo.date_planned_start::date + rtw.produce_delay * INTERVAL '1 day') ELSE
                    (mo.date_planned_start::date+leadtime.value*INTERVAL '1 day') END END as MFAR
FROM 
    mrp_production AS mo
    LEFT JOIN product_product AS pp ON mo.product_id = pp.id
    LEFT JOIN mrp_routing AS rt ON mo.routing_id = rt.id
    LEFT JOIN mrp_routing_workcenter AS rtw ON rt.id = rtw.routing_id
    CROSS JOIN
        (SELECT value from ir_config_parameter where key = 'xes.mrp.production.default.kit.complete.to.far.offset.days') as leadtime 
WHERE 
    mo.state IN ('planning','confirmed','draft','ready','in_production','done')
    AND (pp.default_code LIKE '900%')

现在的问题似乎是这部分中发生的实际数学问题,因为当我将 leadtime.value 作为一列时,它按预期结果为 21。

(mo.date_planned_start::date + leadtime.value * INTERVAL '1 day')

最后一个查询的语法错误是因为您正在执行 LEFT JOIN 而没有 ON 条件。实际上,您这样做是在尝试模仿 CROSS JOIN 的行为。将其更改为 CROSS JOIN 应该有效:


    SELECT
        mo.name,
        CASE WHEN mo.x_far_confirmed::date is not null then mo.x_far_confirmed::date else
                 CASE WHEN rtw.produce_delay != 0 THEN (mo.date_planned_start::date + rtw.produce_delay * INTERVAL '1 day') ELSE
                        (mo.date_planned_start::date+leadtime.value*INTERVAL '1 day') END END as MFAR
    FROM 
        mrp_production AS mo
        LEFT JOIN product_product AS pp ON mo.product_id = pp.id
        LEFT JOIN mrp_routing AS rt ON mo.routing_id = rt.id
        LEFT JOIN mrp_routing_workcenter AS rtw ON rt.id = rtw.routing_id
        CROSS JOIN
            (SELECT value from ir_config_parameter where key = 'xes.mrp.production.default.kit.complete.to.far.offset.days') as leadtime
    WHERE 
        mo.state IN ('planning','confirmed','draft','ready','in_production','done')
        AND (pp.default_code LIKE '900%')

我在 leadtime.value 之后添加了 ::numeric,它解决了我的问题。

SELECT
    mo.name,
    CASE WHEN mo.x_far_confirmed::date is not null then mo.x_far_confirmed::date else
             CASE WHEN rtw.produce_delay != 0 THEN (mo.date_planned_start::date + rtw.produce_delay * INTERVAL '1 day') ELSE
                    (mo.date_planned_start::date+leadtime.value::numeric *INTERVAL '1 day') END END as MFAR
FROM 
    mrp_production AS mo
    LEFT JOIN product_product AS pp ON mo.product_id = pp.id
    LEFT JOIN mrp_routing AS rt ON mo.routing_id = rt.id
    LEFT JOIN mrp_routing_workcenter AS rtw ON rt.id = rtw.routing_id
    CROSS JOIN
        (SELECT value from ir_config_parameter where key = 'xes.mrp.production.default.kit.complete.to.far.offset.days') as leadtime
WHERE 
    mo.state IN ('planning','confirmed','draft','ready','in_production','done')
    AND (pp.default_code LIKE '900%')