Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivoting of data using two columns

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.


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.

like image 228
Jaanna Avatar asked Aug 16 '12 12:08

Jaanna


People also ask

Can you pivot 2 columns in SQL?

SQL Pivot Multiple Columns :You can use the SQL Pivot statement to transpose multiple columns.

Can you combine two sets of data in a pivot table?

You can use the PivotTable and PivotChart Wizard to consolidate multiple ranges. In the wizard, you can choose between using no page fields, a single page field, or multiple page fields.

How do I sort by two columns in a pivot table?

In a PivotTable, click the small arrow next to Row Labels and Column Labels cells. Click a field in the row or column you want to sort. on Row Labels or Column Labels, and then click the sort option you want. To sort data in ascending or descending order, click Sort A to Z or Sort Z to A.


2 Answers

PIVOT should work fine - SQL Fiddle demo (schema borrowed from 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)
      );
like image 153
Peter Lang Avatar answered Oct 20 '22 10:10

Peter Lang


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

like image 37
Taryn Avatar answered Oct 20 '22 11:10

Taryn