帮助向我的 sql 过程结果添加一个字段

发布于 2024-10-13 05:59:10 字数 2540 浏览 2 评论 0原文

我有下面的 SQL 过程,我正在尝试将 SRS 开始日期字段添加到下面的过程的结果集中。我想为 SRS_StartDate 添加一个字段。该字段应定义为:coalesce(projectMilestone.startDate,releaseschedual.startDate) as SRS_StartDate - 但仅适用于 CID=37,因为这是 SRS 的 cid

我在下面附上我的示例 sql 代码我当前的进程和还有表结构和数据的文件

您可以使用这些项目 ID 进行测试,因为 Excel 文件有数据支持它们。

exec rainbows '66,97,25'   --testing



create PROC rainbows
    @ProjectIDs NVARCHAR(1000)      
AS      

DECLARE @ProjIDs TABLE (id INT)     
INSERT @ProjIDs  SELECT DISTINCT value FROM fn_Split(@ProjectIDs, ',')    

--get core data set
SELECT t1.ProjectID,t1.ProjectName,
       case sco.CID when 37 then 'BRS Start'
                    when 37 then 'BRS end'
                    when 39 then 'SRS'
                    when 41 then 'SAD'
                    when 45 then 'Product Profile Review'
                    when 47 then 'SE Integration'
                    when 50 then 'IDE'
                    when 53 then 'UAT'
                    when 72 then 'PE Testing'
                    when 73 then 'Code Freeze'
                    when 75 then 'Dark Pod'
                    when 77 then 'Production' end CID, 
       coalesce(t2.EndDate, t1.EndDate) MilestoneEndDate 
INTO #DATA    
FROM StatusCode sco    
      LEFT OUTER JOIN    
           (SELECT p.ProjectId,p.ProjectName,sc.CID,rs.EndDate
              FROM StatusCode sc    
             INNER JOIN ReleaseSchedule rs ON sc.CID = rs.MilestoneCID    
             INNER JOIN Project p  ON rs.ReleaseID = p.ReleaseID 
             INNER JOIN @ProjIDs pList ON p.ProjectId = pList.id
            ) AS t1 ON sco.CID = t1.CID    
      LEFT OUTER JOIN    
           (SELECT sc.CID, pm.EndDate
              FROM StatusCode sc    
             INNER JOIN ProjectMilestone pm ON sc.CID = pm.MilestoneCID 
             INNER JOIN @ProjIDs pList ON pm.ProjectId = pList.id
            ) AS t2 ON sco.CID = t2.CID   
WHERE sco.CID IN (37, 39, 41, 45, 47, 50, 53, 72, 73, 75, 77)    

select ProjectID,ProjectName,[BRS start],[BRS Start] [BRS end],[SRS],[SAD],[Product Profile Review],[SE Integration],[IDE],[UAT],
                                         [PE Testing],[Code Freeze],[Dark Pod],[Production]
 from (
SELECT * FROM  #DATA
PIVOT (MAX(MilestoneEndDate) FOR CID IN ([BRS start],[BRS end],[SRS],[SAD],[Product Profile Review],[SE Integration],[IDE],[UAT],
                                         [PE Testing],[Code Freeze],[Dark Pod],[Production]
)) AS P) t1

I have the SQL proc below that I am trying to add a SRS start date field to the result set of the proc below. I want to add a field for SRS_StartDate. The field should be defined as such: coalesce(projectMilestone.startDate, releaseschedual.startDate) as SRS_StartDate - but only for CID=37 as this is the cid for SRS

I am attaching my sample sql code below for my current proc and a file of table structure and data as well.

you can test with these projectIDs as the excel file has data to support them.

exec rainbows '66,97,25'   --testing



create PROC rainbows
    @ProjectIDs NVARCHAR(1000)      
AS      

DECLARE @ProjIDs TABLE (id INT)     
INSERT @ProjIDs  SELECT DISTINCT value FROM fn_Split(@ProjectIDs, ',')    

