0

我有一张桌子 [dbo].[RISK_DISPCLN]

  RISK_DISCPLN_ID   RISK_DISCPLN_NM
    1                  LegalRisk
    2                  institutional Risk
    4                  Market Risk

选项卡名称 [dbo].[PROJ_RISK_DISCPLN]

   PROJ_ID  RISK_DISCPLN_ID
     1      1
     1      2
     1      4
     2      1
     2      2
     2      4
     3      1
     3      2
     3      4

表名 [dbo].[USER]

     USER_ID    USER_FIRST_NM   USER_LST_NM
      2        saravanakumar    rajkumar
      3        Soosai          Antony
      4       Adam             Allen
      5       Babita           Tripathy
      9       stacey            Davis
      11      NULL              NULL

我尝试在下面的查询中将名称显示为相应的风险,我将所有名称都作为空值...请帮助

 ;with cte as   
                (  
SELECT [PROJ_RISK_DISCPLN].PROJ_ID,
       [USER].USER_LST_NM + ' '+ [USER].USER_FIRST_NM  as Name,
       [RISK_DISCPLN].RISK_DISCPLN_NM,
        [RISK_DISCPLN].RISK_DISCPLN_ID,
        [USER].[USER_ID]                       
FROM  dbo.[PROJ_RISK_DISCPLN]     
left join [dbo].[USER]    
on [PROJ_RISK_DISCPLN].RISK_OWN_USER_ID = [dbo].[USER].[USER_ID]
left join  dbo.[RISK_DISCPLN] on
[PROJ_RISK_DISCPLN].RISK_DISCPLN_ID = [RISK_DISCPLN].RISK_DISCPLN_ID
)   
select *  
from  
(  
select c1.PROJ_ID,
c1.RISK_DISCPLN_NM,  
STUFF(  
 (SELECT ', ' + c2.Name  
  FROM cte c2  
  where c1.PROJ_ID = c2.PROJ_ID  
 and c1.RISK_DISCPLN_ID = c2.RISK_DISCPLN_ID
  FOR XML PATH (''))  
  , 1, 1, '')  AS Name  
 from cte c1  
 ) d      
 pivot  
 (  
  max(Name)  
  for RISK_DISCPLN_NM in ([LegalRisk Owner],[institutional Risk Owner],[Market Risk Owner])  
  ) piv
4

1 回答 1

0

我得到了答案,相反 [LegalRisk Owner],[institutional Risk Owner],[Market Risk Owner] 我必须使用 [LegalRisk],[institutional Risk],[Market Risk] ....

于 2013-08-23T08:09:24.757 回答