需要有关在具有多个日期列的 mysql 中 unpivot 的帮助 [英] Need help on unpivot in mysql with multiple date columns

查看:31
本文介绍了需要有关在具有多个日期列的 mysql 中 unpivot 的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的意见.我想看到我的输出:(我试图在 Domo 中的 mysql 数据转换中进行逆透视)

Here is my input. I want to see my output like : (I am trying to do unpivot in mysql data transform in Domo)

Product Type, Date, Revenue

A, 12-31-2015, 100
B, 12-31-2015, 0
C, 12-31-2015, 200
D, 12-31-2015, 300
E, 12-31-2015, 400 
A, 01-31-2016, 400
B, 01-31-2016, 86.88
C, 01-31-2016, 400 
D, 01-31-2016, 55
E, 01-31-2016, 455

等等

我正在尝试使用存储过程:

I am trying to use stored procedure:

create PROCEDURE unpivot_cols()
BEGIN
SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT
CONCAT('select `Product Type`, ' '''', COLUMN_NAME, ''' col, ', column_name, ' as value from testing_unpivot'
) separator ' union all '
) INTO @sql
FROM INFORMATION_SCHEMA.COLUMNS
where table_name = 'testing_unpivot'
and column_name <> 'Product Type';
set @sql = CONCAT(@sql);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END

当我尝试执行上述过程时,我看不到任何输出.谁能帮我?PS:我尝试使用堆栈溢出的所有可用帮助.但非对我来说是有帮助的.

When i try to execute above procedure , I see no output. Can anyone help me? PS: I tried using all help available on stack overflow. But non were helpful in my case.

推荐答案

我在 MySQL 8.0.1 上测试了以下内容:

I tested the following on MySQL 8.0.1:

SELECT GROUP_CONCAT(
  CONCAT(
    'SELECT `Product Type`, ', 
       QUOTE(COLUMN_NAME), ' AS `Date`, ',
       '`', COLUMN_NAME, '` AS `Revenue` ',
    'FROM testing_unpivot'
  ) SEPARATOR ' UNION ALL '
)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'testing_unpivot'
AND COLUMN_NAME <> 'Product Type';

使用QUOTE() 函数 使它更简单,因为它消除了计算三引号和四引号的需要.

Using the QUOTE() function makes it simpler, because it eliminates the need to figure out the triple vs. quad quotes.

输出:

SELECT `Product Type`, '01-31-2016' AS `Date`, `01-31-2016` AS `Revenue` FROM testing_unpivot 
UNION ALL 
SELECT `Product Type`, '02-29-2016' AS `Date`, `02-29-2016` AS `Revenue` FROM testing_unpivot 
UNION ALL 
SELECT `Product Type`, '12-31-2015' AS `Date`, `12-31-2015` AS `Revenue` FROM testing_unpivot

然后我做了复制&粘贴以运行该查询并获得此输出:

Then I did copy & paste to run that query and got this output:

+--------------+------------+---------+
| Product Type | Date       | Revenue |
+--------------+------------+---------+
| A            | 01-31-2016 |  400.00 |
| B            | 01-31-2016 |   86.88 |
| C            | 01-31-2016 |  400.00 |
| D            | 01-31-2016 |   55.00 |
| E            | 01-31-2016 |  455.00 |
| A            | 02-29-2016 |   55.00 |
| B            | 02-29-2016 |   55.00 |
| C            | 02-29-2016 |   55.00 |
| D            | 02-29-2016 |   11.00 |
| E            | 02-29-2016 |   22.00 |
| A            | 12-31-2015 |  100.00 |
| B            | 12-31-2015 |    0.00 |
| C            | 12-31-2015 |  200.00 |
| D            | 12-31-2015 |  300.00 |
| E            | 12-31-2015 |  400.00 |
+--------------+------------+---------+

我刚刚用前三个日期的数据加载了我的测试数据.

I just loaded my test data with your first three dates worth of data.

这篇关于需要有关在具有多个日期列的 mysql 中 unpivot 的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