--get core data set
SELECT t1.ProjectID,t1.ProjectName,
       case sco.CID when 37 then 'BRS Start'
                    when 37 then 'BRS end'
                    when 39 then 'SRS'
                    when 41 then 'SAD'
                    when 45 then 'Product Profile Review'
                    when 47 then 'SE Integration'
                    when 50 then 'IDE'
                    when 53 then 'UAT'
                    when 72 then 'PE Testing'
                    when 73 then 'Code Freeze'
                    when 75 then 'Dark Pod'
                    when 77 then 'Production' end CID, 
       coalesce(t2.EndDate, t1.EndDate) MilestoneEndDate 
INTO #DATA    
FROM StatusCode sco    
      LEFT OUTER JOIN    
           (SELECT p.ProjectId,p.ProjectName,sc.CID,rs.EndDate
              FROM StatusCode sc    
             INNER JOIN ReleaseSchedule rs ON sc.CID = rs.MilestoneCID    
             INNER JOIN Project p  ON rs.ReleaseID = p.ReleaseID 
             INNER JOIN @ProjIDs pList ON p.ProjectId = pList.id
            ) AS t1 ON sco.CID = t1.CID    
      LEFT OUTER JOIN    
           (SELECT sc.CID, pm.EndDate
              FROM StatusCode sc    
             INNER JOIN ProjectMilestone pm ON sc.CID = pm.MilestoneCID 
             INNER JOIN @ProjIDs pList ON pm.ProjectId = pList.id
            ) AS t2 ON sco.CID = t2.CID   
WHERE sco.CID IN (37, 39, 41, 45, 47, 50, 53, 72, 73, 75, 77)    

select ProjectID,ProjectName,[BRS start],[BRS Start] [BRS end],[SRS],[SAD],[Product Profile Review],[SE Integration],[IDE],[UAT],
                                         [PE Testing],[Code Freeze],[Dark Pod],[Production]
 from (
SELECT * FROM  #DATA
PIVOT (MAX(MilestoneEndDate) FOR CID IN ([BRS start],[BRS end],[SRS],[SAD],[Product Profile Review],[SE Integration],[IDE],[UAT],
                                         [PE Testing],[Code Freeze],[Dark Pod],[Production]
)) AS P) t1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2024-10-20 05:59:10
create PROC rainbows
    @ProjectIDs NVARCHAR(1000)      
AS      

DECLARE @ProjIDs TABLE (id INT)     
INSERT @ProjIDs  SELECT DISTINCT value FROM fn_Split(@ProjectIDs, ',')    

--get core data set
SELECT t1.ProjectID,t1.ProjectName,
       case sco.CID when 37 then 'BRS Start'
                    when 37 then 'BRS end'
                    when 39 then 'SRS'
                    when 41 then 'SAD'
                    when 45 then 'Product Profile Review'
                    when 47 then 'SE Integration'
                    when 50 then 'IDE'
                    when 53 then 'UAT'
                    when 72 then 'PE Testing'
                    when 73 then 'Code Freeze'
                    when 75 then 'Dark Pod'
                    when 77 then 'Production' end CID, 
       coalesce(t2.EndDate, t1.EndDate) MilestoneEndDate ,
       CASE WHEN sco.CID=37 then coalesce(t2.startDate, t1.startDate) else null as SRS_StartDate
INTO #DATA    
FROM StatusCode sco    
      LEFT OUTER JOIN    
           (SELECT p.ProjectId,p.ProjectName,sc.CID,rs.EndDate, rs.startDate
              FROM StatusCode sc    
             INNER JOIN ReleaseSchedule rs ON sc.CID = rs.MilestoneCID    
             INNER JOIN Project p  ON rs.ReleaseID = p.ReleaseID 
             INNER JOIN @ProjIDs pList ON p.ProjectId = pList.id
            ) AS t1 ON sco.CID = t1.CID    
      LEFT OUTER JOIN    
           (SELECT sc.CID, pm.EndDate, pm.startDate
              FROM StatusCode sc    
             INNER JOIN ProjectMilestone pm ON sc.CID = pm.MilestoneCID 
             INNER JOIN @ProjIDs pList ON pm.ProjectId = pList.id
            ) AS t2 ON sco.CID = t2.CID   
