简体   繁体   English

根据SQL查询的前一个CASE语句制作CASE语句

[英]Making CASE statement from previous CASE statement for SQL Query

I currently am working with SQL Server 2012 and trying to figure out how to get results from a CASE statement. 我目前正在使用SQL Server 2012,并试图弄清楚如何从CASE语句中获取结果。

So currently I have a CASE statement to calculate an AgeBusiness field that I currently am using to find the Business Age of requests. 因此,目前我有一个CASE语句来计算一个AgeBusiness字段,该字段当前用于查找请求的业务年龄。 This is the code I have for it: 这是我的代码:

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

Basically, if End Date is greater than Start Date, use End Date - Start Date to calculate. 基本上,如果“结束日期”大于“开始日期”,请使用“结束日期-开始日期”进行计算。 If no End Date, use GETDATE() - Start Date to calculate. 如果没有结束日期,请使用GETDATE()-开始日期进行计算。 If they're equal, then 1 business day. 如果它们相等,则为1个工作日。

Anyways, I have this AgeBusiness field and am trying to either use this field or data from this field to calculate a new Case When statement to calculate requirements. 无论如何,我有这个Ag​​eBusiness字段,并且正在尝试使用该字段或该字段中的数据来计算新的Case While语句来计算需求。

In a perfect world my CASE statement would go something like this: 在理想的情况下,我的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

Basically I want to make a new case statement called Within Requirements where if it fell within a certain amount of days and had a particular name, then it would be yes or in this case 1. The name field already exists from a table that I can pull in. Though one of the requirements for making WithinRequirements requires the AgeBusiness to exist, which the fields would be created in query at same time...so that's not possible. 基本上,我想做一个新的case语句,称为Requirements内,如果它在一定天数之内并且有一个特定的名称,那么它将是yes或在这种情况下1。name字段已经存在于一个表中,我可以拉入。尽管进行InsideRequirements的要求之一要求存在AgeBusiness,但将在查询中同时创建字段,所以这是不可能的。

Is there a way to use the info from AgeBusiness that I put above to create WithinRequirements somehow? 有没有办法使用我上面提到的AgeBusiness提供的信息以某种方式创建InsideRequirements? The goal is to figure this out in a query...even if it means a long winded query. 目标是在查询中解决这个问题,即使这意味着冗长的查询。

Use CROSS APPLY for intermediate variables 使用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

If when A.[End_Date] is null will C2.[BUS_DY_OF_CAL_NUM] also be null you can rewrite the first case statement like this: 如果当A. [End_Date]为null时,C2。[BUS_DY_OF_CAL_NUM]也为null,则可以这样重写第一个case语句:

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

You also use BETWEEN in a case statement and operate on a single value so rewrite the 2nd one like this: 您还可以在case语句中使用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

I believe this query is less "long winded" than your original. 我相信这个查询比您原来的查询“少了很多”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM