根据键的支付日期比较(大于或小于)第一行值和第二行值,然后检查第三行是否大于第二行
Compare (greater than or less than) 1st row value with 2nd row value based on Paid date for the Key, and then check if 3rd row is greater than 2nd
当前结果
-- Scenario1--对于ID 1和2,我想比较Diff Between (DD,Begin date of ID 2 , End Date OF ID 1) < = 1, 那么该值应该为真,否则显示不显示该值
Scenario2 对于 ID 10 和 11 -- ID 10 的开始日期和 ID 11 的开始日期相同,所以我想要根据付款日期进行比较,因为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
查看第二个场景的日期。 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
当前结果
-- Scenario1--对于ID 1和2,我想比较Diff Between (DD,Begin date of ID 2 , End Date OF ID 1) < = 1, 那么该值应该为真,否则显示不显示该值
Scenario2 对于 ID 10 和 11 -- ID 10 的开始日期和 ID 11 的开始日期相同,所以我想要根据付款日期进行比较,因为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
查看第二个场景的日期。 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