如何取消在mysql中的动态数据透视表

时间:2019-05-09 06:54:25

标签: mysql-workbench

我创建了employee表,在其中按emp_profile分别对名称和薪金进行了分组。为此,我使用了动态数据透视表。现在我必须取消对数据透视表的透视。我为此使用了静态方法,但是我想对它使用动态透视表动态枢纽。

我在以下代码中使用的动态PIVOT

Select 
  group_concat(
    DISTINCT 
       if(emp_name is null,
          CONCAT('max(if (emp_name is null, salary, 0)) as ''NULL'' '),
          CONCAT('max(if (emp_name=''', emp_name, ''', salary, 0)) as ''',emp_name, ''' '))
    ) into @sql from employees join (SELECT @sql:='')a;
set @sql = concat('select emp_profile, ', @sql, 'from append_query.employees group by emp_profile;');
PREPARE stmt FROM @sql;
EXECUTE stmt;
               FOR UNPIVOT THE DYNAMIC PIVOT I USED BELOW CODE
create table temp_pivot(emp_profile text,Jyothi int,latha int,Mani int,Nagasri int,Navya int,Roopa int,Saritha int,Sneha int,Sushmitha int);
insert into temp_pivot values('data analyst',0,0,0,0,20000,15000,0,42000,25000),('data scientist',36000,60000,0,50000,0,0,0,0,0),('data engineering',0,0,16000,0,0,0,0,0,0),('code developer',0,0,0,0,0,0,23000,0,0);
select * from temp_pivot;  

create table unpivotdata(emp_profile text,emp_name char(20),salary int);
INSERT INTO unpivotdata SELECT emp_profile, 'Jyothi' col, Jyothi 
FROM temp_pivot
UNION ALL
SELECT emp_profile, 'Latha' col, Latha
FROM temp_pivot
UNION ALL
SELECT emp_profile, 'Mani' col, Mani
FROM temp_pivot
UNION ALL
SELECT emp_profile, 'Nagasri' col, Nagasri
FROM temp_pivot
UNION ALL
SELECT emp_profile, 'Navya' col, Navya
FROM temp_pivot
UNION ALL
SELECT emp_profile, 'Roopa' col, Roopa
FROM temp_pivot
UNION ALL
SELECT emp_profile, 'Saritha' col, Saritha
FROM temp_pivot
UNION ALL
SELECT emp_profile, 'Sneha' col, Sneha
FROM temp_pivot
UNION ALL
SELECT emp_profile, 'Sushmitha' col, Sushmitha
FROM temp_pivot;
select * from unpivotdata;
                          ouput
emp_profile emp_name    salary
data analyst    Jyothi  0
data scientist  Jyothi  36000
data engineering    Jyothi  0
code developer  Jyothi  0
data analyst    Latha   0
data scientist  Latha   60000
data engineering    Latha   0
code developer  Latha   0
data analyst    Mani    0
data scientist  Mani    0
data engineering    Mani    16000
code developer  Mani    0
data analyst    Nagasri 0
data scientist  Nagasri 50000
data engineering    Nagasri 0
code developer  Nagasri 0
data analyst    Navya   20000
data scientist  Navya   0
data engineering    Navya   0
code developer  Navya   0
data analyst    Roopa   15000
data scientist  Roopa   0
data engineering    Roopa   0
code developer  Roopa   0
data analyst    Saritha 0
data scientist  Saritha 0
data engineering    Saritha 0
code developer  Saritha 23000
data analyst    Sneha   42000
data scientist  Sneha   0
data engineering    Sneha   0
code developer  Sneha   0
data analyst    Sushmitha   25000
data scientist  Sushmitha   0
data engineering    Sushmitha   0
code developer  Sushmitha   0

我想像这样:

emp_profile emp_name salary
data analyst    Roopa   15000
data scientist  Jyothi  36000
data analyst    Navya   20000
data analyst  Sushmitha 25000
data scientist  latha   60000
data scientist  Nagasri 50000
data analyst    Sneha   42000
data engineering Mani   16000
Code developer  Saritha 23000

0 个答案:

没有答案