基于与先前行sql的比较的条件计数

时间:2015-06-29 16:22:53

标签: sql-server sql-server-2008 date

让我们从我正在使用的数据样本开始:

    Policy No | start date
    1         | 2/15/2006
    1         | 2/15/2009
    1         | 2/15/2012         
    2         | 3/15/2006
    3         | 3/19/2006
    3         | 3/19/2012
    4         | 3/31/2006
    4         | 3/31/2009

我正在尝试在SQL Server 2008中编写代码中的代码。原则是投保人最早的开始日期是政策开始的时间。每三年向客户提供一次增加。如果他们同意增加,则开始日期将刷新,与三年后的原始日期相同。如果它们拒绝,则根本不会向数据库添加任何内容。

我试图不仅计算客户接受报价的次数(或将开始日期增加三年),而是将其与第一次报价或第二次报价分开。从最初的开始日期开始,然后将现在和之间的天数除以1095得到我的报价总数,所以我已经到了那么远。我真正希望它做的是将每个政策编号与之前的政策编号进行比较,以查看它是否相同(它已经按政策编号排序),然后在新的“已接受”列中计算日期更改并计算它没有的时间不会改变但可能会“拒绝”。

这是一个我需要自己加入表格来比较日期的情况吗?或者有更简单的方法吗?

2 个答案:

答案 0 :(得分:1)

你正在寻找这个: -

Set Nocount On;

Declare @Test Table
(
     PolicyNo       Int
    ,StartDate      Date
)

Declare @PolicyWithInc Table
(
     RowId          Int Identity(1,1) Primary Key
    ,PolicyNo       Int
    ,StartDate      Date
)

Insert Into @Test(PolicyNo,StartDate) Values
 (1,'2/15/2006')
,(1,'2/15/2009')
,(1,'2/15/2012')         
,(2,'3/15/2006')
,(3,'3/19/2006')
,(3,'3/19/2012')
,(4,'3/31/2006')
,(4,'3/31/2009')

Insert Into @PolicyWithInc(PolicyNo,StartDate)
Select   t.PolicyNo
        ,t.StartDate
From    @Test As t

Select   pw.PolicyNo
        ,Sum(Case When Datediff(Year,t.StartDate, pw.StartDate) = 3 Then 1 Else 0 End) As DateArrived
        ,Sum(Case When Datediff(Year,t.StartDate, pw.StartDate) > 3 Then 1 Else 0 End) As DateNotArrived
        ,Sum(Case When Isnull(Datediff(Year,t.StartDate,pw.StartDate),0) = 3 Then 1 Else 0 End) As Years3IncrementCount
From    @PolicyWithInc As pw
        Left Join @PolicyWithInc As t On pw.PolicyNo = t.PolicyNo And pw.RowId = (t.RowId + 1)
Group By pw.PolicyNo

答案 1 :(得分:1)

下面可能会有所帮助:

Set Nocount On;

Declare @Test Table
(
     PolicyNo       Int
    ,StartDate      Date
)

Insert Into @Test(PolicyNo,StartDate) Values
 (1,'2/15/2006')
,(1,'2/15/2009')
,(1,'2/15/2012')         
,(2,'3/15/2006')
,(3,'3/19/2006')
,(3,'3/19/2012')
,(4,'3/31/2006')
,(4,'3/31/2009')

select PolicyNo, StartDate, dateadd(yy, 3, StartDate)Offer1, dateadd(yy, 6, StartDate)Offer2, dateadd(yy, 9, StartDate)Offer3  from 
(select * , row_number() over (partition by PolicyNo order by StartDate) rn from @Test)A
where rn = 1

select 
    count(*) * 3 TotalOffersMade,
    count(Data1.StartDate) FirstOfferAccepted, 
    count(Data2.StartDate) SecondOfferAccepted, 
    count(Data3.StartDate) ThirdOfferAccepted,
    count(*) - count(Data1.StartDate) FirstOfferDeclined,
    count(*) - count(Data2.StartDate) SecondOfferDeclined,
    count(*) - count(Data3.StartDate) ThirdOfferDeclined
from 
    (
    select PolicyNo, StartDate, dateadd(yy, 3, StartDate)Offer1, dateadd(yy, 6, StartDate)Offer2, dateadd(yy, 9, StartDate)Offer3  from 
    (select * , row_number() over (partition by PolicyNo order by StartDate) rn from @Test)A
    where rn = 1
    )Offers 
LEFT JOIN
    @Test Data1 
    on Offers.PolicyNo = Data1.PolicyNo and Offers.Offer1 = Data1.StartDate
LEFT JOIN
    @Test Data2
    on Offers.PolicyNo = Data2.PolicyNo and Offers.Offer2 = Data2.StartDate
LEFT JOIN
    @Test Data3
    on Offers.PolicyNo = Data3.PolicyNo and Offers.Offer3 = Data3.StartDate