在不同行的相同数据列中记录记录

时间:2015-01-07 09:25:15

标签: sql sql-server sql-server-2008

我需要在不同的行中加入相同的记录。就像我的TESTNAME是不同行中的相同项目名称我需要将它们分组并将其显示为一行,我使用的查询也是为了制作枢轴请找到下面的代码

select ProjectName,  
ProjectNos,  
isnull([Questionnaire Submission for sripting],'') as c1,  
isnull([Programing of questionnaire and/or ConJoint Design/Email Send out],'') as c2,  
isnull([Quality Check (Desktop and Mobile)],'') as c3,  
isnull([Questionaire link sent to client for review],'') as c4,  
isnull([Hard Launch],'') as c5,  
isnull([Data Collection],'') as c6,  
isnull([Quality Check (Sampling)],'') as c7,  
isnull([Status of sampling process sent to client],'') as c8,  
isnull([Data Exported & Cleansed],'') as c9,  
isnull([Coding of Open Answers],'') as c10,  
isnull([Table Report],'') as c11,  
isnull([PPT Report],'') as c12,  
isnull([Data Analysis],'') as c13,  
isnull([Raw Data Delivery to CDT],'')as c14,  
isnull([Quality Check ( Table Report/Raw Data)],'')as c15,  
isnull([Power Point Quality Check ],'')as c16,  
isnull([Final Presentation Delivery to CDT],'')as c17,  
isnull([Extra Analysis (after presentation)],'')as c18,  
isnull([N-visualize report],'')as c19,  
isnull([Excel Delivery],'') as c20 from tbl_ProjectDetails   
PIVOT   (  
 max(Status_Name) for   
 [Task_assigned] IN   
 ([Questionnaire Submission for sripting],  
 [Programing of questionnaire and/or ConJoint Design/Email Send out],  
 [Quality Check (Desktop and Mobile)],  
 [Questionaire link sent to client for review],  
  [Hard Launch],  
 [Data Collection],  
 [Quality Check (Sampling)],  
 [Status of sampling process sent to client],  
 [Data Exported & Cleansed ],  
 [Coding of Open Answers],  
 [Table Report],[PPT Report],  
 [Data Analysis],  
 [Raw Data Delivery to CDT],  
 [Quality Check ( Table Report/Raw Data)],  
 [Power Point Quality Check ],  
 [Final Presentation Delivery to CDT],  
 [Extra Analysis (after presentation)],  
 [N-visualize report],[Excel Delivery])) as pivottable where flag='0'

及其输出如下

ProjectName ProjectNos                  c1              c2         c3                 c4        
test 123    123456                  Not yet started                                                         
TESTNAME    45612                                       Alert                                                           
testfr      785412                  Missed deadline         
sdsd        adsads                                                Not Applicable                                                        
tyeuir      8948948                 In progress                                                                     
wwew        2324234234                                                                Not yet started                           
TESTNAME    45612                   Missed deadline                                                                         

1 个答案:

答案 0 :(得分:1)

试试这个:

select ProjectName,  
ProjectNos,  
isnull([Questionnaire Submission for sripting],'') as c1,  
isnull([Programing of questionnaire and/or ConJoint Design/Email Send out],'') as c2,  
isnull([Quality Check (Desktop and Mobile)],'') as c3,  
isnull([Questionaire link sent to client for review],'') as c4,
isnull([Hard Launch],'') as c5,  
isnull([Data Collection],'') as c6,  
isnull([Quality Check (Sampling)],'') as c7,  
isnull([Status of sampling process sent to client],'') as c8,  
isnull([Data Exported & Cleansed],'') as c9,  
isnull([Coding of Open Answers],'') as c10,  
isnull([Table Report],'') as c11,  
isnull([PPT Report],'') as c12,  
isnull([Data Analysis],'') as c13,  
isnull([Raw Data Delivery to CDT],'')as c14,  
isnull([Quality Check ( Table Report/Raw Data)],'')as c15,  
isnull([Power Point Quality Check ],'')as c16,  
isnull([Final Presentation Delivery to CDT],'')as c17,  
isnull([Extra Analysis (after presentation)],'')as c18,  
isnull([N-visualize report],'')as c19,  
isnull([Excel Delivery],'') as c20 
from (
    select ProjectName,  
    ProjectNos,
    Status_Name,
    [Task_assigned] 
    from tbl_ProjectDetails
    where flag='0') as t
PIVOT   ( 
 max(Status_Name) for   
 [Task_assigned] IN   
 ([Questionnaire Submission for sripting],  
 [Programing of questionnaire and/or ConJoint Design/Email Send out],  
 [Quality Check (Desktop and Mobile)],  
 [Questionaire link sent to client for review],  
  [Hard Launch],  
 [Data Collection],  
 [Quality Check (Sampling)],  
 [Status of sampling process sent to client],  
 [Data Exported & Cleansed ],  
 [Coding of Open Answers],  
 [Table Report],[PPT Report],  
 [Data Analysis],  
 [Raw Data Delivery to CDT],  
 [Quality Check ( Table Report/Raw Data)],  
 [Power Point Quality Check ],  
 [Final Presentation Delivery to CDT],  
 [Extra Analysis (after presentation)],  
 [N-visualize report],[Excel Delivery])) as pivottable 

结果

ProjectName ProjectNos  c1              c2      c3              c4  ...
-------------------------------------------------------------------------------
sdsd        adsads                              Not Applicable                                                                  
test 123    123456      Not yet started                                                                         
testfr      785412      Missed deadline                                                                         
TESTNAME    45612       Missed deadline Alert                                                                       
tyeuir      8948948     In progress                                                                         
wwew    2324234234                                              Not yet started