在 PL/SQL 中合并两个表中的两列
我在尝试合并 PL/SQL 中两个表中的两列时遇到了巨大的困难。我从早上 9:00 就开始做这件事,但我放弃了,请帮助我。
目的
我想创建一个新表(称之为临时表)。这会合并两个不同表中其他两列的信息。
到目前为止的代码
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
你能发现我做错了什么吗? (欢迎任何帮助)
I'm having tremendous difficulty trying to merge two columns from two tables in PL/SQL. I have been working since 9:00am on this and well I give up, please help me.
Purpose
I would like to create a new table (call it temp). That merges information from two other columns in two different tables.
Code So far
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;
So far, this code only gives me a violation of unique constraint:
ERROR at line 1:
ORA-00001: unique constraint (OPS$FNAVA.XPKTEMPTIME) violated
Can you spot what I am doing wrong? (any help is welcome)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
Try this:
奇怪的是,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”填充它。
或者,您可以对表进行完整的外部联接。
然后使用该日期作为子查询来执行您需要在日期上执行的所有工作。
编辑:
我认为你真正的问题可能来自使用 HH / HH12。
如果你这样做怎么办:
It's odd, UNION should remove duplicates automatically. Is this on Oracle DBMS?
UNIQUE CONSTRAINT violated errors are due to your query returning multiple rows for the key column on your table that you are inserting into.
In your case, TO_CHAR(trainFareDate, 'MM/DD/YYYY HH:MM:SS Q') would be inserted into the key column. If there are two rows with the same MM/DD/YYYY HH:MM:SS Q, you will get this error, assuming your DBMS is really doing a "UNION ALL".
You could add a second column to your primary key, maybe call it "SOURCE" and populate it with "BUS" or "TRAIN".
Or, you could do a full outer join of the tables.
Then perform all the work you need to do on the date using that as a subquery.
EDIT:
I think your real problem might be coming from using HH / HH12.
What if you do this:
您的巴士票价和时间票价必须具有相同的日期/时间,违反了限制。为什么不尝试添加一个 GUID 列作为临时表的主键并使用 newid() 在联合的每一侧生成值。
You must have a bus fare and time fare with the same date/time violating the constraint. Why not try to add a GUID column as the primary key of temp Table and use newid() to generate the value in each side of the union.
尝试在 TO_CHAR() 周围添加 TRIM(),如下所示:
我想我记得 TO_CHAR 有时会在您可能意想不到的地方添加空格填充。如果空格填充导致总长度超过 24 个字符,那么当它进入临时表中的目标列时,它将被截断,这会使得看起来有重复,而实际上没有(即,它们是不同的)仅在最后 2 或 3 个字符中被截断)。
或者,将 CHAR(24) 列扩展到更大的列,例如 CHAR(1000),看看问题是否消失。
Try adding a TRIM() around your TO_CHAR(), as in:
I think I remember that TO_CHAR will sometimes add space padding where you might not expect it. If the space padding causes the total length to exceed 24 characters, then it will be truncated as it goes into the target column in the temp table, which would make it seem that there are duplicates when there aren't (i.e., they are distinct only in the last 2 or 3 characters, which are being cut off).
Alternatively, expand the CHAR(24) column to something larger like CHAR(1000) just to see if the problem goes away.