这里的另一个小问题。 我必须合并两个不同的表。 在第一个中,我必须找到一小时日期时间范围内的平均外部温度。 换句话说,我必须在一小时的日期时间范围内计算每小时千瓦的使用量。 单个查询完美地运行,但是当我尝试加入它们时,它们不起作用。 此查询不起作用:
select dataora, AVG(Test)
from temperature
where dataora between '2013-11-01' and '2013-11-31' GROUP BY date(dataora),hour(dataora)
natural join (
SELECT
(t1.dt) AS time,(case
when t1.value>t2.value AND t2.value>0
then abs(t1.value - t2.value)
when t1.value>t2.value AND t2.value=0
then t2.value
else t1.value END)kwhg,
(case
when t1.value1>t2.value1 AND t2.value1>0
then abs(t1.value1 - t2.value1)
when t1.value1>t2.value1 AND t2.value1=0
then t2.value1
when t1.value1=t2.value1 AND t1.value1>0
then abs(t1.value1 - t2.value1)
else 0 END) kwhc,
(case
when t1.value2>t2.value2 AND t2.value2>0
then abs(t1.value2 - t2.value2)
when t1.value2>t2.value2 AND t2.value2=0
then t2.value2
when t1.value2=t2.value2 AND t1.value2>0
then abs(t1.value2 - t2.value2)
else 0 END)kwhi,
(case
when t1.value3>t2.value3 AND t2.value3>0
then abs(t1.value3 - t2.value3)
when t1.value3>t2.value3 AND t2.value3=0
then t2.value3
when t1.value3=t2.value3 AND t1.value3>0
then abs(t1.value3 - t2.value3)
else 0 END) kwhfm
FROM
(SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3 FROM misure where dataora between '2013-11-01' and '2013-11-31' GROUP BY date(dt),hour(dt)) t1
JOIN
(SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3 FROM misure where dataora between '2013-11-01' and '2013-11-31' GROUP BY date(dt),hour(dt)) t2
on t1.dt = t2.dt + interval 1 hour) t3
这个只给我一行(第一行,而不是本例中需要的720行):
SELECT
(t1.dt) AS time,(case
when t1.value>t2.value AND t2.value>0
then abs(t1.value - t2.value)
when t1.value>t2.value AND t2.value=0
then t2.value
else t1.value END)kwhg,
(case
when t1.value1>t2.value1 AND t2.value1>0
then abs(t1.value1 - t2.value1)
when t1.value1>t2.value1 AND t2.value1=0
then t2.value1
when t1.value1=t2.value1 AND t1.value1>0
then abs(t1.value1 - t2.value1)
else 0 END) kwhc,
(case
when t1.value2>t2.value2 AND t2.value2>0
then abs(t1.value2 - t2.value2)
when t1.value2>t2.value2 AND t2.value2=0
then t2.value2
when t1.value2=t2.value2 AND t1.value2>0
then abs(t1.value2 - t2.value2)
else 0 END)kwhi,
(case
when t1.value3>t2.value3 AND t2.value3>0
then abs(t1.value3 - t2.value3)
when t1.value3>t2.value3 AND t2.value3=0
then t2.value3
when t1.value3=t2.value3 AND t1.value3>0
then abs(t1.value3 - t2.value3)
else 0 END) kwhfm, AVG(Test)
FROM
(SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3 FROM misure where dataora between '2013-11-01' and '2013-11-31' GROUP BY date(dt),hour(dt)) t1
JOIN
(SELECT (dataora) dt, MAX(kwhg) value, MAX(kwhc) value1, MAX(kwhi) value2, MAX(kwhfm) value3 FROM misure where dataora between '2013-11-01' and '2013-11-31' GROUP BY date(dt),hour(dt)) t2
on t1.dt = t2.dt + interval 1 hour
natural join temperature
我使用自然连接,因为我需要每小时真正的千瓦和每小时外部温度的4个测量值(每15分钟)的平均值。 也许答案很简单,但我还没有找到它! 在此先感谢您的任何帮助
答案 0 :(得分:2)
请勿使用natural join
。它连接在具有相同名称的所有字段上。相反,做一个正常的连接(我猜是inner join
或left outer join
)并在日期使用on
子句:
(<subquery 1>) s1 join
(<subquery 2>) s2
on s1.time = s2.time