Sql nd个不同的config1/config2对,这些对不在表A中。您应该在tableA中插入4行。运行内部选择时,您看到了什么? Time term_id config1 config2

Sql nd个不同的config1/config2对,这些对不在表A中。您应该在tableA中插入4行。运行内部选择时,您看到了什么? Time term_id config1 config2 ,sql,sql-server,tsql,sql-server-2016,Sql,Sql Server,Tsql,Sql Server 2016,nd个不同的config1/config2对,这些对不在表A中。您应该在tableA中插入4行。运行内部选择时,您看到了什么? Time term_id config1 config2 final 2017-04-19 23:59:00 8 147 0 NULL 2017-04-19 23:59:00 9 87

nd个不同的config1/config2对,这些对不在表A中。您应该在tableA中插入4行。运行内部选择时,您看到了什么?
Time                             term_id   config1   config2    final
2017-04-19 23:59:00                 8        147       0        NULL
2017-04-19 23:59:00                 9        87        0        NULL
2017-04-19 23:59:00                10        284       0        NULL
Time                             term_id   config1   config2       Claims     Rejects
2017-04-20 00:00:00                8         148       0            568.2        62
2017-04-20 01:00:00                8         148       0            569          62
2017-04-20 02:00:00                8         148       0            572          62
2017-04-20 00:00:00                9         88        0            458          12
2017-04-20 01:00:00                9         88        0            459          12
2017-04-20 02:00:00                9         88        0            462          12
2017-04-20 00:00:00               10         285       0            125          13
2017-04-20 01:00:00               10         285       0            123          13
2017-04-20 02:00:00               10         285       0            117          13
2017-04-20 03:00:00               10         286       1            119          13
Time                           term_id         config1     config2     final
2017-04-19 23:59:00                 8            147           0        NULL
2017-04-19 23:59:00                 9            87            0        NULL
2017-04-19 23:59:00                 10           284           0        NULL
2017-04-20 00:00:00                 8            148           0         1
2017-04-20 00:00:00                 9            88            0         1
2017-04-20 00:00:00                 10           285           0         1
2017-04-20 03:00:00                 10           286           1         1
insert into TableA 
select B.[time], B.term_id, B.Config1, B.config2,
        (CASE
        WHEN B.config1 <> A.config1 OR 
             B.config2 <> A.config2 
        THEN 1 
        ELSE 0 END)
from 
    TableB B   
left join 
    (select max([time]) as maxtime, term_id, config1, config2
     from TableA
     group by [time], term_id, config1, config2
    ) as A
on A.term_id = B.term_id
from TableB B left join 
     (select max([time]) as maxtime, term_id, config1, config2
      from TableA
      group by term_id, config1, config2
     ) A
     on A.term_id = B.term_id
INSERT INTO TableA
SELECT MIN([time]) as ConfigTime, term_id, config1, config2, 1
FROM TableB
GROUP BY term_id, config1, config2
INSERT INTO TableA
SELECT B.*, 1
FROM (
    SELECT MIN([time]) as ConfigTime, term_id, config1, config2
    FROM TableB
    GROUP BY term_id, config1, config2
) B
LEFT JOIN TableA A ON a.term_id = B.term_id
     AND A.config1 = B.config1 AND A.config2 = B.config2
WHERE A.[time] iS NULL
INSERT INTO tablea
SELECT 
    min_time, b.term_id, b.config1, b.config_2, 1 as final
FROM
    (SELECT 
        term_id, config1, config_2, 
        MIN(TIME) AS min_time
    FROM
        tableb
    GROUP BY
        term_id, config1, config_2) b INNER JOIN
    tablea a ON
    b.termid = a.termid AND
    ((b.config1 <> a.config1) OR
    (b.config2 <> a.config2))