WHERE sco.CID IN (37, 39, 41, 45, 47, 50, 53, 72, 73, 75, 77)    

select ProjectID,ProjectName,SRS_StartDate,[BRS start],[BRS Start] [BRS end],[SRS],[SAD],[Product Profile Review],[SE Integration],[IDE],[UAT],
                                         [PE Testing],[Code Freeze],[Dark Pod],[Production]
 from (
SELECT * FROM  #DATA
PIVOT (MAX(MilestoneEndDate) FOR CID IN ([BRS start],[BRS end],[SRS],[SAD],[Product Profile Review],[SE Integration],[IDE],[UAT],
                                         [PE Testing],[Code Freeze],[Dark Pod],[Production]
)) AS P) t1
create PROC rainbows
    @ProjectIDs NVARCHAR(1000)      
AS      

DECLARE @ProjIDs TABLE (id INT)     
INSERT @ProjIDs  SELECT DISTINCT value FROM fn_Split(@ProjectIDs, ',')    

--get core data set
SELECT t1.ProjectID,t1.ProjectName,
       case sco.CID when 37 then 'BRS Start'
                    when 37 then 'BRS end'
                    when 39 then 'SRS'
                    when 41 then 'SAD'
                    when 45 then 'Product Profile Review'
                    when 47 then 'SE Integration'
                    when 50 then 'IDE'
                    when 53 then 'UAT'
                    when 72 then 'PE Testing'
                    when 73 then 'Code Freeze'
                    when 75 then 'Dark Pod'
                    when 77 then 'Production' end CID, 
       coalesce(t2.EndDate, t1.EndDate) MilestoneEndDate ,
       CASE WHEN sco.CID=37 then coalesce(t2.startDate, t1.startDate) else null as SRS_StartDate
INTO #DATA    
FROM StatusCode sco    
      LEFT OUTER JOIN    
           (SELECT p.ProjectId,p.ProjectName,sc.CID,rs.EndDate, rs.startDate
              FROM StatusCode sc    
             INNER JOIN ReleaseSchedule rs ON sc.CID = rs.MilestoneCID    
             INNER JOIN Project p  ON rs.ReleaseID = p.ReleaseID 
             INNER JOIN @ProjIDs pList ON p.ProjectId = pList.id
            ) AS t1 ON sco.CID = t1.CID    
      LEFT OUTER JOIN    
           (SELECT sc.CID, pm.EndDate, pm.startDate
              FROM StatusCode sc    
             INNER JOIN ProjectMilestone pm ON sc.CID = pm.MilestoneCID 
             INNER JOIN @ProjIDs pList ON pm.ProjectId = pList.id
            ) AS t2 ON sco.CID = t2.CID   
WHERE sco.CID IN (37, 39, 41, 45, 47, 50, 53, 72, 73, 75, 77)    

select ProjectID,ProjectName,SRS_StartDate,[BRS start],[BRS Start] [BRS end],[SRS],[SAD],[Product Profile Review],[SE Integration],[IDE],[UAT],
                                         [PE Testing],[Code Freeze],[Dark Pod],[Production]
 from (
SELECT * FROM  #DATA
PIVOT (MAX(MilestoneEndDate) FOR CID IN ([BRS start],[BRS end],[SRS],[SAD],[Product Profile Review],[SE Integration],[IDE],[UAT],
                                         [PE Testing],[Code Freeze],[Dark Pod],[Production]
)) AS P) t1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文