SQL 插入没有匹配项的缺失行

SQL Insert Missing Rows with no Match

我有一个问题,我目前有 1 个 table,其中充满了数据并按周汇总,不幸的是 table 数据来自没有每个人的条目每周(有很多原因),因此我想根据第一个温度 table 的结果创建一个 table 而不是一周检查温度 table

这是第一个 select 进入温度 table

的结果

Table 1

Logger Name AGENT   ManagerName AGENT_ID    Week    Logs    MonthID
Logger1     Agent1  Manager1    ID1         42      25      179
Logger1     Agent1  Manager1    ID1         44      120     180
Logger1     Agent1  Manager1    ID1         45      11      180
Logger1     Agent1  Manager1    ID1         48      41      181
Logger1     Agent1  Manager1    ID1         49      223     181
Logger1     Agent1  Manager1    ID1         50      92      181

Table 2(临时工周)对照

Week    Month
40      179
41      179
42      179
43      179
44      180
45      180
46      180
47      180
48      181
49      181
50      181

结束 Table 期望的结果(已添加缺失周数的数据,从 Table 1 开始,日志值为 0

Logger Name AGENT   ManagerName AGENT_ID    Week    Logs    MonthID         
Logger1     Agent1  Manager1    ID1         40      0       179
Logger1     Agent1  Manager1    ID1         41      0       179
Logger1     Agent1  Manager1    ID1         42      25      179
Logger1     Agent1  Manager1    ID1         43      0       179
Logger1     Agent1  Manager1    ID1         44      120     180
Logger1     Agent1  Manager1    ID1         45      11      180
Logger1     Agent1  Manager1    ID1         46      0       180
Logger1     Agent1  Manager1    ID1         47      0       180
Logger1     Agent1  Manager1    ID1         48      41      181
Logger1     Agent1  Manager1    ID1         49      223     181
Logger1     Agent1  Manager1    ID1         50      92      181

任何人都可以帮助我创建一个查询来创建这个 table,Table 1 中会有很多代理,但日志名称将保持不变。

可能不需要周检查温度 table 这只是我想以某种方式检查,如果 table 中不存在月份和周 1 然后添加!

这是一种选择。查询查找代理和周的所有组合。并删除第一个 table 中已经存在的周。然后插入剩余数据

declare @t table ([Logger Name] varchar(100), AGENT varchar(100), ManagerName varchar(100), AGENT_ID varchar(100), Week int, Logs int, MonthID int)
insert into @t
values ('Logger1', 'Agent1', 'Manager1', 'ID1', 42, 25, 179)
    , ('Logger1', 'Agent1', 'Manager1', 'ID1', 44, 120, 180)
    , ('Logger1', 'Agent1', 'Manager1', 'ID1', 45, 11, 180)
    , ('Logger1', 'Agent1', 'Manager1', 'ID1', 48, 41, 181)
    , ('Logger1', 'Agent1', 'Manager1', 'ID1', 49, 223, 181)
    , ('Logger1', 'Agent1', 'Manager1', 'ID1', 50, 92, 181)

declare @q table (Week int, Month int)
insert into @q
values (40, 179), (41, 179)
    , (42, 179), (43, 179), (44, 180)
    , (45, 180), (46, 180), (47, 180)
    , (48, 181), (49, 181), (50, 181)

insert into @t
select
    distinct t.[Logger Name], t.AGENT, t.ManagerName, t.AGENT_ID, q.Week, 0, q.Month
from
    @t t
    cross join @q q
where
    not exists (
        select 1
        from
            @t f
        where
            f.AGENT = t.AGENT
            and f.Week = q.Week
            and f.MonthID = q.Month
    )

select * from @t
order by AGENT, Week

使用 cross join 生成行,然后使用 left join 添加附加数据:

select l.*, w.*,
       coalesce(t1.logs, 0) as logs
from (select distinct LoggerName, Agent, ManagerName, AgentId
      from table1
     ) l cross join
     tempweeks w left join
     table1 t1
     on t1.LoggerName = l.LoggerName and t1.Agent = l.Agent and
        t1.ManagerName = l.ManagerName and t1.AgentId = l.AgentId and
        t1.week = w.week and t1.monthid = w.weekid;

您可以轻松地将其转换为插入内容,方法如下:

insert into table1 ( . . . )
    select . . ., 0 as logs
    from (select distinct LoggerName, Agent, ManagerName, AgentId
          from table1
         ) l cross join
         tempweeks w left join
         table1 t1
         on t1.LoggerName = l.LoggerName and t1.Agent = l.Agent and
            t1.ManagerName = l.ManagerName and t1.AgentId = l.AgentId and
            t1.week = w.week and t1.monthid = w.weekid
    where t1.LoggerName is null;