Sql 如何将行数据显示到ORACLE的不同列中
我是SQL新手,目前我正尝试这样做: 在同一行的不同列中显示多行数据 我有一张这样的桌子:Sql 如何将行数据显示到ORACLE的不同列中,sql,oracle,view,Sql,Oracle,View,我是SQL新手,目前我正尝试这样做: 在同一行的不同列中显示多行数据 我有一张这样的桌子: CREATE TABLE TRIPLEG( T# NUMBER(10) NOT NULL, LEG# NUMBER(2) NOT NULL, DEPARTURE VARCHAR(30) NOT NULL, DESTINATION VARCHAR(30) NOT NULL
CREATE TABLE TRIPLEG(
T# NUMBER(10) NOT NULL,
LEG# NUMBER(2) NOT NULL,
DEPARTURE VARCHAR(30) NOT NULL,
DESTINATION VARCHAR(30) NOT NULL,
CONSTRAINT TRIPLEG_PKEY PRIMARY KEY (T#, LEG#),
CONSTRAINT TRIPLEG_UNIQUE UNIQUE(T#, DEPARTURE, DESTINATION),
CONSTRAINT TRIPLEG_FKEY1 FOREIGN KEY (T#) REFERENCES TRIP(T#) );
INSERT INTO TRIPLEG VALUES( 1, 1, 'Sydney', 'Melbourne');
INSERT INTO TRIPLEG VALUES( 1, 2, 'Melbourne', 'Adelaide');
结果应该是这样的:
CREATE TABLE TRIPLEG(
T# NUMBER(10) NOT NULL,
LEG# NUMBER(2) NOT NULL,
DEPARTURE VARCHAR(30) NOT NULL,
DESTINATION VARCHAR(30) NOT NULL,
CONSTRAINT TRIPLEG_PKEY PRIMARY KEY (T#, LEG#),
CONSTRAINT TRIPLEG_UNIQUE UNIQUE(T#, DEPARTURE, DESTINATION),
CONSTRAINT TRIPLEG_FKEY1 FOREIGN KEY (T#) REFERENCES TRIP(T#) );
INSERT INTO TRIPLEG VALUES( 1, 1, 'Sydney', 'Melbourne');
INSERT INTO TRIPLEG VALUES( 1, 2, 'Melbourne', 'Adelaide');
起源|目的1 |目的2
1 |悉尼|梅尔博伦|阿德莱德
起源就是离开。
DESTINATION1可以是出发点或目的地。
DESTINATION2是目的地
查询应该包括COUNTT<3,因为我只需要显示小于3的记录。如何使用2关系视图实现此结果?尝试以下方法:
select t1.T#,
(select t2.departure from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 1) Origin,
(select t2.destination from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 1) Destination1,
(select t2.destination from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 2) Destination2
from tripleg t1
group by t1.T#
having count(1) < 3
试试这个:
select t1.T#,
(select t2.departure from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 1) Origin,
(select t2.destination from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 1) Destination1,
(select t2.destination from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 2) Destination2
from tripleg t1
group by t1.T#
having count(1) < 3
试试这个:
select t1.T#,
(select t2.departure from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 1) Origin,
(select t2.destination from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 1) Destination1,
(select t2.destination from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 2) Destination2
from tripleg t1
group by t1.T#
having count(1) < 3
试试这个:
select t1.T#,
(select t2.departure from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 1) Origin,
(select t2.destination from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 1) Destination1,
(select t2.destination from tripleg t2 where t1.T# = t2.T# and t2.LEG# = 2) Destination2
from tripleg t1
group by t1.T#
having count(1) < 3
您可以使用分层查询 数据设置: 查询: 说明: 根据出发点和目的地之间的关系连接行。 筛选满足条件的行 腿是不用的。
您可以使用分层查询 数据设置: 查询: 说明: 根据出发点和目的地之间的关系连接行。 筛选满足条件的行 腿是不用的。
您可以使用分层查询 数据设置: 查询: 说明: 根据出发点和目的地之间的关系连接行。 筛选满足条件的行 腿是不用的。
您可以使用分层查询 数据设置: 查询: 说明: 根据出发点和目的地之间的关系连接行。 筛选满足条件的行 腿是不用的。 请试试这个
select t#,regexp_substr(tree,'[^:]+',2) origin,
regexp_substr(tree,'[^:]+',2,2) destination1,
regexp_substr(tree,'[^:]+',2,3) destination2
from
(
select a.*,
sys_connect_by_path(departure,':') tree,
level lvl
from tripleg a
connect by nocycle prior destination = departure and prior t# = t#
start with leg# = 1
and level < = 3
)
where lvl = 3;
请试试这个
select t#,regexp_substr(tree,'[^:]+',2) origin,
regexp_substr(tree,'[^:]+',2,2) destination1,
regexp_substr(tree,'[^:]+',2,3) destination2
from
(
select a.*,
sys_connect_by_path(departure,':') tree,
level lvl
from tripleg a
connect by nocycle prior destination = departure and prior t# = t#
start with leg# = 1
and level < = 3
)
where lvl = 3;
请试试这个
select t#,regexp_substr(tree,'[^:]+',2) origin,
regexp_substr(tree,'[^:]+',2,2) destination1,
regexp_substr(tree,'[^:]+',2,3) destination2
from
(
select a.*,
sys_connect_by_path(departure,':') tree,
level lvl
from tripleg a
connect by nocycle prior destination = departure and prior t# = t#
start with leg# = 1
and level < = 3
)
where lvl = 3;
请试试这个
select t#,regexp_substr(tree,'[^:]+',2) origin,
regexp_substr(tree,'[^:]+',2,2) destination1,
regexp_substr(tree,'[^:]+',2,3) destination2
from
(
select a.*,
sys_connect_by_path(departure,':') tree,
level lvl
from tripleg a
connect by nocycle prior destination = departure and prior t# = t#
start with leg# = 1
and level < = 3
)
where lvl = 3;
很抱歉,我忘了将问题添加到具有2个关系视图的此查询中。很抱歉,我忘了将问题添加到具有2个关系视图的此查询中。很抱歉,我忘了将问题添加到具有2个关系视图的此查询中。很抱歉,我忘了将问题添加到具有2个关系视图的此查询中2个关系视图。你所说的两个关系视图是什么意思?您在问题中没有提到这些。您所说的两个关系视图是什么意思?您在问题中没有提到这些。您所说的两个关系视图是什么意思?您在问题中没有提到这些。您所说的两个关系视图是什么意思?你在问题中没有提到这些。