结合多个select语句的结果。

时间:2014-03-19 14:43:22

标签: sql ms-access-2007

我拥有的Access数据库是一个大型单表 - 设计更像是电子表格而不是数据库。我试图从中获得3种不同组件的产量。

我已经提出了查询以获取每个项目的失败和失败价值的机会。计算这些值中的每一个都需要它自己独立的where子句 - 它不像遗憾地对列进行求和那么简单。所以我试图通过使用每个查询的结果来计算收益率,然后将它们结合在一起。这是一个伪示例:

Select "Component 1", ((t1.OpForFails - IIf(t2.GenuineFails IS NULL, 0,t2.GenuineFails)) * 100.0) / t1.OpForFails as Yield FROM t1 and t2

UNION 

Select "Component 2", ((t3.OpForFails - IIf(t4.GenuineFails IS NULL, 0,t4.GenuineFails)) * 100.0) / t3.OpForFails as Yield FROM t3 and t4

UNION 

Select "Component 3", ((t5.OpForFails - IIf(t6.GenuineFails IS NULL, 0,t6.GenuineFails)) * 100.0) / t5.OpForFails as Yield FROM t5 and t6

给我一​​个输出:

Component 1      81
Component 2      94
Component 3      77

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:0)

只需使用UNION ALL并确保您在选择中拥有相同数量和类型的列。

Here is the full article