2012-08-16 67 views
13

我以透視格式顯示數據。它看起來像這樣:使用兩列數據透視

----------------------------------------- 
| user_id | org | position | lang | 
----------------------------------------- 
| 1001 | USE | Boss  | EN | 
| 1001 | USD | Bossa  | FI | 
| 1002 | GWR | Dim  | SV | 
| 1003 | GGA | DCS  | FI | 
| 1003 | GCA | DDD  | SV | 
----------------------------------------- 

我想有表示爲數據:

------------------------------------------------------------------------------------- 
| user_id | org_fi | position_fi | org_en | position_en | org_sv | position_sv | 
------------------------------------------------------------------------------------- 
| 1001 | USD | Bossa  | USE | Boss  |   |    | 
| 1002 |  |    |   |    | GWR  | Dim  | 
| 1003 | GGA | DCS  |   |    | GCA  | DDD  | 
------------------------------------------------------------------------------------- 

我認爲,需要通過命令連接樞軸查詢。


這就是我試圖做的:

SELECT user_id, 
    org, 
    position, 
    lang, 
    ROW_NUMBER() OVER (PARTITION BY lang, user_id ORDER BY ROWID) rn 
FROM source 

但是,我不知道如何前進。

+0

您是否嘗試過使用PIVOT命令? http://www.oracle.com/technetwork/articles/sql/11g-pivot-097235.html – tbone 2012-08-16 12:39:16

+0

是的,我試圖使用樞軸。但問題是,我得到1,2,3,4,5 ....在我的查詢價值,所以我現在有點卡住了,我怎麼能在這種情況下使用樞軸 – Jaanna 2012-08-16 12:49:46

+0

提供的示例提供的示例 – tbone 2012-08-16 13:59:14

回答

11

這是一種方式來獲得你想要的格式的數據:

SELECT user_id, 
    max(case when lang = 'FI' THEN org ELSE ' ' END) org_fi, 
    max(case when lang = 'FI' THEN position ELSE ' ' END) position_fi, 
    max(case when lang = 'EN' THEN org ELSE ' ' END) org_en, 
    max(case when lang = 'EN' THEN position ELSE ' ' END) position_en, 
    max(case when lang = 'SV' THEN org ELSE ' ' END) org_sv, 
    max(case when lang = 'SV' THEN position ELSE ' ' END) position_sv 
FROM source 
group by user_id 
order by user_id 

SQL Fiddle with Demo

+0

謝謝,幫助我:-) – Jaanna 2012-08-16 14:04:27

+0

在Oracle 10g中旋轉文本的好解決方案。 – Tykin 2017-02-22 15:44:38

44

PIVOT應該很好地工作 - SQL Fiddle demo(模式從bluefeets answer借來的)

SELECT * 
FROM source 
PIVOT (
     MIN(org) AS org, 
     MIN(position) AS position 
     FOR lang 
     IN('EN' AS en, 'FI' AS fi, 'SV' AS sv) 
    ); 
+7

+1我不知道你可以像這樣在兩列上旋轉。非常好 – Taryn 2012-08-16 14:01:36

+0

謝謝..這是相當有用的瞭解有關旋轉兩列。 – Jaanna 2012-08-16 14:03:58

+0

謝謝。我正在尋找這一個:)。 – 2013-09-06 03:24:48

4

11gr2 PIVOT示例

SQL> create table t1 
(
user_id number, 
org varchar2(10), 
position varchar2(10), 
lang varchar2(10) 
) 
Table created. 
SQL> insert into t1 values (1,'ABC','VVV','EN') 
1 row created. 
SQL> insert into t1 values (1,'DEF','WWW','FI') 
1 row created. 
SQL> insert into t1 values (2,'GHI','XXX','FI') 
1 row created. 
SQL> insert into t1 values (2,'JKL','YYY','SV') 
1 row created. 
SQL> insert into t1 values (3,'MNO','ZZZ','EN') 
1 row created. 
SQL> commit 
Commit complete. 
SQL> select * from t1 

    USER_ID ORG  POSITION LANG  
---------- ---------- ---------- ---------- 
     1 ABC  VVV  EN   
     1 DEF  WWW  FI  
     2 GHI  XXX  FI  
     2 JKL  YYY  SV  
     3 MNO  ZZZ  EN  

5 rows selected. 
SQL> select * from t1 
pivot(max(org) as org, max(position) as position for lang in ('EN' as "EN",'FI' as "FI",'SV' as "SV")) 

    USER_ID EN_ORG  EN_POSITION FI_ORG  FI_POSITION SV_ORG  SV_POSITION 
---------- ---------- ----------- ---------- ----------- ---------- ----------- 
     1 ABC  VVV   DEF  WWW        
     2      GHI  XXX   JKL  YYY  
     3 MNO  ZZZ              

3 rows selected.