When和Else中的嵌套Case

时间:2013-07-10 14:59:19

标签: tsql case

我正在尝试在我的选择中创建“Buckets”。基本上我想确定记录的老化(在这种情况下是票证)。老龄化取决于2个领域。如果存在所需日期,则它基于此字段,如果没有要求日期,则基于输入故障单的日期。我想要达到的目标是在120 +,90-120天,60-90天,30-60天,15-30天,5-15天和1-5天内使用老化桶。
以下查询无法正常工作(语法错误)。我现在已经盯着它看了太长时间。

编辑格式化

SELECT     v_rpt_Service.TicketNbr, v_rpt_Service.Location, v_rpt_Service.Board_Name, v_rpt_Service.Status_Description, v_rpt_Service.ClosedDesc, 
                      v_rpt_Service.ServiceType, v_rpt_Service.SubType_RecID, v_rpt_Service.ServiceSubType, 
                      v_rpt_Service.company_name, v_rpt_Service.Summary, v_rpt_Service.team_name, v_rpt_Service.date_entered, v_rpt_Service.entered_by, 
                      v_rpt_Service.Date_Required, v_rpt_Service.hours_budget, v_rpt_Service.Hours_Scheduled, Member.Member_ID, v_rpt_Service.Territory,
                      Case
                      When Date_Required IS Null then round((DATEDIFF(DAY,date_entered,GETDATE())+1)*.69,0) else round((DATEDIFF(Day,date_required,getdate())+1)*.69,0) END as Age,
                      **case when 
                        (Case when Date_Required IS Null
                            then round((DATEDIFF(DAY,date_entered,GETDATE())+1)*.69,0) 
                            else round((DATEDIFF(Day,date_required,getdate())+1)*.69,0) 
                        end )>= 120 
                        then '120+' 
                        else 
                            (Case when Date_Required IS Null
                            then round((DATEDIFF(DAY,date_entered,GETDATE())+1)*.69,0) 
                            else round((DATEDIFF(Day,date_required,getdate())+1)*.69,0) 
                            end) < 120 AND 
                            case when Date_Required IS Null
                            then round((DATEDIFF(DAY,date_entered,GETDATE())+1)*.69,0) 
                            else round((DATEDIFF(Day,date_required,getdate())+1)*.69,0) 
                        end >= 90
                        then '90-120 Days'
                        else 'Newer' 
                        end as Aging**
FROM         Member AS Member INNER JOIN
                      SR_Team AS SR_Team ON Member.Member_RecID = SR_Team.Member_RecID INNER JOIN
                      v_rpt_Service AS v_rpt_Service ON SR_Team.Description = v_rpt_Service.team_name
WHERE     (v_rpt_Service.ClosedDesc = 'Open') AND (v_rpt_Service.Board_Name = 'Solution Design')

1 个答案:

答案 0 :(得分:1)

尝试整理代码,例如

WITH source_date AS (
    SELECT
       v_rpt_Service.TicketNbr,
       v_rpt_Service.Location,
       v_rpt_Service.Board_Name,
       v_rpt_Service.Status_Description,
       v_rpt_Service.ClosedDesc,
       v_rpt_Service.ServiceType,
       v_rpt_Service.SubType_RecID,
       v_rpt_Service.ServiceSubType,
       v_rpt_Service.company_name,
       v_rpt_Service.Summary,
       v_rpt_Service.team_name,
       v_rpt_Service.date_entered,
       v_rpt_Service.entered_by,
       v_rpt_Service.Date_Required,
       v_rpt_Service.hours_budget,
       v_rpt_Service.Hours_Scheduled,
       Member.Member_ID,
       v_rpt_Service.Territory,
       CASE
          WHEN Date_Required IS NULL THEN round((DATEDIFF(DAY,date_entered,GETDATE())+1)*.69,0)
          ELSE round((DATEDIFF(DAY,date_required,getdate())+1)*.69,0)
       END AS Age
    FROM Member AS Member
    INNER JOIN SR_Team AS SR_Team ON Member.Member_RecID = SR_Team.Member_RecID
    INNER JOIN v_rpt_Service AS v_rpt_Service ON SR_Team.Description = v_rpt_Service.team_name
    WHERE (v_rpt_Service.ClosedDesc = 'Open') AND (v_rpt_Service.Board_Name = 'Solution Design')
)
SELECT
    sd.*,
    CASE
       WHEN sd.Age >= 1 AND sd.Age<5 THEN '1-5'
       WHEN sd.Age >= 5 AND sd.Age<15 THEN '5-15'
       WHEN sd.Age >= 15 AND sd.Age<30 THEN '15-30'
       WHEN sd.Age >= 30 AND sd.Age<60 THEN '30-60'
       WHEN sd.Age >= 60 AND sd.Age<90 THEN '60-90'
       WHEN sd.Age >= 90 AND sd.Age<120 THEN '90-120'
       WHEN sd.Age >= 120 THEN '120+'
       ELSE 'Unknown'
    END AS Aging
FROM source_date AS sd