我想从另一个查询中获得的结果查询每分钟的总和,该查询计算后续值之间的差异。
Select *
from
(
(select division.name,
date(meter_data.reading_date) AS reading_date,
sum(meter_data.meter_delta) 'Gas Consumption (m3)'
from meter, meter_data, division
where meter.meter_id = meter_data.meter_id
and meter.division_id = division.division_id
and Date(meter_data.reading_date) between '2018-04-01' and '2018-05-03'
group by date(meter_data.reading_date)
) as table1,
(Select Date(receipt.receipt_date) AS receipt_date,
sum(case receipt_item.item_name
when 'Gas' then receipt_item.item_amount else 0
end) 'Gas Purchase'
from receipt_item, receipt
where receipt.receipt_id = receipt_item.receipt_id
and Date(receipt.receipt_date) between '2018-04-01' and '2018-05-03'
group by Date(receipt.receipt_date)
) as table2
)
WHERE table1.reading_date = table2.receipt_date;
例如,如果我从suqbquery
获得以下内容select sum(ph1), sum(ph2), sum(ph2) from (select
non_negative_difference(day_chan1) as ph1,
non_negative_difference(day_chan2) as ph2,
non_negative_difference(day_chan3) as ph3
from electricity)
group by time(1m) tz('Europe/Dublin')
我想将它们总结为
time ph1 ph2 ph3
---- --- --- ---
2017-04-02T14:40:38Z 0 0 2
2017-04-02T14:41:38Z 1 1 1
2017-04-02T14:41:39Z 0 0 2
2017-04-02T14:42:38Z 1 1 1
2017-04-02T14:42:39Z 0 1 2
但我从查询得到的是 time ph1 ph2 ph3
---- --- --- ---
2017-04-02T14:40:00Z 0 0 2
2017-04-02T14:41:00Z 1 1 3
2017-04-02T14:42:00Z 1 2 3
,但是如果我自己做了子查询,它会返回结果
答案 0 :(得分:0)
我也为此花了很长时间,终于找到了解决方案:
select sum(ph1), sum(ph2), sum(ph2) from (select
这是正确的。现在我们要在non_negative_difference调用中添加一个聚合函数(正如错误也表明的那样)。我想你想总结一切。
non_negative_difference(sum(day_chan1)) as ph1,
non_negative_difference(sum(day_chan2)) as ph2,
non_negative_difference(sum(day_chan3)) as ph3
from electricity
现在,如果不添加以下行,则内部查询的按功能分组也将为1m。我们不希望这样,因为如果缺少值,则潮涌计算总和的方式将导致很大的差异。因此,我们按照您拥有的最小间隔(例如1秒)对该子查询进行分组
group by time(1s))
最后,您可以按照希望将值加在一起的间隔对外部查询进行分组。
group by time(1m) tz('Europe/Dublin')