查询中的联合字段

时间:2017-07-10 09:39:31

标签: sql firebird

我的表格包含字段A1,A2,A3,A4,B1,B2,B3和B4。我需要一个选择语句,即联合字段:C1中的A1和B1,C2中的A2和B2,C3中的A3和B3以及C4中的B4

例如:

 TABLE ONE
 A1 A2 A3 A4 B1 B2 B3 B4
 1  1  1  1  0  0  0  0
 2  2  2  2  0  0  0  0
 0  0  0  0  3  3  3  3 

我需要这样的结果:

 TABLE TWO
 C1 C2 C3 C4 
 1  1  1  1
 2  2  2  2 
 3  3  3  3

2 个答案:

答案 0 :(得分:0)

你需要这样的东西

[0]

答案 1 :(得分:0)

应该很容易:

SELECT A1 AS C1
      ,A2 AS C2
      ,A3 AS C3
      ,A4 AS C4
  FROM table_one
 WHERE A1 != 0 
   AND A2 != 0
   AND A3 != 0
   AND A4 != 0
UNION ALL 
SELECT B1 AS C1
      ,B2 AS C2
      ,B3 AS C3
      ,B4 AS C4
  FROM table_two
 WHERE B1 != 0 
   AND B2 != 0
   AND B3 != 0
   AND B4 != 0