在DATEDIFF之后使用CASE逻辑

时间:2018-05-18 18:06:10

标签: sql sql-server tsql case datediff

我成功使用DATEDIFF逻辑来计算年龄。我需要能够使用Age并将其用作CASE逻辑中的列。当我尝试这样做时,SMSS将Age标记为红色并表示它是无效列。我该如何解决这个问题?

USE DataWarehouse
SELECT DISTINCT
M.[Account Name Birth Date] as 'Birth Date',
M.[Mailing Address State] as 'State',
E.[ANSVIES2] as 'Income',
E.[FICO] as 'FICO Score',

DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 as [Age],

CASE WHEN [Age] >= 18 AND [Age] <= 34 AND E.[ANSVIES2] >= 50 THEN 'Credit 
Driven'
WHEN [Age] < 45 AND E.[ANSVIES2] < 50 THEN 'Fee Driven'
WHEN [Age] >= 35 AND [Age] <= 54 AND E.[ANSVIES2] >= 50 AND E.[ANSVIES2] <= 
125 THEN 'Middle Market'
WHEN [Age] >= 45 AND E.[ANSVIES2] < 50 THEN 'Low Income Depositor'
WHEN [Age] >= 55 AND E.[ANSVIES2] >= 50 AND E.[ANSVIES2] <= 125 THEN 'Middle 
Income Depositor'
WHEN [Age] >= 35 AND E.[ANSVIES2] >= 125 THEN 'Upscale'
ELSE 'Unclassified' END as 'Consumer Segment'

4 个答案:

答案 0 :(得分:1)

或者使用子选择(不是最好的方式,具体取决于你的数据库大小和索引,临时表或临时表可能会更好,这取决于你的位置,但这应该工作):

   Select CASE WHEN [Age] >= 18 AND [Age] <= 34 AND E.[ANSVIES2] >= 50 THEN 'Credit 
Driven'
WHEN [Age] < 45 AND E.[ANSVIES2] < 50 THEN 'Fee Driven'
WHEN [Age] >= 35 AND [Age] <= 54 AND E.[ANSVIES2] >= 50 AND E.[ANSVIES2] <= 
125 THEN 'Middle Market'
WHEN [Age] >= 45 AND E.[ANSVIES2] < 50 THEN 'Low Income Depositor'
WHEN [Age] >= 55 AND E.[ANSVIES2] >= 50 AND E.[ANSVIES2] <= 125 THEN 'Middle 
Income Depositor'
WHEN [Age] >= 35 AND E.[ANSVIES2] >= 125 THEN 'Upscale'
ELSE 'Unclassified' END as 'Consumer Segment'
, OtherColumnsHere
FROM (
     SELECT DISTINCT
     M.[Account Name Birth Date] as 'Birth Date',
     M.[Mailing Address State] as 'State',
     E.[ANSVIES2] as 'Income',
     E.[FICO] as 'FICO Score',    
     DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 as [Age],
     FROM RestOfYourSelectStatementHere
) SubSelect

答案 1 :(得分:1)

SSMS 突出您的语法的原因是logical-processing order of SQL

简单地说,选择阶段中使用的别名“年龄”不能在同一阶段引用 - 它可以在后续阶段引用 - (例如下一阶段 - ORDER BY )。

因此,工作方法是创建 CTE 嵌套表

  

查询的逻辑步骤序列:

  1. FROM / JOIN / APPLY / PIVOT / UNPIVOT
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT list / DISTINCT
  6. ORDER BY
  7. TOP / OFFSET-FETCH

答案 2 :(得分:0)

您不需要使用subquery,您可以使用value构造直接表达:

 SELECT  . . . ,
        (CASE WHEN tt.Age >= 18 AND tt.Age <= 34 AND t.[ANSVIES2] >= 50 
              THEN 'Credit Driven'
              WHEN [Age] < 45 AND E.[ANSVIES2] < 50 THEN 'Fee Driven'
              WHEN [Age] >= 35 AND [Age] <= 54 AND 
                   E.[ANSVIES2] >= 50 AND E.[ANSVIES2] <= 125 
              THEN 'Middle Market'
              WHEN [Age] >= 45 AND E.[ANSVIES2] < 50 
              THEN 'Low Income Depositor'
              WHEN [Age] >= 55 AND E.[ANSVIES2] >= 50 AND E.[ANSVIES2] <= 125 
              THEN 'Middle Income Depositor'
              WHEN [Age] >= 35 AND E.[ANSVIES2] >= 125 THEN 'Upscale'
              ELSE 'Unclassified'
         END) AS [Consumer Segment]
FROM table t 
CROSS APPLY (VALUES 
                (DATEDIFF(DD, t.[Account Name Birth Date],GETDATE())/365)
            ) tt(Age); 

答案 3 :(得分:0)

上面发布的代码因某些原因无效。我能够成功地实现这个解决方案 -

USE DataWarehouse
SELECT DISTINCT
M.[Symitar Account Id] as 'Symitar ID',
M.[Account Name Birth Date] as 'Birth Date',
M.[Account Name Death Date] as 'Death Date',
M.[Mailing Address State] as 'State',
E.[ANSVIES2] as 'Income',
E.[FICO] as 'FICO Score',
M.[Loan Type] as 'Loan Description',
M.[Loan Type Code] as 'Loan Type',
M.[Share Type] as 'Share Description',
M.[Share Type Code] as 'Share Type',

DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 as [Age],

CASE WHEN DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 >= 18 AND 
DATEDIFF(DD,M.[Account Name Birth Date],
GETDATE())/365 <= 34 AND E.[ANSVIES2] >= 50 THEN 'Credit Driven'
WHEN DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 < 45 AND E. 
[ANSVIES2] < 50 THEN 'Fee Driven'
WHEN DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 >= 35 AND 
DATEDIFF(DD,M.[Account Name Birth Date],
GETDATE())/365 <= 54 AND E.[ANSVIES2] >= 50 AND E.[ANSVIES2] <= 125 THEN 
'Middle Market'
WHEN DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 >= 45 AND E. 
[ANSVIES2] < 50 THEN 'Low Income Depositor'
WHEN DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 >= 55 AND E. 
[ANSVIES2] >= 50 AND E.[ANSVIES2] <= 125 
THEN 'Middle Income Depositor'
WHEN DATEDIFF(DD,M.[Account Name Birth Date],GETDATE())/365 >= 35 AND E. 
[ANSVIES2] >= 125 THEN 'Upscale'
ELSE 'Unclassified' END as 'Consumer Segment'

FROM Accounts.ExperianData E
INNER JOIN Accounts.MemberCube M
ON E.[Symitar Account Id] = M.[Symitar Account Id]
WHERE M.[Account Name Type] = 'Primary' AND /* Primary acct holders */
M.[Account Status] = 'Open' AND 
M.[Account Type] IN ('General Membership', 'Indirect Account', 'Employee Acc 
ount') AND 
/* Remove businesses and internal accounts */
M.[Account Name Birth Date] <= '05/18/1997' /* Older than 21 */ AND
M.[Mailing Address State] IN('UT', 'ID', 'AZ', 'NV', 'NM')