简体   繁体   English

使用 SQL 旋转表

[英]Pivoting a table with SQL

I have a table with position (junior, senior), salary, and an ID.我有一张桌子,上面有 position(初级、高级)、薪水和身份证。 I have done the following to find the highest salary for each position.我做了以下查找每个 position 的最高薪水。

SELECT position, MAX(salary) FROM candidates GROUP BY position;

What I am getting:我得到了什么: 在此处输入图像描述

How I want it:我想要它: 在此处输入图像描述

I want to transpose the outcome so that 'junior' and 'senior' are the columns without using crosstab.我想转置结果,以便“初级”和“高级”是不使用交叉表的列。 I have looked at many pivot examples but they are done on examples much more complex than mine.我看过许多 pivot 示例,但它们是在比我的复杂得多的示例上完成的。

Here is my attempt at teaching myself crosstab:这是我自学交叉表的尝试:

CREATE EXTENSION IF NOT EXISTS tablefunc;

select Junior
  , Senior
from
(
  select *
  from crosstab
  (
    'select 1, position, max(salary)
    from candidates
    group by position
    '
    , $$VALUES('Junior'), ('Senior')$$
  )
  as ct(row_number integer, Junior integer, Senior integer) --I don't know your actual data types, so you will need to update this as needed
) q

Edit: Below is no longer relevant as this appears to be PostgreSQL编辑:下面不再相关,因为这似乎是 PostgreSQL

Based on your description, it sounds like you probably want a pivot like this:根据您的描述,听起来您可能想要这样的 pivot:

select q.*
from
(
    select position
        , salary
    from candidates
) q
pivot (
    max(salary) for position in ([Junior], [Senior])
) p

This example was made in SQL Server since we don't know DBMS.这个例子是在 SQL 服务器中制作的,因为我们不知道 DBMS。

I am not proficient in PostgreSQL, but I believe there is a practical workaround solution since this is a simple table:我不精通 PostgreSQL,但我相信有一个实用的解决方案,因为这是一个简单的表:

SELECT 
    max(case when position = 'senior' then salary else null end) senior,
    max(case when position = 'junior' then salary else null end) junior
FROM payments

It worked with this example:它适用于这个例子:

create table payments (id integer, position varchar(100), salary int);
        insert into payments (id, position, salary) values (1, 'junior', 1000);
        insert into payments (id, position, salary) values (1, 'junior', 2000);
        insert into payments (id, position, salary) values (1, 'junior', 5000);
        insert into payments (id, position, salary) values (1, 'junior', 3000);
        insert into payments (id, position, salary) values (2, 'senior', 3000);
        insert into payments (id, position, salary) values (2, 'senior', 8000);
        insert into payments (id, position, salary) values (2, 'senior', 9000);
        insert into payments (id, position, salary) values (2, 'senior', 7000);
        insert into payments (id, position, salary) values (2, 'senior', 4000);
        select 
            max(case when position = 'junior' then salary else 0 end) junior,
            max(case when position = 'senior' then salary else 0 end) senior
        from payments;

It depends on which SQL dialect you are running.这取决于您正在运行哪种 SQL 方言。 It also depends on the complexity of your table.它还取决于表的复杂性。 In SQL Server, I believe you can use the solutions provided in this question for relatively simple tables: Efficiently convert rows to columns in sql server在 SQL 服务器中,我相信您可以使用此问题中提供的解决方案来处理相对简单的表: Efficiently convert rows to columns in sql server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM