DB2 - select 公共主键的多个键值

DB2 - select multiple key values for common primary key

我有以下数据

ID KEY 值

1 K1 1


1 K2 2


2 K1 3


2 K2 4


3 K1 5


3 K2 6

我需要在 DB2 中从上面的 table 数据做一个 select 来显示如下 -

ID Key1 值 Key2 值

1 K1 1 K2 2


2 K1 3 K2 4


3 K1 5 K2 6

您必须将您的来源 table 与自身合并。

一个可能的解决方案是:

select t1.ID, t1.KEY as KEY1, t1.value, t2KEY as KEY2, t2.value
  from <tabname> t1,
       <tabname> t2
 where t1.ID = t2.ID 
   and t1.KEY='K1'
   and t2.KEY='K2'