从以前的SQL Query语句中为CAS查询生成CASE语句

时间:2016-12-06 18:12:03

标签: sql sql-server-2012 field case case-when

我目前正在使用SQL Server 2012并尝试弄清楚如何从CASE语句中获取结果。

所以目前我有一个CASE语句来计算我目前用来查找请求的业务年龄的AgeBusiness字段。这是我的代码:

CASE    WHEN A.[End_Date] > A.[Start_Date] THEN C2.[BUS_DY_OF_CAL_NUM] - C1.[BUS_DY_OF_CAL_NUM] 
        WHEN A.[End_Date] IS NULL and A.[Start_Date] IS NOT NULL THEN C3.[BUS_DY_OF_CAL_NUM] - C1.[BUS_DY_OF_CAL_NUM]
        WHEN A.[End_Date] = A.[Start_Date] THEN 1
END AS AgeBusiness

基本上,如果结束日期大于开始日期,请使用结束日期 - 开始日期进行计算。 如果没有结束日期,请使用GETDATE() - 开始日期进行计算。 如果他们是平等的,则为1个工作日。

无论如何,我有这个Ag​​eBusiness字段,并试图使用此字段或此字段中的数据来计算新的Case When语句来计算需求。

在一个完美的世界里,我的CASE陈述会是这样的:

CASE WHEN AgeBusiness >= 0 AND AgeBusiness <11 AND Name = "RequirementA" Then 1 
     WHEN AgeBusiness >= 0 AND AgeBusiness <8 AND Name = "RequirementB" Then 1  
     WHEN AgeBusiness >= 0 AND AgeBusiness <181 AND Name = "RequirementC" Then 1
     WHEN AgeBusiness >= 0 AND AgeBusiness <8 AND Name = "RequirementD" Then 1
     WHEN AgeBusiness >= 0 AND AgeBusiness <11 AND Name = "RequirementE" Then 1
     WHEN AgeBusiness >= 0 AND AgeBusiness <16 AND Name = "RequirementF" Then 1
End AS WithinRequirements

基本上我想创建一个名为Within Requirements的新案例声明,如果它在一定的天数内并且有一个特定的名称,那么它将是,或者在这种情况下1.名称字段已存在于表中我可以参与其中。虽然制作InsideRequirements的要求之一要求AgeBusiness存在,但同时在查询中创建字段......所以这是不可能的。

有没有办法使用上面提到的AgeBusiness中的信息以某种方式创建WithinRequirements?目标是在查询中解决这个问题......即使这意味着冗长的查询。

2 个答案:

答案 0 :(得分:1)

对中间变量使用CROSS APPLY

SELECT 
CASE WHEN AgeBusiness >= 0 AND AgeBusiness <11 AND Name = "RequirementA" Then 1 
     WHEN AgeBusiness >= 0 AND AgeBusiness <8 AND Name = "RequirementB" Then 1  
     WHEN AgeBusiness >= 0 AND AgeBusiness <181 AND Name = "RequirementC" Then 1
     WHEN AgeBusiness >= 0 AND AgeBusiness <8 AND Name = "RequirementD" Then 1
     WHEN AgeBusiness >= 0 AND AgeBusiness <11 AND Name = "RequirementE" Then 1
     WHEN AgeBusiness >= 0 AND AgeBusiness <16 AND Name = "RequirementF" Then 1
End AS WithinRequirements
FROM  A 
CROSS APPLY (
   SELECT CASE WHEN A.[End_Date] > A.[Start_Date] THEN C2.[BUS_DY_OF_CAL_NUM] - C1.[BUS_DY_OF_CAL_NUM] 
           WHEN A.[End_Date] IS NULL and A.[Start_Date] IS NOT NULL THEN C3.[BUS_DY_OF_CAL_NUM] - C1.[BUS_DY_OF_CAL_NUM]
           WHEN A.[End_Date] = A.[Start_Date] THEN 1
        END AS AgeBusiness
) vars

答案 1 :(得分:0)

如果A. [End_Date]为空时,C2。[BUS_DY_OF_CAL_NUM]也为空,你可以像这样重写第一个case语句:

 ABS(COALESCE(COALESCE(C2.[BUS_DY_OF_CAL_NUM],C3.[BUS_DY_OF_CAL_NUM])-C1.[BUS_DY_OF_CAL_NUM],1))

您还在案例陈述中使用BETWEEN并对单个值进行操作,因此重写第二个值,如下所示:

CASE ABS(COALESCE(COALESCE(C2.[BUS_DY_OF_CAL_NUM],C3.[BUS_DY_OF_CAL_NUM])-C1.[BUS_DY_OF_CAL_NUM],1))
     WHEN AgeBusiness >= 0 AND AgeBusiness <11 AND Name = "RequirementA" Then 1 
     WHEN BETWEEN 0 AND 11  AND Name = "RequirementB" Then 1  
     WHEN BETWEEN 0 AND 181 AND Name = "RequirementC" Then 1
     WHEN BETWEEN 0 AND 8   AND Name = "RequirementD" Then 1
     WHEN BETWEEN 0 AND 11  AND Name = "RequirementE" Then 1
     WHEN BETWEEN 0 AND 16  AND Name = "RequirementF" Then 1
End AS WithinRequirements

我相信这个查询不像你原来那么“冗长”。