我需要在查询中同时使用“union”和“into”。这两个版本运行正常
SELECT x.* INTO
NewTABLE FROM
(SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2) x
或者
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, val2 FROM TABLE1 UNION SELECT val1, val2 FROM TABLE2) x
但我需要的是这样的东西
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2 FROM TABLE1
UNION SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2 FROM TABLE2) x
看起来总和和/或Iif是问题所在。我该如何解决这个问题!?
答案 0 :(得分:1)
你忘记了工会选择的GROUP BY。
SELECT x.* INTO
NewTABLE FROM
(SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2
FROM TABLE1 GROUP BY Val1
UNION
SELECT val1, sum(Iif(val2<0,0,val2)) as PositiveVal2
FROM TABLE2 GROUP BY Val1) x
您必须聚合或GROUP BY聚合查询中的所有选择字段。