在另一个字段中创建 Field off Case 语句
Create Field off Case Statement in Another Field
我正在尝试找出一个查询来帮助我根据提取的源代码创建一个 AgeBucket 字段。目前,我创建了一个名为 BusinessAge 的字段,它基本上计算从给定请求的开始日期算起的工作日数,并尝试解释下面的代码。
CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END AS BusinessAge,
根据它 returns 的数字,我想创建一个 AgeBucket 字段...例如...
CASE WHEN BusinessAge >=0 and BusinessAge <6 THEN '0-5'
WHEN BusinessAge >5 and BusinessAge <11 THEN '6-10'
等等,有多个条件。
有没有一种方法可以操纵上述查询并制作更复杂的多案例语句,我可以使用它来创建年龄段字段?或者最好的方法是什么?
试试这样想
SELECT BusinessAge ,
CASE WHEN BusinessAge >= 0 AND BusinessAge < 6 THEN '0-5'
WHEN BusinessAge >= 6 AND BusinessAge <11 THEN '6-10'
END AS AgeBucket,
FROM (
SELECT CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END AS BusinessAge
FROM TableName
)t
已更新:
SELECT CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END AS BusinessAge,
CASE WHEN
CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END BETWEEN 0 AND 6
THEN '0-5'
WHEN
CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END BETWEEN 6 AND 11
THEN '6-11'
END AS AgeBucket
FROM TableName
我正在尝试找出一个查询来帮助我根据提取的源代码创建一个 AgeBucket 字段。目前,我创建了一个名为 BusinessAge 的字段,它基本上计算从给定请求的开始日期算起的工作日数,并尝试解释下面的代码。
CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END AS BusinessAge,
根据它 returns 的数字,我想创建一个 AgeBucket 字段...例如...
CASE WHEN BusinessAge >=0 and BusinessAge <6 THEN '0-5'
WHEN BusinessAge >5 and BusinessAge <11 THEN '6-10'
等等,有多个条件。
有没有一种方法可以操纵上述查询并制作更复杂的多案例语句,我可以使用它来创建年龄段字段?或者最好的方法是什么?
试试这样想
SELECT BusinessAge ,
CASE WHEN BusinessAge >= 0 AND BusinessAge < 6 THEN '0-5'
WHEN BusinessAge >= 6 AND BusinessAge <11 THEN '6-10'
END AS AgeBucket,
FROM (
SELECT CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END AS BusinessAge
FROM TableName
)t
已更新:
SELECT CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END AS BusinessAge,
CASE WHEN
CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END BETWEEN 0 AND 6
THEN '0-5'
WHEN
CASE WHEN [EndDate] > [StartDate] THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] --End Date - Start Date
WHEN [EndDate] IS NULL and [StartDate] IS NOT NULL THEN [BusinessCalendarDayNum] - [BusinessCalendarDayNum] -- GetDate - StartDate
WHEN [EndDate] = [StartDate] THEN 1
END BETWEEN 6 AND 11
THEN '6-11'
END AS AgeBucket
FROM TableName