如果另一个单元格不为NULL,则用字符串替换NULL

时间:2013-09-18 14:36:15

标签: sql sql-server

我正在处理查询:

SELECT D.PointPerson AS Person, R.[Name] AS Project, ISNULL(P.[Name]+' - '+P.[Description], 'KanBan') AS Sprint, COALESCE(S.[Number], N.IncidentNumber) AS Story, T.[Name] AS Task,
   sum(CASE WHEN DatePart(dw, D.ActivityDate) = 1 THEN D.[Hours] ELSE 0 END) AS Monday,
   sum(CASE WHEN DatePart(dw, D.ActivityDate) = 2 THEN D.[Hours] ELSE 0 END) AS Tuesday,
   sum(CASE WHEN DatePart(dw, D.ActivityDate) = 3 THEN D.[Hours] ELSE 0 END) AS Wednesday,
   sum(CASE WHEN DatePart(dw, D.ActivityDate) = 4 THEN D.[Hours] ELSE 0 END) AS Thursday,
   sum(CASE WHEN DatePart(dw, D.ActivityDate) = 5 THEN D.[Hours] ELSE 0 END) AS Friday,
   sum(CASE WHEN DatePart(dw, D.ActivityDate) = 6 THEN D.[Hours] ELSE 0 END) AS Saturday,
   sum(CASE WHEN DatePart(dw, D.ActivityDate) = 7 THEN D.[Hours] ELSE 0 END) AS Sunday,
   sum(D.[Hours]) AS Total
FROM DailyTaskHours D
LEFT JOIN Task T ON D.TaskId = T.PK_Task 
LEFT JOIN Story S ON T.StoryId = S.PK_Story 
LEFT JOIN NonScrumStory N ON D.NonScrumStoryId = N.PK_NonScrumStory 
LEFT JOIN Sprint P ON S.SprintId = P.PK_Sprint 
LEFT JOIN Product R ON S.ProductId = R.PK_Product
GROUP BY D.PointPerson, R.[Name], P.[Name], P.[Description], S.[Number], N.IncidentNumber, T.[Name]
ORDER BY CASE WHEN ISNULL(P.[Name]+' - '+P.[Description], 'KanBan') = 'KanBan'
    THEN 1
    ELSE 0 END,
    Project ASC,
    Sprint ASC,
    Story ASC,
    Task ASC

如果Name和Dexcription为NULL,则使用'KanBan填充单元格 “     ISNULL(P. [姓名] +' - '+ P. [描述],'看看')

这是我想要的方式。但是,如果第三个单元格为NULL(T. [Name]),我也希望这个单元格为NULL。

所以如果P. [Name] +' - '+ P。[Description](Sprint)IS NULL则填充KanBan,UNLESS T. [Name] IS NULL在这种情况下我也想(Sprint)是NULL

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

这是使用case

的一个很好的理由
select (case when T.Name is null then NULL
             when P.[Name]+' - '+P.[Description] is NULL then 'KanBan'
             else P.[Name]+' - '+P.[Description]
        end) AS Sprint