更新存在重叠的最早记录

时间:2019-02-08 12:32:11

标签: sql sql-server-2008 stored-procedures common-table-expression overlap

我有很多不准确的记录;分析师上传的数据未经处理,因此在此数据库中,@Component public class TenantRestClient { private Logger log = LogManager.getLogger(); @Autowired private RestTemplate restTemplate; public Set<String> findDatabaseInstanceIds() { //my logic here ResponseEntity<Set<String>> response = restTemplate.exchange(uriBuilder.toUriString(), HttpMethod.GET, entity, new ParameterizedTypeReference<Set<String>>() { }); } } INI中的行之间有很多重叠的日期,例如:

FIN

为此,我想创建一个显示两条记录的CTE /存储过程:一个重叠的记录和一个重叠的记录。然后根据valido ID id_tip id_hr perpro rut ini fin ult_act ------ --- ------ ----- --------- ---------- ------------------------- ----------------------- ---------------------- 1 52 001 666 201802 6666666-6 2018-05-01 00:00:00.000 2018-05-10 00:00:00.000 2018-09-12 00:00:00.000 1 53 001 666 201802 6666666-6 2018-05-09 00:00:00.000 2018-05-12 00:00:00.000 2018-09-13 00:00:00.000 更新最旧的文件,这是上载文件的时间戳。我当前的选择能够打印与新记录重叠的旧记录以及具有匹配的ULT_ACTINI2日期的记录:

FIN1

打印以下内容:

SELECT ROW_NUMBER() OVER (PARTITION BY ID_HR ORDER BY ULT_ACT ASC) AS IDOR, 
T.ID, 
T.RUT, 
CONVERT(VARCHAR,T.INI,112) AS INI,
CONVERT(VARCHAR,T.FIN,112) AS FIN, 
CONVERT(VARCHAR,T.ULT_ACT,112) AS ULT_ACT
FROM TSTSOLAP t 
WHERE EXISTS 
    (
        SELECT 1 FROM TSTSOLAP T2 
        WHERE t.RUT = t2.RUT AND
            T.ID_TIP = T2.ID_TIP AND
            T.ID <> t2.ID AND 
            (T2.INI BETWEEN T.INI AND T.FIN)
            OR T2.INI = T.FIN
            --T2.INI < T.FIN OR
            --T2.INI = T.FIN
            --OR t.INI <= t2.FIN
    )
ORDER BY ID
--UPDATE I WANT TO IMPLEMENT, IT CHANGES FIN AND SETS IT TO T2.INI - 1 SO IT REMOVES THE OVERLAPPED CONDITION
--UPDATE TSTSOLAP SET T.FIN=INI-1 WHERE IDOR > 1

我希望以更“可执行”的方式打印案例和更新实现。

0 个答案:

没有答案