ORDER BY底部的某个字符串,然后按不同的列按字母顺序排列

时间:2013-09-17 14:36:33

标签: sql sql-server

我正在尝试订购查询,但未获得所需的结果:

SELECT R.[Name] AS Project, ISNULL(P.[Name]+' - '+P.[Description], 'KanBan') AS Sprint, S.[Number] 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
FROM DailyTaskHours D
INNER JOIN Task T ON D.TaskId = T.PK_Task 
INNER JOIN Story S ON T.StoryId = S.PK_Story 
LEFT JOIN Sprint P ON S.SprintId = P.PK_Sprint 
INNER JOIN Product R ON S.ProductId = R.PK_Product
GROUP BY R.[Name], P.[Name], P.[Description], S.[Number], T.[Name]
ORDER BY CASE WHEN (P.[Name]+' - '+P.[Description]) = 'KanBan' then 1 else 0 end, Project ASC, Story ASC, Task ASC

首先,我想在哪里P. [名称] +' - '+ P。[描述]是NULL并且用'KanBan'填充到底部之后比我想要的按产品按字母顺序排列,然后冲刺产品,然后故事然后任务。

我怎样才能完成这个?

编辑:谢谢大家这里是最后的查询:

SELECT R.[Name] AS Project, ISNULL(P.[Name]+' - '+P.[Description], 'KanBan') AS Sprint, S.[Number] 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
FROM DailyTaskHours D
INNER JOIN Task T ON D.TaskId = T.PK_Task 
INNER JOIN Story S ON T.StoryId = S.PK_Story 
LEFT JOIN Sprint P ON S.SprintId = P.PK_Sprint 
INNER JOIN Product R ON S.ProductId = R.PK_Product
GROUP BY R.[Name], P.[Name], P.[Description], S.[Number], 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

2 个答案:

答案 0 :(得分:2)

只需稍微修改一下:

ORDER BY CASE WHEN ISNULL(P.[Name]+' - '+P.[Description], 'KanBan') = 'KanBan'
    THEN 0
    ELSE 1 END

问题是您的KanBan条款中永远不会得到ORDER BY

答案 1 :(得分:1)

这看起来有点令人费解,但我认为这是最具说明性的方法:

; WITH cte AS (
  <put your query here>
)
SELECT list
     , of
     , columns
     , including
     , the
     , kanban
     , one
FROM   cte
ORDER
    BY CASE WHEN sprint = 'KanBan' THEN 937 ELSE -937 END ASC
     , Product
     , Sprint
     , story
     , task