简体   繁体   中英

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.

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

In Postgres, you can use something like:

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. 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:

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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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