使用两列对数据进行旋转

时间:2021-02-25 08:20:03

I have data in pivoted format. It looks like this:

我有数据的中心格式。它看起来像这样:

-----------------------------------------
| 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    |
-----------------------------------------

I would like to have the data represented as:

我希望将数据表示为:

-------------------------------------------------------------------------------------
| 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        |
------------------------------------------------------------------------------------- 

I think that a pivot query with connect by command is needed.

我认为需要使用带connect by命令的pivot查询。


This is what I tried to do:

这就是我想做的:

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

However, I have no idea how to go forward.

然而,我不知道如何前进。

3 个解决方案

#1


11  

Here is a way to get the data in the format you want:

下面是一种获取数据的方式:

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

See SQL Fiddle with Demo

参见SQL小提琴演示

#2


47  

PIVOT should work fine - SQL Fiddle demo (schema borrowed from bluefeets answer)

PIVOT应该工作得很好——SQL Fiddle演示(从bluefeets的回答中借来的模式)

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

#3


4  

11gr2 PIVOT example

11 gr2主示例

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.

#1


11  

Here is a way to get the data in the format you want:

下面是一种获取数据的方式:

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

See SQL Fiddle with Demo

参见SQL小提琴演示

#2


47  

PIVOT should work fine - SQL Fiddle demo (schema borrowed from bluefeets answer)

PIVOT应该工作得很好——SQL Fiddle演示(从bluefeets的回答中借来的模式)

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

#3


4  

11gr2 PIVOT example

11 gr2主示例

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.