Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / Oracle / Oracle Server / February 2006

Tip: Looking for answers? Try searching our database.

Updating table with values of prior records

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Wachtel - 27 Feb 2006 22:11 GMT
Hi!

Given is a table SEASONS with a DATE field and an ID field. Some IDs have
values, some IDs are NULL values. Now I want to update the NULL values with
the last assigned ID value of a prior DATE.

STARTDATE        SEASON_ID
----------------      ------------
01.01.2006 00:00  1
02.01.2006 00:00  (null)
03.01.2006 00:00  2
04.01.2006 00:00  (null)

My below mentioned UPDATE statement fails with an ORA-00904 "S"."STARTDATE"
invalid identifier error. It seems that the table to be updated is out of
focus in the subquery.

Any help greatly appreciated.

tia

Robert

DROP TABLE SEASONS;
CREATE TABLE SEASONS (
"STARTDATE" DATE NOT NULL,
"SEASON_ID" NUMBER(15,5) NULL );

INSERT INTO SEASONS VALUES (TO_DATE('01.01.2006', 'DD.MM.YYYY'), 1);
INSERT INTO SEASONS VALUES (TO_DATE('02.01.2006', 'DD.MM.YYYY'), NULL);
INSERT INTO SEASONS VALUES (TO_DATE('03.01.2006', 'DD.MM.YYYY'), 2);
INSERT INTO SEASONS VALUES (TO_DATE('04.01.2006', 'DD.MM.YYYY'), NULL);

SELECT * FROM SEASONS;

UPDATE SEASONS S
SET S.SEASON_ID =
 (SELECT SEASON_ID FROM
   (SELECT * FROM SEASONS
   WHERE SEASON_ID IS NOT NULL
   AND STARTDATE < S.STARTDATE
   ORDER BY STARTDATE DESC)
 WHERE ROWNUM = 1)
WHERE S.SEASON_ID IS NULL;

SELECT * FROM SEASONS;
Robert Wachtel - 27 Feb 2006 22:45 GMT
Ok, should have written the last msg earlier... ;-)

It seems that using the analytic function LAST like this

UPDATE SEASONS S
 SET S.SEASON_ID = (SELECT MIN(SEASON_ID) KEEP (DENSE_RANK LAST ORDER BY
STARTDATE) FROM SEASONS WHERE STARTDATE < S.STARTDATE AND SEASON_ID IS NOT
NULL)
WHERE S.SEASON_ID IS NULL

did the trick.

Maybe anyone can confirm this solution?

tia

Robert
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.