简体   繁体   English

SQL查询以比较位于同一列中但由两组不同的查询返回的两个值

[英]SQL Query to compare two values which are in the same column but returned by two different set of queries

I have a table similar to the one shown below. 我有一张类似于以下所示的表格。

-----------------------------
JOB ID | parameter | result |
-----------------------------
1       | xyz      | 10     |
1       | abc      | 15     |
2       | xyz      | 12     |
2       | abc      | 8      |
2       | mno      | 20     |
-----------------------------

I want the result as shown below. 我想要如下所示的结果。

 parameter | result 1 | result 2 |
----------------------------------
   xyz     |  10      |  12      |
   mno     |  NULL    |  20      |
   abc     |  15      |  8       |
----------------------------------

My goal is to have a single table which can compare the result values of two different jobs. 我的目标是拥有一个表,该表可以比较两个不同作业的结果值。 It can be two or more jobs. 它可以是两个或多个工作。

you want to simulate a pivot table since mysql doesn't have pivots. 您想要模拟数据透视表,因为mysql没有数据透视表。

select 
    param, 
    max(case when id = 1 then res else null end) as 'result 1',
    max(case when id = 2 then res else null end) as 'result 2'
from table
group by param

SQL FIDDLE TO PLAY WITH SQL玩

In Postgres, you can use something like: 在Postgres中,您可以使用以下方法:

select parameter, (array_agg(result))[1], (array_agg(result))[2] from my_table group by parameter;

The idea is: aggregate all the results for a given parameter into an array of results, and then fetch individual elements from those arrays. 这个想法是:将给定参数的所有结果聚合到一个结果数组中,然后从这些数组中获取单个元素。

I think that you can achieve something similar in MySQL by using GROUP_CONCAT() , although it returns a string instead of an array, so you cannot easily index it. 我认为您可以通过使用GROUP_CONCAT()在MySQL中实现类似的功能,尽管它返回的是字符串而不是数组,因此您无法轻松地对其进行索引。 But you can split by commas after that. 但是之后您可以按逗号分开。

If you are using MySQL there are no "outer join" need to use union right and left join: Something like: 如果使用的是MySQL,则不需要“外部联接”就可以使用左右联接:

select t1.parameter, t1.result 'Result 1', t2.result 'Result 2' from
table as t1 left join table as t2
on t1.parameter=t2.parameter
where t1.'JOB ID' = 1 and t2.'JOB ID' = 2
union
select t1.parameter, t1.result 'Result 1', t2.result 'Result 2' from
table as t1 right join table as t2
on t1.parameter=t2.parameter
where t1.'JOB ID' = 1 and t2.'JOB ID' = 2

If the SQL with full outer join will make it more easier: 如果具有完全外部联接的SQL将使其更容易:

select t1.parameter, t1.result 'Result 1', t2.result 'Result 2' from
table as t1 outer join table as t2
on t1.parameter=t2.parameter
where t1.'JOB ID' = 1 and t2.'JOB ID' = 2
select q1.parameter, q2.result as r1, q3.result as r2
  from 
    (select distinct parameter from temp2) q1
    left join (select parameter, result from temp2 where job_id = 1) q2
      on q1.parameter = q2.parameter
    left join (select parameter, result from temp2 where job_id = 2) q3
      on q1.parameter = q3.parameter;

It works, but it's not efficient. 它可以工作,但是效率不高。 Still, since I'm gathering you are trying to solve something more complex than what's presented, this might help form your general solution. 不过,由于我正在召集您,您正在尝试解决比所介绍的问题更复杂的问题,因此这可能有助于形成您的一般解决方案。

While I'm at it, here's a slightly cleaner solution: 在我这样做的时候,这是一个稍微干净的解决方案:

select distinct q1.parameter, q2.result as r1, q3.result as r2
  from 
    temp2 q1
    left join (select parameter, result from temp2 where job_id = 1) q2
      on q1.parameter = q2.parameter
    left join (select parameter, result from temp2 where job_id = 2) q3
      on q1.parameter = q3.parameter;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM