使用case语句分配列名

时间:2012-05-28 12:20:20

标签: sql case

我正在尝试将其重新编写为案例陈述:

 SELECT dateadd(day, -@Day, getdate()) AS Dt
    ,COUNT(pkey) AS 'BlockerOut'
  FROM tablename
  WHERE PROJECT='10270' AND RESOLUTION='1'
        AND CREATED>dateadd(day, -(30+@Day), getdate()) AND CREATED<dateadd(day, -@Day, getdate())
        AND PRIORITY='1' AND (DATEDIFF(hour, [CREATED], [RESOLUTIONDATE]))>4) t1
JOIN
(SELECT dateadd(day, -@Day, getdate()) AS Dt
    ,COUNT(pkey) AS 'BlockerIn'
FROM jobissue
WHERE PROJECT='10270' AND RESOLUTION='1'
        AND CREATED>dateadd(day, -(30+@Day), getdate()) AND CREATED<dateadd(day, -@Day, getdate())
        AND PRIORITY='1' AND (DATEDIFF(hour, [CREATED], [RESOLUTIONDATE]))<=4) t2
        ON t1.Dt = t2.Dt

我已经做到这一点,无法解决如何比列

更进一步
SELECT dateadd(day, -@Day, getdate()) AS Dt, 
CASE  
WHEN PRIORITY='1' AND (DATEDIFF(hour, [CREATED], [RESOLUTIONDATE]))<=4 then  (pkey)   end  'BlockerIn'
FROM [jobissue] 
WHERE PROJECT='10270' AND RESOLUTION='1'
AND CREATED>dateadd(day, -(30+@Day), getdate()) AND CREATED<dateadd(day, -@Day, getdate())
group by PRIORITY, CREATED, resolutiondate, pkey

这可以..但是对于两列我得到一个错误:

SELECT dateadd(day, -@Day, getdate()) AS Dt, 
CASE  
WHEN PRIORITY='1' AND (DATEDIFF(hour, [CREATED], [RESOLUTIONDATE]))<=4 then  (pkey)   'BlockerIn'
WHEN PRIORITY='2' AND (DATEDIFF(hour, [CREATED], [RESOLUTIONDATE]))>24 then  (pkey)   'CriticalOut'
end
FROM jobissue
WHERE PROJECT='10270' AND RESOLUTION='1'
AND CREATED>dateadd(day, -(30+@Day), getdate()) AND CREATED<dateadd(day, -@Day, getdate())
group by PRIORITY, CREATED, resolutiondate, pkey

Msg 102,Level 15,State 1,Line 18 'BlockerIn'附近的语法不正确。

我无法计算出多个列名的语法..请帮帮我?感谢。

1 个答案:

答案 0 :(得分:1)

这就是我的意思

CASE WHEN PRIORITY='1' then  (ID)   END as 'BlockerIn',
CASE WHEN PRIORITY='2' then  (ID)  END  as 'CriticalOut'

这将创建两列,其值为。

如果您想要一个具有多个值的列,您可以

CASE WHEN PRIORITY='1' then  'BlockerIn' 
WHEN PRIORITY='2' then  'CriticalOut' END  AS Priority