你可以嘗試一些稍微不同的東西,用包含數據連接的單個列構建輸出。 假設每個用戶都有每一天完全是一個活動,你可以嘗試這樣的事情:
with users(user_id, user_name) as
(
select 1, 'naveen' from dual union all
select 2, 'richard' from dual union all
select 3, 'reddy' from dual
),
user_activity(user_id, Activity, activity_date) as
(
select 1, 'holiday' , date '2016-08-01' from dual union all
select 2, 'working' , date '2016-08-01' from dual union all
select 3, 'training' , date '2016-08-01' from dual union all
select 1, 'working' , date '2016-08-02' from dual union all
select 2, 'working' , date '2016-08-02' from dual union all
select 3, 'working' , date '2016-08-02' from dual
)
select rpad('Date', 16, ' ') || ',' || listagg(rpad(user_name, 16, ' '), ',') within group (order by user_id)
from users
union all
select rpad(to_char(activity_date, 'yyyy-mm-dd'),16, ' ') || ',' || listagg(rpad(activity, 16, ' '), ',') within group (order by user_id)
from users
inner join user_activity using(user_id)
group by activity_date
結果:
RPAD('DATE',16,'')||','||LISTAGG(RPAD(USER_NAME,16,''),',')WITHINGROUP(ORDERBYUS
--------------------------------------------------------------------------------
Date ,naveen ,richard ,reddy
2016-08-01 ,holiday ,working ,training
2016-08-02 ,working ,working ,working
如果我添加第四個男人:
RPAD('DATE',16,'')||','||LISTAGG(RPAD(USER_NAME,16,''),',')WITHINGROUP(ORDERBYUSER_ID)
----------------------------------------------------------------------------------------------------
Date ,naveen ,richard ,reddy ,Fourth Man
2016-08-01 ,holiday ,working ,training ,training
2016-08-02 ,working ,working ,working ,working
你試過什麼了?如果你有3個以上的用戶呢?你期望構建一個可變數量列的查詢嗎? – Aleksej
只是通過樞軸功能工作。但是,正如你告訴我,如果沒有用戶超過3個,我就無法創建。尋找最佳解決方案。 –
你不能建立一個簡單的SQL返回一個列數可變的結果。你可以構建一個動態SQL然後運行它,這會給出可變數量的列,但是如何處理這個結果呢?其他方式,您可以使用包含值的串聯的單個列與一些分隔符構建結果 – Aleksej