示例,我有SQL - 查询:
SELECT
(SELECT value->>"$[0].value"
FROM leads_fields
WHERE leads_fields.field_id = 244991 AND leads_fields.lead_id = leads.external_id)
as 'coefRL',
(SELECT value->>"$[0].value"
FROM leads_fields
WHERE leads_fields.field_id = 244943 AND leads_fields.lead_id = leads.external_id)
as 'coefWF'
FROM leads
为了得到这些值的不同,我需要像这样用第3个子查询复制这些请求吗?
(
(SELECT value->>"$[0].value"
FROM leads_fields
WHERE leads_fields.field_id = 244991 AND leads_fields.lead_id = leads.external_id)
-
(SELECT value->>"$[0].value"
FROM leads_fields
WHERE leads_fields.field_id = 244943 AND leads_fields.lead_id = leads.external_id)
) as 'difference'
或者有更实用的方法,例如(coefRL - coefWF)
?
答案 0 :(得分:1)
这是一种没有第三个子查询的方法......
SELECT coefRL,
coefWF,
coefRL-coefWF difference
FROM
(SELECT
(SELECT value->>"$[0].value"
FROM leads_fields
WHERE leads_fields.field_id = 244991
AND leads_fields.lead_id = leads.external_id
) AS 'coefRL',
(SELECT value->>"$[0].value"
FROM leads_fields
WHERE leads_fields.field_id = 244943
AND leads_fields.lead_id = leads.external_id
) AS 'coefWF'
FROM leads
)