为不同的标准多次选择相同的列 - SQL

时间:2017-02-08 20:09:44

标签: sql sqlite

我正在尝试运行一个选择两个相同列但具有不同条件/条件的查询。我可以独立运行该查询并获得结果但是当我尝试一起运行它们时(如第二部分所示)我得到一个错误。预期表如下所示为save_line1,save_line2。但我获得的输出是第一行 - 对于所有行。

我想知道对我的第二个查询做了哪些更改才能使其正常工作。谢谢。

select save_line from save_output_table  
where execution_id in ('292') 
and seq_id = '0' 
and save_type='R' 
order by line_id ASC

+------------+
| Save_line  |
+------------+
|         17 |
|         22 |
|         23 |
+------------+



SELECT 
( select save_line 
 from save_output_table
 where execution_id in ('292') 
 and seq_id = '0' 
and save_type='R' 
order by line_id ASC 
) as save_line1,
( select save_line 
  from save_output_table  
 where execution_id in ('286') 
and seq_id = '0' 
and save_type='R' 
order by line_id ASC 
 ) as save_line2
 from save_output_table 

 +-------------+------------+
 | Save_line 1 | Save_line2 |
 +-------------+------------+
 |          17 |          9 |
 |          22 |          5 |
 |          23 |          3 |
 +-------------+------------+

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT stragg( distinct(save_line1.save_line)), stragg( distinct(save_line2.save_line))
from
( select save_line 
 from save_output_table 
 where execution_id in ('292') 
 and seq_id = '0' 
and save_type='R' 
order by line_id ASC 
) as save_line1,
( select save_line 
  from save_output_table  
 where execution_id in ('286') 
and seq_id = '0' 
and save_type='R' 
order by line_id ASC 
 ) as save_line2