SQL Server:创建派生列

时间:2018-02-09 16:18:14

标签: sql-server derived

以下是我工作的表格的示例数据。我希望创建一个新列RenIndicator,其中包含3个不同的值,应根据NextPolicy列和Expiry date列进行填充。

任何想法都会很好。包括样本数据和我期望的结果。

新列RenIndicator,它有三个可能的值之一:YesNoN/a yet

- 当NextPolicy有一个policynumber& Expiry date是当前日期之前的

- 当NextPolicy列没有任何值时& Expiry date小于当前日期

N / A - NextPolicy列没有任何价值& Expiry date大于当前日期

我正在寻找更像这样的查询

                select a.*, 
                        case when a.NextPolicy <> NULL and NewRenewal = 'New' then 'Renewal' ELSE
                         when a.NextPolicy = NULL and ExpiryDate > GETDATE() then 'Not Availble Yet' ELSE
                        when a.NextPolicy = NULL and ExpiryDate < GETDATE() then 'Not Availble Yet' 
                        end as 'Renewal Indicator'  
                        from final a 

示例数据:

--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable') IS NOT NULL
    DROP TABLE #mytable

--===== Create the test table with 
CREATE TABLE #mytable 
(
    Region nvarchar(300), 
    NextPolicy nvarchar(300),
    CurrentPolicyNumber nvarchar(300),
    PolicyNumber nvarchar(100), 
    NewRenewal nvarchar(100), 
    EffectiveDate Date, 
    ExpiryDate Date, 
    Coverage nvarchar(100), 
    InsuredName nvarchar(300)
)

SET DATEFORMAT DMY

--===== Insert the test data into the test table
INSERT INTO #mytable (Region, NextPolicy, CurrentPolicyNumber, PolicyNumber, NewRenewal, EffectiveDate, ExpiryDate, Coverage, InsuredName)
    SELECT 'Asia', '47-ACA-000001-02', '47-ACA-000001-01', '000001', 'New', '2016-12-25', '2017-12-25', '', 'CPC Corporation, Taiwan (CPC)' 
    UNION ALL
    SELECT 'Asia', '', '47-ACA-000001-02', '000001', 'Renewal', '2017-12-25', '2018-12-25', '', 'CPC Corporation, Taiwan (CPC)' 
    UNION ALL
    SELECT 'North America', '42-XPR-000001-02', '42-PRP-000001-01', '000001', 'New', '2013-05-15', '2014-05-15', 'PRP', 'AvalonBay Communities,Inc.' 
    UNION ALL
    SELECT 'North America', '42-XPR-000001-03', '42-XPR-000001-02', '000001', 'Renewal', '2014-05-15', '2015-05-15', 'XPR', 'AvalonBay Communities, Inc.' 
    UNION ALL
    SELECT 'North America', '42-XPR-000001-04', '42-XPR-000001-03', '000001', 'Renewal', '2015-05-15', '2016-05-15', 'XPR', 'AvalonBay Communities, Inc.' 
    UNION ALL
    SELECT 'North America', '', '42-XPR-000001-04', '000001', 'Renewal', '2016-05-15', '2017-05-15', 'XPR', 'AvalonBay Communities, Inc.' 
    UNION ALL
                     SELECT 'Asia',         '47-ABA-000001-02','47-ABA-000001-01','000001','New',       '2015-11-25',   '2016-11-25','','Taiwan' UNION ALL
                     SELECT 'Asia',         '','47-ABA-000001-02','000001','Renewal','2016-11-25','2017-11-25','','Taiwan'

2 个答案:

答案 0 :(得分:1)

这可以使用案例陈述来完成:

SELECT *
     , CASE WHEN NextPolicy <> '' AND ExpiryDate < CAST(GETDATE() AS DATE) THEN 'Yes'
            WHEN NextPolicy = '' AND ExpiryDate < CAST(GETDATE() AS DATE) THEN 'No'
            WHEN NextPolicy = '' AND ExpiryDate >  CAST(GETDATE() AS DATE) THEN 'N/a yet'
       END AS RenIndicator
FROM #mytable

我已将空字符串''用于&#34;没有任何值&#34;要求,但如果您在这些字段中有NULLS,则需要在CASE中执行ISNULL(NextPolicy, '')之类的操作来解释它们。

我们假设任何值都是有效的策略编号。可根据需要包括其他数据验证。

即将开始编辑,等待有关ExpiryDate为今天的日期后会发生什么的更多信息。

答案 1 :(得分:1)

...可能

ALTER TABLE #mytable ADD RenIndicator AS CASE WHEN NULLIF(NextPolicy,'') IS NOT NULL AND ExpiryDate < GETDATE() THEN 'Yes'
                                              WHEN NULLIF(NextPolicy,'') IS NULL AND ExpiryDate < GETDATE() THEN 'No'
                                              WHEN NULLIF(NextPolicy,'') IS NULL THEN 'N/A yet'
                                         END;

但如果NULL确实有值且续订日期是将来,则会返回 NextPolicy

我注意到您的示例数据中包含'',而不是 NULL 以显示缺少数据,因此我使用了NULLIF但是,为了以防万一,您可以将其更改为= ''<> ''或删除NULLIF

我还假设您的陈述&#34;我希望创建一个新列RenIndicator&#34;你实际上是指创建一个新列(改为表&#39; DDL)。