我想在现有输出中添加一列AVG。首先,我使用INNER JOIN计算电力和天然气的使用量。但是现在我想将“ Day hour”的AVG添加到输出中。
表格栏
Id, Datum, Tijd, consumed_rate1, consumed_rate2, gas
当我想输出最近24条记录的消耗率,消耗率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 JOINs合作,但我不知道如何。有没有什么机构可以帮助我呢?
最佳答案
尝试这样的事情:
SELECT
AVG(Elektra), AVG(Gas)
FROM(
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
) as a