0

I'm working on a query:

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

If Name and Dexcription are NULL, the cell is populated with 'KanBan ' ISNULL(P.[Name]+' - '+P.[Description], 'KanBan')

This is working how I want it to. But, If a 3rd cell is NULL (T.[Name]) I would also like this cell is NULL.

So if P.[Name]+' - '+P.[Description] (Sprint) IS NULL then populate with KanBan, UNLESS T.[Name] IS NULL in which case I would also like (Sprint) to be NULL

How can I achieve this?

1 Answer 1

3

This is a good reason to use 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
Sign up to request clarification or add additional context in comments.

4 Comments

perhaps: select (case when T.Name is null then NULL else coalesce(P.[Name]+' - '+P.[Description], 'KanBan') end) AS Sprint
why the brackets around the case? Before you say "clarity" - it isn't more clear. It's more cluttered.
@Bohemian . . . Not to my eye. I use parentheses to see where statements end. Nested case statements are a particular pain.
Isn't that why god invented the end keyword? When you have syntactic delimiters case ... end brackets are visually and logically redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.