[英]Joining multiple rows into a single row without aggregation using Oracle11g
下面的鏈接顯示了我想問的相同問題以及提供的用於回答查詢的代碼,我使用了這個查詢(修改了它)但是請教我如何在這個查詢中添加一個連接表和一個 where 子句? 請參閱下面我添加到以下鏈接中的代碼的代碼,但我收到錯誤消息。 我希望輸出與以下鏈接中提供的輸出相同
請記住,鏈接中提供的所需結果與我所需的結果相同。
下面是我修改過的腳本:
select max(case when seqnum = 1 then p.PERSONID end) as PersonID,
max(case when seqnum = 1 then t.PHONEID end) as PhoneID1,
max(case when seqnum = 1 then t.PHONENUM end) as PhoneNum1,
max(case when seqnum = 1 then t.TYPE end) as Type1,
max(case when seqnum = 1 then t.ISPRIMARY end) as IsPrimary1,
max(case when seqnum = 1 then t.ROWSTAMP1 end) as Rowstamp1,
max(case when seqnum = 2 then t.PHONEID end) as PhoneID2,
max(case when seqnum = 2 then t.PHONENUM end) as PhoneNum2,
max(case when seqnum = 2 then t.TYPE end) as Type2,
max(case when seqnum = 2 then t.ISPRIMARY end) as IsPrimary2,
max(case when seqnum = 2 then t.ROWSTAMP1 end) as Rowstamp2,
max(case when seqnum = 3 then t.PHONEID end) as PhoneID3,
max(case when seqnum = 3 then t.PHONENUM end) as PhoneNum3,
max(case when seqnum = 3 then t.TYPE end) as Type3,
max(case when seqnum = 3 then t.ISPRIMARY end) as IsPrimary3,
max(case when seqnum = 3 then t.ROWSTAMP1 end) as Rowstamp3
from (test1.phone t left join test2.person p
ON t1.PERSONID = t2.PERSONID
where t2.PERSONID = 'MXSDFD'
) t;
rownum as seqnum
from t
使用PIVOT
:
select personid,
"1_PHONEID" AS phoneid1,
"1_PHONENUM" AS phonenum1,
"1_TYPE" AS type1,
"1_ISPRIMARY" AS isprimary1,
"1_ROWSTAMP1" AS rowstamp1,
"2_PHONEID" AS phoneid2,
"2_PHONENUM" AS phonenum2,
"2_TYPE" AS type2,
"2_ISPRIMARY" AS isprimary2,
"2_ROWSTAMP1" AS rowstamp2,
"3_PHONEID" AS phoneid3,
"3_PHONENUM" AS phonenum3,
"3_TYPE" AS type3,
"3_ISPRIMARY" AS isprimary3,
"3_ROWSTAMP1" AS rowstamp3
from (
SELECT p.personid,
t.phoneid,
t.phonenum,
t.type,
t.isprimary,
t.rowstamp1,
ROW_NUMBER() OVER (
PARTITION BY p.personid ORDER BY t.isprimary DESC, t.rowstamp1 DESC
) AS seqnum
FROM person p
left join phone t
ON ( t.PERSONID = p.PERSONID )
where t.PERSONID = 'MXSDFD'
)
PIVOT (
MAX( phoneid ) AS phoneid,
MAX( phonenum ) AS phonenum,
MAX( type ) AS type,
MAX( isprimary ) AS isprimary,
MAX( rowstamp1 ) AS rowstamp1
FOR seqnum IN ( 1, 2, 3 )
)
其中,對於樣本數據:
CREATE TABLE person ( personid, name ) AS
SELECT 'MXSDFD', 'Alice' FROM DUAL;
CREATE TABLE phone ( personid, phonenum, phoneid, type, isprimary, rowstamp1 ) AS
SELECT 'MXSDFD', '012346', 1, 'A', 'N', TIMESTAMP '2020-10-13 09:00:00' FROM DUAL UNION ALL
SELECT 'MXSDFD', '555666', 3, 'C', 'N', TIMESTAMP '2020-10-13 08:30:00' FROM DUAL UNION ALL
SELECT 'MXSDFD', '987654', 2, 'B', 'Y', TIMESTAMP '2020-10-13 08:00:00' FROM DUAL;
輸出:
\n人物 | 電話號碼1 | 電話號碼1 | TYPE1 | ISPRIMARY1 | ROWSTAMP1 | 電話號碼2 | 電話號碼2 | 類型2 | ISPRIMARY2 | ROWSTAMP2 | PHONEID3 | 電話號碼3 | TYPE3 | ISPRIMARY3 | 行戳3 \n :------- | -------: | :-------- | :---- | :--------- | :--------------------------- | -------: | :-------- | :---- | :--------- | :--------------------------- | -------: | :-------- | :---- | :--------- | :---------------------------\n MXSDFD | 2 | 987654 | 乙 | 是 | 13-OCT-20 08.00.00.000000000 | 1 | 012346 | 一個 | 否 | 13-OCT-20 09.00.00.000000000 | 3 | 555666 | C | 否 | 13-OCT-20 08.30.00.000000000\n
db<> 在這里擺弄
你似乎想要這樣的邏輯:
select max(case when seqnum = 1 then t.PERSONID end) as PersonID,
max(case when seqnum = 1 then t.PHONEID end) as PhoneID1,
max(case when seqnum = 1 then t.PHONENUM end) as PhoneNum1,
max(case when seqnum = 1 then t.TYPE end) as Type1,
max(case when seqnum = 1 then t.ISPRIMARY end) as IsPrimary1,
max(case when seqnum = 1 then t.ROWSTAMP1 end) as Rowstamp1,
max(case when seqnum = 2 then t.PHONEID end) as PhoneID2,
max(case when seqnum = 2 then t.PHONENUM end) as PhoneNum2,
max(case when seqnum = 2 then t.TYPE end) as Type2,
max(case when seqnum = 2 then t.ISPRIMARY end) as IsPrimary2,
max(case when seqnum = 2 then t.ROWSTAMP1 end) as Rowstamp2,
max(case when seqnum = 3 then t.PHONEID end) as PhoneID3,
max(case when seqnum = 3 then t.PHONENUM end) as PhoneNum3,
max(case when seqnum = 3 then t.TYPE end) as Type3,
max(case when seqnum = 3 then t.ISPRIMARY end) as IsPrimary3,
max(case when seqnum = 3 then t.ROWSTAMP1 end) as Rowstamp3
from (select t.*, rownum as seqnum
from test1.phone t join
test2.person p
on t.PERSONID = p.PERSONID
where p.PERSONID = 'MXSDFD'
) t;
也就是說,您似乎根本不需要join
,因為您正在過濾連接鍵(它在兩個表中)。 所以:
from (select t.*, rownum as seqnum
from test1.phone t
where t.PERSONID = 'MXSDFD'
) t
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.