[英]Update values in a table based on a self-join query to itself in Oracle
I have a table that contains logged events for manufactured items.我有一个表,其中包含制造项目的记录事件。 We consider each event as having 2 statuses that are based on detail and calculations from the prior logged events for the same item.
我们将每个事件视为具有 2 个状态,这些状态基于相同项目的先前记录事件的详细信息和计算。 So, I've developed a SELECT query that uses multiple self-joins to analyze factors from prior events relative to each event, and calculates the statuses.
因此,我开发了一个 SELECT 查询,它使用多个自联接来分析与每个事件相关的先前事件的因素,并计算状态。 But because this query is relatively slow, I have added 2 status columns, and I want to UPDATE the columns with the calculated statuses well after the events happen.
但是因为这个查询相对较慢,我添加了 2 个状态列,我想在事件发生后用计算的状态更新列。 This way I can get fast reports later on the status columns instead of having to run all the calculations each time.
通过这种方式,我可以稍后快速获得有关状态列的报告,而不必每次都运行所有计算。
Here would be my table:这是我的桌子:
CREATE TABLE ItemLog
(
ItemID decimal(11) NOT NULL,
MessageTime DATE NOT NULL,
Temperature float(7),
Voltage float(7),
Status1 VARCHAR2(10 BYTE),
Status2 VARCHAR2(10 BYTE),
CONSTRAINT "ItemLog_PK" PRIMARY KEY ("ItemID ", "MessageTime ")
);
My SELECT calculation query is something like this:我的 SELECT 计算查询是这样的:
SELECT ItemID, MessageTime,
CASE WHEN A.Voltage<B.Voltage and A.Voltage<C.Avg_Voltage and C.SD_Voltage<5 THEN 'Good' ELSE 'Bad' END Calculated_Status1,
CASE WHEN A.Temperature<B.Temperature and A.Temperature>C.Temperature and C.SD_Temperature>10 THEN 'Good' ELSE 'Bad' END Calculated_Status2
FROM ItemLog A,
(SELECT F.ItemID,
F.MessageTime Key_MessageTime,
S.Voltage,
S.Temperature
FROM ItemLog F,
ItemLog S
WHERE F.ItemID=S.ItemID
and S.MessageTime=
SELECT MAX(MessageTime)
FROM ItemLog
WHERE ItemID=F.ItemID
and MessageTime<F.MessageTime
and Voltage<12
and Temperature<125
) B, -- Returns the Voltage and Temperature from the prior time it was <12 and <125
(SELECT K.ItemID, K.MessageTime,
AVG(L.Temp) Avg_Temperature, STDDEV(L.Temperature) SD_Temp,
AVG(L.Voltage) Avg_Voltage, STDDEV(L.Voltage) SD_Voltage
FROM ItemLog K,
ItemLog L
WHERE K.ItemID=L.ItemID
and L.MessageTime=
SELECT MAX(MessageTime)
FROM ItemLog
WHERE ItemID=K.ItemID
and MessageTime<K.MessageTime
GROUP BY K.ItemID, K.MessageTime
) C -- Returns the Voltage and Temperature stats from all prior messages
(SELECT ItemID
FROM ItemLog
WHERE Voltage>40
) D -- Returns all ItemID where Voltage was ever >40, to exclude them
WHERE A.ItemID=B.ItemID and A.MessageTime=B.MessageTime
and A.ItemID=C.ItemID and A.MessageTime=C.MessageTime
and A.ItemID=D.ItemID(+) and D.ItemID IS NULL
So, the question is, how can I update the Status1 and Status2 columns in the table to be the Calculated_Status1 and Calculated Status2 columns?所以,问题是,如何将表中的 Status1 和 Status2 列更新为Calculated_Status1 和Calculated Status2 列? I've tried taking my calculation query and joining it to the table by the 2 primary keys, but I get the "ORA-01779: cannot modify a column which maps to a non key-preserved table" error.
我尝试将我的计算查询通过 2 个主键连接到表中,但我收到“ORA-01779:无法修改映射到非键保留表的列”错误。
UPDATE (
SELECT U.*,
V.Calculated_Status1
V.Calculated_Status2
FROM ItemLog U,
( <calculation query above> ) V
WHERE U.ItemID=V.ItemID and U.MessageTime=V.MessageTime )
SET U.Status1=V.CalculatedStatus1,
U.Status2=V.CalculatedStatus2
I could imagine an UPDATE with a SET Status1=(SELECT...
but that would need some sort of correlated WHERE for the ItemID and MessageTime, and I'd expect it to run horribly slow. It would seem that there should be a more direct way to do this?我可以想象一个带有
SET Status1=(SELECT...
直接的方式来做到这一点?
I hope the solution you are looking for can be served up using MERGE statement.我希望您正在寻找的解决方案可以使用 MERGE 语句提供。 I hope the query which you have posted is correct.
我希望您发布的查询是正确的。 I have build the solution on top of the query.
我已经在查询之上构建了解决方案。 Let me know if this helps.
如果这有帮助,请告诉我。
MERGE INTO ItemLog it USING
(SELECT ItemID, MessageTime,
CASE WHEN A.Voltage<B.Voltage and A.Voltage<C.Avg_Voltage and C.SD_Voltage<5 THEN 'Good' ELSE 'Bad' END Calculated_Status1,
CASE WHEN A.Temperature<B.Temperature and A.Temperature>C.Temperature and C.SD_Temperature>10 THEN 'Good' ELSE 'Bad' END Calculated_Status2
FROM ItemLog A,
(SELECT F.ItemID,
F.MessageTime Key_MessageTime,
S.Voltage,
S.Temperature
FROM ItemLog F,
ItemLog S
WHERE F.ItemID=S.ItemID
and S.MessageTime=
SELECT MAX(MessageTime)
FROM ItemLog
WHERE ItemID=F.ItemID
and MessageTime<F.MessageTime
and Voltage<12
and Temperature<125
) B, -- Returns the Voltage and Temperature from the prior time it was <12 and <125
(SELECT K.ItemID, K.MessageTime,
AVG(L.Temp) Avg_Temperature, STDDEV(L.Temperature) SD_Temp,
AVG(L.Voltage) Avg_Voltage, STDDEV(L.Voltage) SD_Voltage
FROM ItemLog K,
ItemLog L
WHERE K.ItemID=L.ItemID
and L.MessageTime=
SELECT MAX(MessageTime)
FROM ItemLog
WHERE ItemID=K.ItemID
and MessageTime<K.MessageTime
GROUP BY K.ItemID, K.MessageTime
) C -- Returns the Voltage and Temperature stats from all prior messages
(SELECT ItemID
FROM ItemLog
WHERE Voltage>40
) D -- Returns all ItemID where Voltage was ever >40, to exclude them
WHERE A.ItemID=B.ItemID and A.MessageTime=B.MessageTime
and A.ItemID=C.ItemID and A.MessageTime=C.MessageTime
and A.ItemID=D.ItemID(+) and D.ItemID IS NULL)z
ON
(it.ItemID = z.ItemID AND it.MessageTime = z.MessageTime)
WHEN MATCHED THEN
UPDATE SET it.STATUS1 = z.Calculated_Status1,
it.STATUS2 = z.Calculated_Status2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.