Tsql 根据特定条件向字段中添加数字

Tsql 根据特定条件向字段中添加数字,tsql,Tsql,我有以下数据 CompId PersonelNo EduId RecordsDay DateEs 1 1000 1 2 1370 1 1000 2 10 1370 1 1002 2 5 1380 1 1003 1 4 1391 1 1003 2 7 1

我有以下数据

CompId PersonelNo EduId RecordsDay DateEs
 1      1000        1       2       1370
 1      1000        2       10      1370
 1      1002        2       5       1380
 1      1003        1       4       1391
 1      1003        2       7       1391
我想为记录添加(1392-1390=2)最大EduID日期和日期小于或等于1390的记录,并为记录添加(DATES-1390)最大EduID日期和日期大于1390的记录

所以数据是这样的

 CompId PersonelNo EduId RecordsDay DateEs
  1      1000        1       2       1370 // record is the same becuase eduID is not Max for this Personel
  1      1000        2       12      1370 // this is max EduId for this personel and DateEs is less than 1390 so (1392-1390) +10 = 12
  1      1002        2       7       1380 //this is the only record for this personel and DateEs is less than 1390(1392-1390) +5 = 7
  1      1003        1       4       1391 // record is the same becuase eduID is not Max for this Personel
  1      1003        2       8       1391 // this is max EduId for this personel and DateEs is Greater than 1390 so (1391-1390) +7 = 8
我想用TSQl。我正在编写,但到目前为止我还可以编写它

您可以尝试:

SELECT CASE 
    WHEN [EduId] = MAX(EduId) OVER (Partition by PersonelNo) AND DateEs <= 1390 THEN RecordsDay + 2 
    WHEN [EduId] = MAX(EduId) OVER (Partition by PersonelNo) AND DateEs > 1390 THEN RecordsDay + (DateEs -1390) END
选择案例
当[EduId]=MAX(EduId)OVER(PersonelNo分区)和dates 1390时,则RecordsDay+(dates-1390)结束
您可以尝试:

SELECT CASE 
    WHEN [EduId] = MAX(EduId) OVER (Partition by PersonelNo) AND DateEs <= 1390 THEN RecordsDay + 2 
    WHEN [EduId] = MAX(EduId) OVER (Partition by PersonelNo) AND DateEs > 1390 THEN RecordsDay + (DateEs -1390) END
选择案例
当[EduId]=MAX(EduId)OVER(PersonelNo分区)和dates 1390时,则RecordsDay+(dates-1390)结束