我在同一个套房中有一个可容纳多人的桌子 我需要在套房中进行匹配,然后将每个人与他们的室友列表一起显示为“第一个ID”。
因为他们不止两个,所以我遇到了多个结果,其中第一个ID被多次列出,而室友的排序方式不同,或者我只能带回4个ID的一行
示例:
我有一个这样的表:
Table Name: ROOM_LIST
ID | BUILDING | SUITE | ROOM
01 | BU_1 | SU_1 | RO_1
02 | BU_1 | SU_1 | RO_2
03 | BU_1 | SU_1 | RO_3
04 | BU_1 | SU_1 | RO_4
05 | BU_1 | SU_2 | RO_1
06 | BU_1 | SU_2 | RO_2
07 | BU_2 | SU_1 | RO_1
08 | BU_2 | SU_1 | RO_2
我尝试过这样的查询:
select A.ID as Primary,
B.ID as Roomate_1,
C.ID as Roomate_2,
D.ID as Roomate_3,
A.BUILDING as Building,
A.SUITE As Suite,
A.ROOM As Room
from ROOM_LIST A
Left Join ROOM_LIST B on A.BUILDING = B.BUILDING and A.SUITE = B.SUITE
Left Join ROOM_LIST C on A.BUILDING = C.BUILDING and A.SUITE = C.SUITE
Left Join ROOM_LIST D on A.BUILDING = D.BUILDING and A.SUITE = D.SUITE
where A.ID > B.ID
and A.ID > C.ID
and A.ID > D.ID
and B.ID > C.ID
and B.ID > D.ID
and C.ID > D.ID
order by Primary,Roomate_1,Roomate_2,Roomate_3,Building,Suite,Room;
摆脱了多余的重复,但每个套件只得到一行,而不是每个ID为主要的一行。
也尝试过类似的操作,但是用<>或!=代替>,然后我得到多个重复的对象,其中第一个是id,但是第二,第三和第四是互换的,因此从技术上讲它们不是重复的。
这就是为什么我说“不相同”重复的原因:)
| Primary | Roomate_1 | Roomate_2 | Roomate_3 | Building | Suite | Room
| 01 | 02 | 03 | 04 | BU_1 | SU_1 | RO_1
| 02 | 03 | 04 | 01 | BU_1 | SU_1 | RO_2
| 03 | 04 | 01 | 02 | BU_1 | SU_1 | RO_3
| 04 | 01 | 02 | 03 | BU_1 | SU_1 | RO_4
| 05 | 06 | Null | Null | BU_1 | SU_2 | RO_1
| 06 | 05 | Null | Null | BU_1 | SU_2 | RO_2
我已经尝试在select语句或from中使用不同的子选择进行多个查询,但是我似乎无法将其归结为每个ID作为主ID的一个结果。我已经考虑过进行数据透视,但是(我的理解)只有在我具有多个结果的同一个ID并且我想将多个结果转换为列的情况下才能起作用。
考虑了一个联盟,但我不知道如何在多个联盟之间进行查询?如果那只是一件事
任何帮助将不胜感激
编辑:以下解决方案仅在12C下可用,但我需要11G解决方案:
with dt as (
select 01 id , 'BU_1' building, 'SU_1' suite ,'RO_1' room from dual union all
select 02 id , 'BU_1' building, 'SU_1' suite ,'RO_2' room from dual union all
select 03 id , 'BU_1' building, 'SU_1' suite ,'RO_3' room from dual union all
select 04 id , 'BU_1' building, 'SU_1' suite ,'RO_4' room from dual union all
select 05 id , 'BU_1' building, 'SU_2' suite ,'RO_1' room from dual union all
select 06 id , 'BU_1' building, 'SU_2' suite ,'RO_2' room from dual union all
select 07 id , 'BU_2' building, 'SU_1' suite ,'RO_1' room from dual union all
select 08 id , 'BU_2' building, 'SU_1' suite ,'RO_2' room from dual )
SELECT
A.ID as Primary,
( select id from (select id,rownum rn from dt b where a.building = b.building AND a.suite = b.suite and b.ID != a.ID order by id ) where rn=1) Roomate_1,
( select id from (select id,rownum rn from dt b where a.building = b.building AND a.suite = b.suite and b.ID != a.ID order by id ) where rn=2) Roomate_2,
( select id from (select id,rownum rn from dt b where a.building = b.building AND a.suite = b.suite and b.ID != a.ID order by id ) where rn=3) Roomate_3,
a.BUILDING as Building,
A.SUITE As Suite,
A.ROOM As Room
FROM
dt a
order by Primary,Roomate_1,Roomate_2,Roomate_3,Building,Suite,Room
我在以下给出的答案之一中添加了以下内容:and b.ID != a.ID
,并更改了rn=2 to rn=1
以从0开始计数。
答案 0 :(得分:1)
不确定性能影响。将需要进行分析,但给出预期的结果。
12c答案。
with dt as (
select 01 id , 'BU_1' building, 'SU_1' suite ,'RO_1' room from dual union all
select 02 , 'BU_1' building, 'SU_1' suite ,'RO_2' room from dual union all
select 03 , 'BU_1' building, 'SU_1' suite ,'RO_3' room from dual union all
select 04, 'BU_1' building, 'SU_1' suite ,'RO_4' room from dual union all
select 05 , 'BU_1' building, 'SU_2' suite ,'RO_1' room from dual union all
select 06 , 'BU_1' building, 'SU_2' suite ,'RO_2' room from dual union all
select 07 , 'BU_2' building, 'SU_1' suite ,'RO_1' room from dual union all
select 08 , 'BU_2' building, 'SU_1' suite ,'RO_2' room from dual )
SELECT
A.ID as Primary,
( select id from (select id,rownum rn from dt b where a.building = b.building AND a.suite = b.suite order by id ) where rn=2) Roomate_1,
( select id from (select id,rownum rn from dt b where a.building = b.building AND a.suite = b.suite order by id ) where rn=3) Roomate_2,
( select id from (select id,rownum rn from dt b where a.building = b.building AND a.suite = b.suite order by id ) where rn=4) Roomate_3,
a.BUILDING as Building,
A.SUITE As Suite,
A.ROOM As Room
FROM
dt a
order by Primary,Roomate_1,Roomate_2,Roomate_3,Building,Suite,Room
11g的答案。我不确定需要多少数据到roommate1到roomate3列中。
WITH dt as (
select 01 id , 'BU_1' building, 'SU_1' suite ,'RO_1' room from dual union all
select 02 id , 'BU_1' building, 'SU_1' suite ,'RO_2' room from dual union all
select 03 id , 'BU_1' building, 'SU_1' suite ,'RO_3' room from dual union all
select 04 id , 'BU_1' building, 'SU_1' suite ,'RO_4' room from dual union all
select 05 id , 'BU_1' building, 'SU_2' suite ,'RO_1' room from dual union all
select 06 id , 'BU_1' building, 'SU_2' suite ,'RO_2' room from dual union all
select 07 id , 'BU_2' building, 'SU_1' suite ,'RO_1' room from dual union all
select 08 id , 'BU_2' building, 'SU_1' suite ,'RO_2' room from dual ),
joindrslt AS (
SELECT a.*, b.id roommate,
ROW_NUMBER() OVER(PARTITION BY a.suite, a.building, a.room ORDER BY b.id ) AS ri
FROM
dt a
JOIN dt b ON a.building = b.building AND a.suite = b.suite AND b.id != a.id
ORDER BY b.id
)
SELECT ID Primary,
roomate_1,
roomate_2,
roomate_3,
Building,Suite,
Room FROM
(
SELECT
*
FROM
joindrslt PIVOT (
MAX ( roommate )
FOR ri
IN ( 1 AS roomate_1, 2 AS roomate_2, 3 AS roomate_3 )
)
)
ORDER BY
Primary,Roomate_1,Roomate_2,Roomate_3,Building,Suite,Room