2017-06-01 116 views
0

我的结构是这样的SQL:联盟与子查询

table1的 键 值 日期

表2 键 值 日期

表3 键 值 日期

我想hav e sql查询返回不同所有密钥 以及所有3个表中每个密钥的所有值的总和。

我的尝试已经

选择键=
(FROM表1中选择table1.key其中date = '二○一七年五月三十〇日' 工会
选择table2.key从表2其中date =' 2017-05-30'union
select table3.key from table3 where date ='2017-05-30'),
(select1(sum)(value) from table1 where table1.key = key and date ='2017-05 -30')+
select (sum(value)from table2 where table2.key = key and date =从表'2017年5月30日')+
选择(总和(值),其中table3.key =键 和日期= '2017年5月30日')
从表1,表2,表3

+1

请格式化查询 - 它几乎不可读 –

回答

1

您可以将3个表格组合成临时表格,然后在临时表格上执行您需要执行的操作:

CREATE TEMP TABLE temp1 AS 
select key, value, date from table1 
union all 
select key, value, date from table2 
union all 
select key, value, date from table3