这是我想要的值,但想知道我是否可以在一个查询中执行此操作并将其放入一个已经拥有大量信息的临时表中。 我可以创建另一个表并放入4Tot,16Tot,TotE,然后使用该表转移到另一个表,我该如何解决这个问题? 谢谢!
4Tot = (select ROUND(b.value-a.value,1) from history a, history b where a.name='TAGG1'
and b.name=a.name and a.ts between st and (st+00:01) and b.ts between en and (en+00:01));
16Tot = (select ROUND(b.value-a.value,1) from history a, history b
where a.name='TAGG2' and b.name=a.name
and a.ts between st and (st+00:01) and b.ts between en and (en+00:01));
TotE = (select ROUND(b.value-a.value,1) from history a, history b where
a.name='Tagg3' and b.name=a.name
and a.ts between st and (st+00:01) and b.ts between en and (en+00:01));
答案 0 :(得分:0)
试试这个:
select sum(case a.name when 'TAGG1' then ROUND(b.value-a.value,1) else 0 end) as 4Tot,
sum(case a.name when 'TAGG2' then ROUND(b.value-a.value,1) else 0 end) as 16Tot,
sum(case a.name when 'TAGG3' then ROUND(b.value-a.value,1) else 0 end) as TotE
from history a, history b
where b.name=a.name
and a.ts between st and (st+00:01)
and b.ts between en and (en+00:01)
根据您在评论中的其他问题,要使用此查询存储在表中,您只需要使用此查询的insert语句,如下所示:
当然,我正在考虑你的另一张桌子只有这三列。
insert into SomeNewTable ( col1, col2, col3 )
select sum(case a.name when 'TAGG1' then ROUND(b.value-a.value,1) else 0 end) as 4Tot,
sum(case a.name when 'TAGG2' then ROUND(b.value-a.value,1) else 0 end) as 16Tot,
sum(case a.name when 'TAGG3' then ROUND(b.value-a.value,1) else 0 end) as TotE
from history a, history b
where b.name=a.name
and a.ts between st and (st+00:01)
and b.ts between en and (en+00:01)