I have a table something like below: table_a
k | id | results
--------------------
a | 1 | mango
b | 1 | orange
c | 2 | apple
d | 2 | banana
a | 2 | mango
I have two select statements ans there results as below:
first select
select k, id, results from
table_a where id = 1
Results:
k | id | results
--------------------
a | 1 | mango
b | 1 | orange
second select
select k, id, results from
table_a where id = 2
Results:
k | id | results
--------------------
c | 2 | apple
d | 2 | banana
a | 2 | mango
how can I get results as below:
k | id | abc | xyz
------------------------
a | 1 | mango | mango
b | 1 | orange| xx
c | 2 | xx | apple
d | 2 | xx | banana
Thanks Gorgon, Praveen. I get results in both ways.
I have another table as below:
table_b
k | 1 | 2
--------------------
a | |
b | |
c | |
d | |
when I try to update table_b with the below query:
update table_b set
abc = x.abc, xyz = x.xyz from (
select k, id, result as abc, 'xx' as xyz
from table_a
where id = 1
union all
select k, id, 'xx' as abc, result as xyz
from table_a
where id = 2 ) x
where table_b.k = x.k
I am getting results as below:
table_b
k | 1 | 2
--------------------
a | xx | mango
b | orange| xx
c | xx | apple
d | xx | banana
how can I get the below results table_b
k | 1 | 2
--------------------
a | mango | mango
b | orange| xx
c | xx | apple
d | xx | banana
Thanks,