CREATE TABLE Landmark
(
Indexc NUMBER
Birth DATE
Address VARCHAR2(50 BYTE)
City VARCHAR2(30 BYTE)
State CHAR(2 BYTE)
Zip VARCHAR2(15 BYTE)
...
Other properties
)
ALTER TABLE Landmark ADD (
CONSTRAINT Landmark_PK PRIMARY KEY (Indexc, Birth));
CREATE TABLE MapPoint
(
Indexc NUMBER
Longitude FLOAT(126)
Latitude FLOAT(126)
BuildDate DATE
)
ALTER TABLE MapPoint ADD (
CONSTRAINT MapPoint_FK FOREIGN KEY (Indexc) REFERENCES Landmark (Indexc));
「地標」表包含可隨時移動的地標列表。 MapPoint表格保存這些地標的經度和緯度。任何時候某個地標的屬性發生變化時,都會在該地標的Landmark表中插入一個新行,並且新的出生日期將無法使用該地址。用第二行到最後一行的數據更新最後一行
我有一個腳本將在MapPoint表中爲所有沒有物理移動的地標設置BuildDate爲空,我試圖編寫一個UDPATE語句來重用最後一行的地址。以下是接近我想要的,但不起作用,因爲最內層的子查詢不會與更新語句相關聯。我得到ORA-00904:「lm2」。「索引」:無效的標識符。
UPDATE Landmark lm1
SET (Address, City, State, Zip) = (
SELECT Address, City, State, Zip
FROM Landmark lm2
WHERE lm2.Indexc = lm1.Indexc
AND lm2.birth = (
SELECT MIN(Birth)
FROM (
SELECT Birth
FROM Landmark lm3
WHERE lm3.Indexc = lm2.Indexc
ORDER BY Birth DESC)
AND ROWNUM < 3))
WHERE Indexc IN (
SELECT Indexc
FROM MapPoint
WHERE BuildDate IS NULL);
樣品之前:
Indexc Birth Address City State Zip
------ ---------------------- ------------ ----------- ----- -----
45 8/16/2009 4:46:21 AM 123 Main St Springfield PA 84679
45 8/18/2009 7:20:27 PM 123 Main St Springfield PA 84679
45 8/20/2009 9:01:44 PM 456 Smith Ln Springfield PA 84153
45 10/31/2009 12:29:07 AM
樣品後:
Indexc Birth Address City State Zip
------ ---------------------- ------------ ----------- ----- -----
45 8/16/2009 4:46:21 AM 123 Main St Springfield PA 84679
45 8/18/2009 7:20:27 PM 123 Main St Springfield PA 84679
45 8/20/2009 9:01:44 PM 456 Smith Ln Springfield PA 84153
45 10/31/2009 12:29:07 AM 456 Smith Ln Springfield PA 84153
您可以發佈一些示例數據以及更新後的樣子嗎? – Quassnoi 2009-12-15 17:04:00