Hi,
I'm trying insert into table rows but I've problems. I explain the tables and the problem.
I have old table with duplicates with this structure and values:
CREATE TABLE controls (
ID VARCHAR(12) NOT NULL,
fdate VARCHAR(10) NOT NULL,
fhour VARCHAR(5) NOT NULL,
place VARCHAR(50) NOT NULL,
code NUMBER(4),
date_code VARCHAR(10),
matter VARCHAR(14),
answer VARCHAR(20)
);
the values are similar as these:
IOI7999O3042 | 2008-03-15 | 11:00 | Sotoseca | 1 | 2008-03-17 | sacarosa | dopaje leve |
IOI7999O3042 | 2008-03-15 | 23:56 | La Granja de la Alameda | 1 | 2008-03-17 | sacarosa | dopaje leve |
OII2056O8476 | 2008-03-14 | 19:08 | Sotoseca | 1 | 2008-03-17 | | dormir poco |
QQO8904O7667 | 2008-03-17 | 11:10 | Sotocalores | 1 | 2008-03-20 | manzanilla | dopaje grave |
QIQ0237Q1090 | 2008-03-19 | 13:40 | Villasequillo del Canto Rodao | 1 | 2008-03-20 | | escaquearse |
QOI1274Q7655 | 2008-03-21 | 14:34 | Sierra del Bosque | 1 | 2008-03-21 | mosto | dopaje leve |
QOQ0821O3249 | 2008-04-05 | 10:22 | Valpiedras de los Trigos | 2 | 2008-04-07 | fructosa | dopaje leve |
IQI6926O4949 | 2008-04-08 | 09:05 | San Pedro del Lago | 2 | 2008-04-09 | miel | dopaje leve |
OQQ8999O8493 | 2008-04-07 | 16:54 | Estacas de los Castillos | 2 | 2008-04-10 | naranja | dopaje grave |
IQI5315O7379 | 2008-04-13 | 11:39 | Sotosombras de la Arboleda | 2 | 2008-04-14 | | escaquearse |
The first table that I insert is:
CREATE TABLE register
(
fdate VARCHAR2(10) NOT NULL,
ID VARCHAR2(12) NOT NULL,
fhour VARCHAR(5) NOT NULL,
place VARCHAR2(50) NOT NULL,
CONSTRAINT PK_REGISTER PRIMARY KEY(fdate,ID),
);
And the insert in this table is:
insert into register(fdate,ID,fhour,place)
select fdate, id, max(fhour), max(fplace) from controls group by (fdate,ID);
For example: between these rows:
IOI7999O3042 | 2008-03-15 | 11:00 | La Granja de la Alameda | 1 | 2008-03-17 | sacarosa | dopaje leve |
IOI7999O3042 | 2008-03-15 | 23:56 | La Granja de la Alameda | 1 | 2008-03-17 | sacarosa | dopaje leve |
only insert the second one because the hour is greather.
The matter is in insert on the second table.
CREATE TABLE EVIDENCES
(
fdateVARCHAR2(10) NOT NULL,
ID VARCHAR2(12) NOT NULL,
matter VARCHAR2(14),
answer VARCHAR2(20) NOT NULL,
code NUMBER(4) NOT NULL,
date_code VARCHAR2(10) NOT NULL,
CONSTRAINT CK_code CHECK (code>0),
CONSTRAINT PK_EVIDENCES PRIMARY KEY (code, date_code),
CONSTRAINT FK_EVIDENCES FOREIGN KEY (date, ID) REFERENCES register (date, ID)
);
the insertion instruction I use is:
insert into EVIDENCES (fdate, ID, matter, answer, code, date_code)
select distinct fdate, ID, matter, answer, code, date_code from controls
where answer is not null and code is not null and date_code is not null;
In the examples following:
IOI7999O3042 | 2008-03-15 | 11:00 | Sotoseca | 1 | 2008-03-17 | sacarosa | dopaje leve |
IOI7999O3042 | 2008-03-15 | 23:56 | La Granja de la Alameda | 1 | 2008-03-17 | sacarosa | dopaje leve |
OII2056O8476 | 2008-03-14 | 19:08 | Sotoseca | 1 | 2008-03-17 | | dormir poco |
only insert this row because the hour is the first. The other registers don't must in the evidences table.
IOI7999O3042 | 2008-03-15 | 11:00 | Sotoseca | 1 | 2008-03-17 | sacarosa | dopaje leve |
Please help with my insertion code.
Thanks in advance.