Home > Database > Mysql Tutorial > How Can I Pivot User Data by Language Using Two Columns (Organization and Position)?

How Can I Pivot User Data by Language Using Two Columns (Organization and Position)?

DDD
Release: 2024-12-28 07:40:26
Original
817 people have browsed it

How Can I Pivot User Data by Language Using Two Columns (Organization and Position)?

Pivoting Data Using Two Columns

This question addresses the need to pivot data from a format where each row represents a user and their corresponding organization and position to a format where each row contains a user and their organizations and positions grouped by language.

The example provided shows that the original data is stored in a table where each row contains a user ID, organization, position, and language. The desired output is to have a table where each row contains a user ID, their organization and position for each language.

To achieve this, a pivot query can be used to transform the data. The query provided by the user is a good starting point, but it requires additional modifications to achieve the desired output.

The following modified query should work:

SELECT user_id, 
    MAX(CASE WHEN lang = 'EN' THEN org END) AS org_en, 
    MAX(CASE WHEN lang = 'EN' THEN position END) AS position_en, 
    MAX(CASE WHEN lang = 'FI' THEN org END) AS org_fi, 
    MAX(CASE WHEN lang = 'FI' THEN position END) AS position_fi, 
    MAX(CASE WHEN lang = 'SV' THEN org END) AS org_sv, 
    MAX(CASE WHEN lang = 'SV' THEN position END) AS position_sv
FROM source
GROUP BY user_id;
Copy after login

This query uses the CASE statement to compare the lang column to the desired languages and returns the corresponding organization or position if the condition is met. The MAX() function is then used to aggregate the results for each user ID.

The above is the detailed content of How Can I Pivot User Data by Language Using Two Columns (Organization and Position)?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template