Home >Database >Mysql Tutorial >How Can SQL's PIVOT Function Unpivot User Data Across Multiple Languages?

How Can SQL's PIVOT Function Unpivot User Data Across Multiple Languages?

DDD
DDDOriginal
2024-12-30 13:30:10538browse

How Can SQL's PIVOT Function Unpivot User Data Across Multiple Languages?

Pivoting Data Using Two Columns

In the provided data, each user has multiple rows representing their organizations and positions based on language. The desired output is to have the data unpivoted, presenting user-specific information such as organization and position for each supported language.

To achieve this, SQL's PIVOT function can be employed as follows:

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

In this query:

  • source is the table where our data resides.
  • lang is specified as the pivoting column.
  • MIN(org) and MIN(position) aggregate the minimum values for each user-language combination.
  • 'EN' AS en, 'FI' AS fi, and 'SV' AS sv' create the pivoted columns for English, Finnish, and Swedish languages, respectively.

This query will pivot the data, producing the desired output by presenting user and language-specific details in separate columns.

The above is the detailed content of How Can SQL's PIVOT Function Unpivot User Data Across Multiple Languages?. For more information, please follow other related articles on the PHP Chinese website!

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