UPDATE amc_machine b
SET with_parts = a.with_parts,
amc_validity_upto = a.amc_validity_upto
FROM (SELECT CASE
WHEN count(*) > 0 THEN (SELECT DISTINCT ON (machine_id) with_parts, amc_validity_upto, machine_id
FROM amc_amcdetail
WHERE machine_id = 2 AND id != 1
ORDER BY machine_id, amc_validity_upto DESC)
WHEN count(*) = 0 THEN (SELECT FALSE AS with_parts, NULL AS amc_validity_upto, 2 AS machine_id)
END AS a
FROM (SELECT DISTINCT ON (machine_id) with_parts, amc_validity_upto, machine_id
FROM amc_amcdetail
WHERE machine_id = 2
ORDER BY machine_id, amc_validity_upto
) AS T) AS foo
WHERE a.machine_id = b.id
显示的错误是
ERROR: subquery must return only one column
LINE 5: WHEN count(*) > 0 THEN (SELECT DISTINCT ON (machine_id) w...
任何人都可以说出似乎是什么问题。 基本上,查询是使用表a中的数据更新表b(如果存在),否则使用null更新,false视情况而定。
查询在独立时执行。我正在使用Postgres 9.3,但部署将在postgres9.1
答案 0 :(得分:0)
子查询返回3列
SELECT DISTINCT ON (machine_id) with_parts, amc_validity_upto, machine_id
让它只返回一个
SELECT DISTINCT ON (machine_id) with_parts