我想在一排中获得3个SQL查询的结果

时间:2019-02-04 17:17:23

标签: mysql sql rdbms

我有一个结果集,每个结果集有3条记录。我希望它们排成一排而不是三排。

我只想从该行中获取特定值。

enter image description here

3 个答案:

答案 0 :(得分:1)

使用聚合函数

select col1,col3,min(timestamp1) as timestamp1,
min(timestamp2) as timestamp2,min(timestamp3) as timestamp3
from t group by  col1,col3

答案 1 :(得分:0)

您似乎想要条件聚合:

select col1, col3,
       max(case when column2 = 'abc' then timestamp1 end) as timestamp1,
       max(case when column2 = 'xyz' then timestamp2 end) as timestamp2,
       max(case when column2 = 'pqr' then timestamp3 end) as timestamp3
from t
group by col1, col3;

注意:根据所提供的数据,我拥有Zaynul的答案对我来说实际上更有意义。假设期望的结果是最小值是更简单的。但是,这也是对该问题的有效解释,并在对该答案的评论中得到了澄清。

答案 2 :(得分:0)

不确定要应用的确切逻辑,但从我的问题来看,似乎您只需要解决数据帧对齐问题,并且如下所示的查询应该可以解决您所要查找的问题,

Select 
    Column1, Column2,
    (select timestamp1 from table 
     where (your logic to filter)) as T1, 
    (select timestamp2 from table 
     where (your logic to filter)) as T2, 
    (select timestamp3 from table 
     where (your logic to filter)) as T3 
from 
    table 
where 
    (your master filter);