0

選択に「バケット」を作成しようとしています。基本的に、レコード (この場合はチケット) の経過時間を調べたいと考えています。エイジングは 2 つのフィールドに依存します。必須の日付がある場合はこのフィールドに基づきます。必須の日付がない場合は、チケットが入力された日付に基づきます。私が達成したいのは、120 日以上、90 ~ 120 日、60 ~ 90 日、30 ~ 60 日、15 ~ 30 日、5 ~ 15 日、および 1 ~ 5 日のエージング バケットを持つことです。
次のクエリは正しく機能しません (構文エラー)。私はあまりにも長い間それを見つめてきました。

EDITフォーマット

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')
4

1 に答える 1

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
于 2013-07-10T15:16:50.377 に答える