如何将SQL查询从输入表中的多行写入输出表中的一行

时间:2014-03-17 05:02:09

标签: sql

请查看以下查询。并建议我

select N_EPD_CORE_BNK_TRANS_ID,
case  
  when X_ASSIGN_STAGE='RM' 
  then  X_ASSIGN_STAGE
  end RM_stage,
  case  
  when X_ASSIGN_STAGE='RM' 
  then X_ACTION
  end RM_action,
  case  
  when X_ASSIGN_STAGE='CO' 
  then  X_ASSIGN_STAGE
  end CO_stage,
  case  
  when X_ASSIGN_STAGE='CO' 
  then X_ACTION
  end CO_action,
  Case  
  when X_ASSIGN_STAGE='CREDITREP' 
  then  X_ASSIGN_STAGE
  end CR_stage,
  Case  
  when X_ASSIGN_STAGE='CREDITREP' 
  then  X_ACTION
  End Cr_Action
  From  Rmwb_Epd_Action_Details_F Where N_Epd_Core_Bnk_Trans_Id='475';

以上查询结果如下三行N_Epd_Core_Bnk_Trans_Id相同ID

 475   null    null     CO      Escalate       null            null
 475   RM      Note     null    null           null            null
 475   null    null     null    null           CREDITREP       Escalate

但是我试图在单一记录中得到以下内容。如果有可能,请建议我。

---------------------------------------------------------------------------------
475     RM  Note    CO  Escalate    CREDITREP   Escalate

1 个答案:

答案 0 :(得分:0)

SELECT   N_EPD_CORE_BNK_TRANS_ID,
    MAX(case  
      when X_ASSIGN_STAGE='RM' 
      then  X_ASSIGN_STAGE
      end) RM_stage,
    MAX(  case  
      when X_ASSIGN_STAGE='RM' 
      then X_ACTION
      end) RM_action,
    MAX(  case  
      when X_ASSIGN_STAGE='CO' 
      then  X_ASSIGN_STAGE
      end) CO_stage,
    MAX(  case  
      when X_ASSIGN_STAGE='CO' 
      then X_ACTION
      end) CO_action,
    MAX(  Case  
      when X_ASSIGN_STAGE='CREDITREP' 
      then  X_ASSIGN_STAGE
      end) CR_stage,
    MAX(  Case  
      when X_ASSIGN_STAGE='CREDITREP' 
      then  X_ACTION
      End )Cr_Action
      From  Rmwb_Epd_Action_Details_F Where N_Epd_Core_Bnk_Trans_Id='475'
    group by N_Epd_Core_Bnk_Trans_Id;