根据Key的付费日期比较(大于或小于)第1行值和第2行值,然后检查第3行是否大于2nd

时间:2017-06-06 02:46:21

标签: sql-server sql-server-2016

当前结果

- 场景1 - 对于ID 1和2,我想比较Diff Between(DD,ID 2的开始日期,ID 1的结束日期) < = 1,那么该值应为true,并显示其他明智的值,不显示值

场景2 对于ID 10和11 - ID 10的开始日期和ID 11的开始日期相同,所以我想根据付费日期进行比较,As ID 11的付费日期是> ID 10的付费日期。我希望显示的值更大

Id  Employeekey ReceiptNo   BeginDate   endDate     PaidDate    main    Supplier    RollNo
1   101         5505        3/28/2016   3/29/2016   4/29/2016   1       2001    655
2   101         5506        3/30/2016   4/1/2016    4/30/2016   1       2001    666
3   101         5507        4/5/2016    4/6/2016    4/30/2016   1       2001    155
4   101         5508        4/7/2016    4/10/2016   5/1/2016    1       2001    155
5   101         5509        4/11/2016   4/14/2016   5/5/2016    1       2001    155
6   101         5510        5/1/2016    5/3/2016    6/24/2016   1       2001    255
7   101         5511        5/1/2016    5/3/2016    6/30/2016   1       2001    265
8   102         5512        3/28/2017   3/29/2016   4/29/2017   1       2001    655
9   102         5513        3/28/2017   3/29/2016   4/29/2017   1       2001    655
10  102         5514        3/28/2017   3/29/2016   4/29/2017   1       2001    655
11  102         5515        3/28/2016   3/29/2016   5/29/2016   1       2001    655
12  102         5515        3/28/2016   3/29/2016   5/29/2016   1       2001    659

1 个答案:

答案 0 :(得分:1)

查看第二个方案的日期。对于ID 10和ID 11的BeginDate,日期不相同。此外,PaidDate不是>,它是< (看看两种情况下的年份)。话虽如此,我编辑了您的测试数据。这是你如何做到这一点。您可以根据自己的需要进行更改,但这样可以帮助您。

declare @table table (id int, 
                     Employeekey int, 
                     RecieptNo int, 
                     BeginDate datetime, 
                     endDate datetime, 
                     PaidDate datetime, 
                     main int, 
                     Supplier int, 
                     RollNo int)
 insert into @table 
 values
(1,101,5505,'3/28/2016','3/29/2016','4/29/2016',1,2001,655),
(2,101,5506,'3/30/2016','4/1/2016','4/30/2016',1,2001,666),
(3,101,5507,'4/5/2016','4/6/2016','4/30/2016',1,2001,155),
(4,101,5508,'4/7/2016','4/10/2016','5/1/2016',1,2001,155),
(5,101,5509,'4/11/2016','4/14/2016','5/5/2016',1,2001,155),
(6,101,5510,'5/1/2016','5/3/2016','6/24/2016',1,2001,255),
(7,101,5511,'5/1/2016','5/3/2016','6/30/2016',1,2001,265),
(8,102,5512,'3/28/2017','3/29/2016','4/29/2017',1,2001,655),
(9,102,5513,'3/28/2017','3/29/2016','4/29/2017',1,2001,655),
(10,102,5514,'3/28/2016','3/29/2016','4/29/2016',1,2001,655),   --changed this to 2016 for being date and paid date
(11,102,5515,'3/28/2016','3/29/2016','5/29/2017',1,2001,655),   --changed this to 2017 for paid date
(12,102,5515,'3/28/2016','3/29/2016','5/29/2016',1,2001,659)



select
    *
    --scenario 1
    ,case 
        when datediff(day,lead(BeginDate) over (partition by EmployeeKey order by Id),endDate) <=1 then 'True' 
    end
    --scenario 2... id 10 and 11 have different years in your test but i fixed this in my test
    ,case 
        when lead(BeginDate) over (partition by EmployeeKey order by Id) = BeginDate
             and  lead(PaidDate) over (partition by EmployeeKey order by Id) > PaidDate then 'True' 
    end
from @table