【发布时间】:2014-11-14 18:21:21
【问题描述】:
我想在现有输出中添加一列 AVG。首先,我使用 INNER JOIN 计算电力和天然气的使用量。但现在我想将“Day hour”的 AVG 添加到输出中。
表格列
Id, Datum, Tijd, consumed_rate1, consumed_rate2, gas
当我想输出最后 24 条记录并使用消耗率 1、消耗率 2 和气体时,我使用以下查询:
SELECT
A.Datum AS Datum,
A.Tijd AS Tijd,
A.Daguur AS Daguur,
A.Aantal AS Aantal,
A.Consumed_rate1 + A.Consumed_rate2 AS Elektra,
(A.Consumed_rate1 + A.Consumed_rate2 - B.Consumed_rate1 - B.Consumed_rate2) AS 'Elektra verbruikt',
A.gas AS Gas, (A.gas - B.gas) AS 'Gas verbruikt'
FROM smartmeter A
INNER JOIN smartmeter B ON B.id = (A.id-1)
ORDER BY A.Id DESC
LIMIT 24
现在我想添加一个列,其中 AVG 的使用量来自消耗率 1 + 消耗率 2 和气体。我想我必须使用 LEFT JOIN,但我不知道怎么做。有没有机构可以帮助我解决这个问题?
【问题讨论】:
-
你能用伪代码写出来吗?我没有得到
consumed_rate1 + consumed_rate2 and gas。你的意思是什么?