英文:
how to combine two different tables into one
问题
I have a table with below structure
INSERT INTO reservation(rid, WHEEL_CHAIR, blind) VALUES (1,8,9)
INSERT INTO reservation(rid, WHEEL_CHAIR, blind) VALUES (2,11,12)
SELECT id,
WHEEL_CHAIR,
blind
FROM reservation
1 | 8 | 9 |
2 | 11 | 12 |
This gives a count of people who need wheel chair(8) and count of blind people (9) and I have a disability
table where the details of the disability is explained
INSERT INTO disability(hid, code, DESCRIPTION) VALUES (5,'aa', 'wheel chair')
INSERT INTO disability(hid, code, DESCRIPTION) VALUES (7,'bl', 'blind')
I want to club the above 2 tables and need to have a new table with the data
insert into newTable( newID,hid,count) values (1,5,8)
insert into newTable( newID,hid,count) values (1,7,9)
insert into newTable( newID,hid,count) values (2,5,11)
insert into newTable( newID,hid,count) values (2,7,12)
here 5 and 7 are the disability ids and 8,9,11,12 are the count of the blind people or the count of people who need a wheel chair.
英文:
I have a table with below structure
INSERT INTO reservation(rid, WHEEL_CHAIR, blind) VALUES (1,8,9)
INSERT INTO reservation(rid, WHEEL_CHAIR, blind) VALUES (2,11,12)
SELECT id,
WHEEL_CHAIR,
blind
FROM reservation
1 | 8 | 9 |
2 | 11 | 12 |
This gives a count of people who need wheel chair(8) and count of blind people (9) and I have a disability
table where the details of the disability is explained
INSERT INTO disability(hid, code, DESCRIPTION) VALUES (5,'aa', 'wheel chair')
INSERT INTO disability(hid, code, DESCRIPTION) VALUES (7,'bl', 'blind')
I want to club the above 2 tables and need to have a new table with the data
insert into newTable( newID,hid,count) values (1,5,8)
insert into newTable( newID,hid,count) values (1,7,9)
insert into newTable( newID,hid,count) values (2,5,11)
insert into newTable( newID,hid,count) values (2,7,12)
here 5 and 7 are the disability ids and 8,9,11,12 are the count of the blind people or the count of people who need a wheel chair.
答案1
得分: 1
以下是翻译好的部分:
对我来说,这看起来像是一个带有case
表达式的交叉连接。
示例数据:
SQL> with
2 reservation (rid, wheel_chair, blind) as
3 (select 1, 8, 9 from dual union all
4 select 2, 11, 12 from dual
5 ),
6 disability (hid, code, description) as
7 (select 5, 'aa', 'wheel chair' from dual union all
8 select 7, 'bl', 'blind' from dual
9 )
查询从这里开始:
10 select r.rid as newid,
11 d.hid,
12 case when d.hid = 5 then r.wheel_chair
13 when d.hid = 7 then r.blind
14 end as count
15 from reservation r cross join disability d;
NEWID HID COUNT
---------- ---------- ----------
1 5 8
1 7 9
2 5 11
2 7 12
SQL>;
如果您需要将这些行插入到新表中,这应该是一个相当简单的任务,所以我会留给您。
英文:
To me, it looks as a cross join with case
expression.
Sample data:
SQL> with
2 reservation (rid, wheel_chair, blind) as
3 (select 1, 8, 9 from dual union all
4 select 2, 11, 12 from dual
5 ),
6 disability (hid, code, description) as
7 (select 5, 'aa', 'wheel chair' from dual union all
8 select 7, 'bl', 'blind' from dual
9 )
Query begins here:
10 select r.rid as newid,
11 d.hid,
12 case when d.hid = 5 then r.wheel_chair
13 when d.hid = 7 then r.blind
14 end as count
15 from reservation r cross join disability d;
NEWID HID COUNT
---------- ---------- ----------
1 5 8
1 7 9
2 5 11
2 7 12
SQL>
If you have to insert these rows into a new table, that should be fairly simple task to do so I'll leave it to you.
答案2
得分: 0
将reservation
表进行解压缩,然后与disabilities
表进行INNER JOIN
:
SELECT rid AS newId, hid, cnt
FROM reservation
UNPIVOT (
cnt FOR description IN (
wheel_chair AS 'wheel chair',
blind AS 'blind'
)
) r
INNER JOIN disability d
ON (d.description = r.description)
对于示例数据:
CREATE TABLE reservation(rid, WHEEL_CHAIR, blind) AS
SELECT 1,8,9 FROM DUAL UNION ALL
SELECT 2,11,12 FROM DUAL;
CREATE TABLE disability(hid, code, DESCRIPTION) AS
SELECT 5, 'aa', 'wheel chair' FROM DUAL UNION ALL
SELECT 7, 'bl', 'blind' FROM DUAL
输出:
NEWID | HID | CNT |
---|---|---|
1 | 5 | 8 |
1 | 7 | 9 |
2 | 5 | 11 |
2 | 7 | 12 |
英文:
Unpivot the reservation
table and then INNER JOIN
it to the disabilities
table:
SELECT rid AS newId, hid, cnt
FROM reservation
UNPIVOT (
cnt FOR description IN (
wheel_chair AS 'wheel chair',
blind AS 'blind'
)
) r
INNER JOIN disability d
ON (d.description = r.description)
Which, for the sample data:
CREATE TABLE reservation(rid, WHEEL_CHAIR, blind) AS
SELECT 1,8,9 FROM DUAL UNION ALL
SELECT 2,11,12 FROM DUAL;
CREATE TABLE disability(hid, code, DESCRIPTION) AS
SELECT 5, 'aa', 'wheel chair' FROM DUAL UNION ALL
SELECT 7, 'bl', 'blind' FROM DUAL
Outputs:
NEWID | HID | CNT |
---|---|---|
1 | 5 | 8 |
1 | 7 | 9 |
2 | 5 | 11 |
2 | 7 | 12 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论