我在尝试从PL / SQL中的两个表合并两个列时遇到了极大的困难。我已经在上午9点开始工作了,我放弃了,请帮助我。
目的
我想创建一个新表(称之为temp)。这会合并来自两个不同表中其他两列的信息。
代码到目前为止
CREATE TABLE temp
(
temptimeKey CHAR(24) NOT NULL ,
temptimeHour INTEGER NULL ,
temptimeMinute INTEGER NULL ,
temptimeSecond INTEGER NULL ,
temptimeMonth INTEGER NULL ,
temptimeDay INTEGER NULL ,
temptimeYear INTEGER NULL ,
temptimeQuarter INTEGER NULL ,
CONSTRAINT XPKTEMPTIME PRIMARY KEY (temptimeKey)
);
insert into temp
SELECT
TO_CHAR(busFareDate, 'MM/DD/YYYY HH:MM:SS Q'),
TO_NUMBER(TO_CHAR(busFareDate, 'HH12')),
TO_NUMBER(TO_CHAR(busFareDate, 'MI')),
TO_NUMBER(TO_CHAR(busFareDate, 'SS')),
TO_NUMBER(TO_CHAR(busFareDate, 'MM')),
TO_NUMBER(TO_CHAR(busFareDate, 'DD')),
TO_NUMBER(TO_CHAR(busFareDate, 'YYYY')),
TO_NUMBER(TO_CHAR(busFareDate, 'Q'))
FROM
bus_fare
UNION
SELECT
TO_CHAR(trainFareDate, 'MM/DD/YYYY HH:MM:SS Q'),
TO_NUMBER(TO_CHAR(trainFareDate, 'HH12')),
TO_NUMBER(TO_CHAR(trainFareDate, 'MI')),
TO_NUMBER(TO_CHAR(trainFareDate, 'SS')),
TO_NUMBER(TO_CHAR(trainFareDate, 'MM')),
TO_NUMBER(TO_CHAR(trainFareDate, 'DD')),
TO_NUMBER(TO_CHAR(trainFareDate, 'YYYY')),
TO_NUMBER(TO_CHAR(trainFareDate, 'Q'))
FROM
train_fare;
drop table temp cascade constraints;
到目前为止,此代码仅违反了唯一约束:
ERROR at line 1:
ORA-00001: unique constraint (OPS$FNAVA.XPKTEMPTIME) violated
你能发现我做错了什么吗? (欢迎任何帮助)
答案 0 :(得分:3)
试试这个:
insert into temp
SELECT
TO_CHAR(busFareDate, 'MM/DD/YYYY HH:MI:SS Q'),
TO_NUMBER(TO_CHAR(busFareDate, 'HH12')),
TO_NUMBER(TO_CHAR(busFareDate, 'MI')),
TO_NUMBER(TO_CHAR(busFareDate, 'SS')),
TO_NUMBER(TO_CHAR(busFareDate, 'MM')),
TO_NUMBER(TO_CHAR(busFareDate, 'DD')),
TO_NUMBER(TO_CHAR(busFareDate, 'YYYY')),
TO_NUMBER(TO_CHAR(busFareDate, 'Q'))
FROM (
SELECT busFareDate
FROM bus_fare
GROUP BY busFareDate
UNION
SELECT trainFareDate
FROM train_fare
GROUP BY trainFareDate) AS Dates
GROUP BY TO_CHAR(busFareDate, 'MM/DD/YYYY HH:MI:SS Q'),
TO_NUMBER(TO_CHAR(busFareDate, 'HH12')),
TO_NUMBER(TO_CHAR(busFareDate, 'MI')),
TO_NUMBER(TO_CHAR(busFareDate, 'SS')),
TO_NUMBER(TO_CHAR(busFareDate, 'MM')),
TO_NUMBER(TO_CHAR(busFareDate, 'DD')),
TO_NUMBER(TO_CHAR(busFareDate, 'YYYY')),
TO_NUMBER(TO_CHAR(busFareDate, 'Q'));
答案 1 :(得分:1)
奇怪的是,UNION应该自动删除重复项。这是在Oracle DBMS上吗?
UNIQUE CONSTRAINT违反错误是由于您的查询为您要插入的表上的键列返回多行。
在您的情况下,TO_CHAR(trainFareDate,'MM / DD / YYYY HH:MM:SS Q')将插入到键列中。如果有两行具有相同的MM / DD / YYYY HH:MM:SS Q,假设您的DBMS确实正在执行“UNION ALL”,您将收到此错误。
您可以在主键上添加第二列,也可以将其命名为“SOURCE”并用“BUS”或“TRAIN”填充。
或者,您可以对表进行完全外连接。
SELECT coalesce(busFareDate, trainFareDate) from
bus_fare FULL OUTER JOIN train_fare ON
(bus_fare.busFareDate = train_fare.trainFareDate);
然后使用它作为子查询执行您在日期上需要完成的所有工作。
编辑:
我认为您的真正问题可能来自使用HH / HH12。
如果你这样做怎么办:
SELECT
TO_CHAR(fareDate , 'MM/DD/YYYY HH24:MI:SS Q'),
TO_NUMBER(TO_CHAR(fareDate , 'HH24')),
TO_NUMBER(TO_CHAR(fareDate , 'MI')),
TO_NUMBER(TO_CHAR(fareDate , 'SS')),
TO_NUMBER(TO_CHAR(fareDate , 'MM')),
TO_NUMBER(TO_CHAR(fareDate , 'DD')),
TO_NUMBER(TO_CHAR(fareDate , 'YYYY')),
TO_NUMBER(TO_CHAR(fareDate , 'Q'))
FROM
(SELECT coalesce(busFareDate, trainFareDate) fareDate from
bus_fare FULL OUTER JOIN train_fare ON
(bus_fare.busFareDate = train_fare.trainFareDate));
答案 2 :(得分:0)
您必须拥有违反约束的相同日期/时间的公共汽车票价和时间票价。为什么不尝试添加GUID列作为临时表的主键,并使用newid()在union的每一侧生成值。
答案 3 :(得分:-1)
尝试在TO_CHAR()周围添加TRIM(),如:
TRIM(TO_CHAR(busFareDate, 'MM/DD/YYYY HH:MI:SS Q'))
我想我记得TO_CHAR有时会在你可能没想到的地方添加空格填充。如果空间填充导致总长度超过24个字符,那么当它进入临时表中的目标列时它将被截断,这将使得它看起来没有重复(即,它们是不同的仅在最后2或3个字符中被切断。
或者,将CHAR(24)列扩展为更大的类似CHAR(1000),以查看问题是否消失。