使用新数据动态更新列
Dynamically Updating Columns with new Data
我正在处理一个 SQL table 超过 10K+ 值,本质上它控制着一天中生产站状态的更新。当前 SQL 服务器将在当前时间戳报告一条新消息 - 因此,一天可以为同一部分生成数百次新条目,同时只有列 "Production_Status" 和 "TimeStamp" 变了。我想创建一个新的 table 来选择唯一的零件名称,然后有两个其他列来控制为该零件调出最新条目。
我目前已经选择了数据 - 对其重新排序,使最新的时间戳排在列表的第一位。我目前正在尝试执行此动态 table 但我是 sql.
的新手
select dateTimeStamp,partNumber,lineStatus
from tblPLCData
where lineStatus like '_ Zone %' or lineStatus = 'Production'
order by dateTimeStamp desc;
预期结果应该是一个新表,其行数基于我们整个生产设施中的零件数量 - 此列将是静态的 - 然后另外两列将检查 Originaltable最新状态和时间戳并更新 newTable 中的其他两列。
我不需要有关 table 创建的帮助,但更多的是围绕基于另一个 table 的行更新的逻辑。
非常感谢。
看起来您可以利用子连接为每个 partNumber 找到 MAX statusDate,然后连接回自身,以便您可以获得与具有最大日期的记录对应的相应 lineStatus 值。我只是给你 inserting/updating 临时 table 但这可能是你可以采用的一般方法。
-- New table that might already exist in your db, I am creating one here
declare @NewTable(
partNumber int,
lineStatus varchar(max),
last_update datetime
)
-- To initially set up your table or to update your table later with new part numbers that were not added before
insert into @NewTable
select tpd.partNumber, tpd.lineStatus, tpd.lineStatusdate
from tblPLCData tpd
join (
select partNumber, MAX(lineStatusdate) lineStatusDateMax
from tblPLCData
group by partNumber
) maxStatusDate on tpd.partNumber = maxStatusDate.partNumber
and tpd.lineStatusdate = maxStatusDate.lineStatusDateMax
left join @NewTable nt on tbd.partNumber = nt.partNumber
where tpd.lineStatus like '_ Zone %' or tpd.lineStatus = 'Production' and nt.partNumber is null
-- To update your table whenever you deem it necessary to refresh it. I try to avoid triggers in my dbs
update nt set nt.lineStatus = tpd.lineStatus, nt.lineStatusdate = tpd.lineStatusDate
from tblPLCData tpd
join (
select partNumber, MAX(lineStatusdate) lineStatusDateMax
from tblPLCData
group by partNumber
) maxStatusDate on tpd.partNumber = maxStatusDate.partNumber
and tpd.lineStatusdate = maxStatusDate.lineStatusDateMax
join @NewTable nt on tbd.partNumber = nt.partNumber
where tpd.lineStatus like '_ Zone %' or tpd.lineStatus = 'Production'
我正在处理一个 SQL table 超过 10K+ 值,本质上它控制着一天中生产站状态的更新。当前 SQL 服务器将在当前时间戳报告一条新消息 - 因此,一天可以为同一部分生成数百次新条目,同时只有列 "Production_Status" 和 "TimeStamp" 变了。我想创建一个新的 table 来选择唯一的零件名称,然后有两个其他列来控制为该零件调出最新条目。
我目前已经选择了数据 - 对其重新排序,使最新的时间戳排在列表的第一位。我目前正在尝试执行此动态 table 但我是 sql.
的新手select dateTimeStamp,partNumber,lineStatus
from tblPLCData
where lineStatus like '_ Zone %' or lineStatus = 'Production'
order by dateTimeStamp desc;
预期结果应该是一个新表,其行数基于我们整个生产设施中的零件数量 - 此列将是静态的 - 然后另外两列将检查 Originaltable最新状态和时间戳并更新 newTable 中的其他两列。
我不需要有关 table 创建的帮助,但更多的是围绕基于另一个 table 的行更新的逻辑。
非常感谢。
看起来您可以利用子连接为每个 partNumber 找到 MAX statusDate,然后连接回自身,以便您可以获得与具有最大日期的记录对应的相应 lineStatus 值。我只是给你 inserting/updating 临时 table 但这可能是你可以采用的一般方法。
-- New table that might already exist in your db, I am creating one here
declare @NewTable(
partNumber int,
lineStatus varchar(max),
last_update datetime
)
-- To initially set up your table or to update your table later with new part numbers that were not added before
insert into @NewTable
select tpd.partNumber, tpd.lineStatus, tpd.lineStatusdate
from tblPLCData tpd
join (
select partNumber, MAX(lineStatusdate) lineStatusDateMax
from tblPLCData
group by partNumber
) maxStatusDate on tpd.partNumber = maxStatusDate.partNumber
and tpd.lineStatusdate = maxStatusDate.lineStatusDateMax
left join @NewTable nt on tbd.partNumber = nt.partNumber
where tpd.lineStatus like '_ Zone %' or tpd.lineStatus = 'Production' and nt.partNumber is null
-- To update your table whenever you deem it necessary to refresh it. I try to avoid triggers in my dbs
update nt set nt.lineStatus = tpd.lineStatus, nt.lineStatusdate = tpd.lineStatusDate
from tblPLCData tpd
join (
select partNumber, MAX(lineStatusdate) lineStatusDateMax
from tblPLCData
group by partNumber
) maxStatusDate on tpd.partNumber = maxStatusDate.partNumber
and tpd.lineStatusdate = maxStatusDate.lineStatusDateMax
join @NewTable nt on tbd.partNumber = nt.partNumber
where tpd.lineStatus like '_ Zone %' or tpd.lineStatus = 'Production'