MySQL将行透视成动态列数


81

可以说我有三个不同的MySQL表:

products

id | name
 1   Product A
 2   Product B

partners

id | name
 1   Partner A
 2   Partner B

sales

partners_id | products_id
          1             2
          2             5
          1             5
          1             3
          1             4
          1             5
          2             2
          2             4
          2             3
          1             1

我想得到一个表格,其中行和产品列为合作伙伴。到目前为止,我已经能够获得如下输出:

name      | name      | COUNT( * )
Partner A   Product A          1
Partner A   Product B          1
Partner A   Product C          1
Partner A   Product D          1
Partner A   Product E          2
Partner B   Product B          1
Partner B   Product C          1
Partner B   Product D          1
Partner B   Product E          1

使用此查询:

SELECT partners.name, products.name, COUNT( * ) 
FROM sales
JOIN products ON sales.products_id = products.id
JOIN partners ON sales.partners_id = partners.id
GROUP BY sales.partners_id, sales.products_id
LIMIT 0 , 30

但我想改成这样:

partner_name | Product A | Product B | Product C | Product D | Product E
Partner A              1           1           1           1           2
Partner B              0           1           1           1           1

问题是我无法确定我将拥有多少个产品,因此列号需要根据产品表中的行动态更改。

这个很好的答案似乎不适用于mysql:T-SQL Pivot吗?根据行值创建表格列的可能性


请参阅链接“行到列”以获取多个建议。
Bhavin Pokiya的


@BhavinPokiya是您提供的MS SQL-Server链接,而被标记为MySQL。
MattSom

Answers:


107

不幸的是,MySQL没有PIVOT您基本上想做的功能。因此,您将需要使用带有以下CASE语句的聚合函数:

select pt.partner_name,
  count(case when pd.product_name = 'Product A' THEN 1 END) ProductA,
  count(case when pd.product_name = 'Product B' THEN 1 END) ProductB,
  count(case when pd.product_name = 'Product C' THEN 1 END) ProductC,
  count(case when pd.product_name = 'Product D' THEN 1 END) ProductD,
  count(case when pd.product_name = 'Product E' THEN 1 END) ProductE
from partners pt
left join sales s
  on pt.part_id = s.partner_id
left join products pd
  on s.product_id = pd.prod_id
group by pt.partner_name

请参阅SQL演示

由于您不了解产品,因此您可能希望动态执行此操作。这可以使用准备好的语句来完成。

使用动态数据透视表(将行转换为列),您的代码将如下所示:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'count(case when Product_Name = ''',
      Product_Name,
      ''' then 1 end) AS ',
      replace(Product_Name, ' ', '')
    )
  ) INTO @sql
from products;

SET @sql = CONCAT('SELECT pt.partner_name, ', @sql, ' from partners pt
left join sales s
  on pt.part_id = s.partner_id
left join products pd
  on s.product_id = pd.prod_id
group by pt.partner_name');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

请参阅SQL演示

可能值得注意的GROUP_CONCAT是,默认情况下限制为1024个字节。您可以通过在整个过程中将其设置得更高一些来解决此问题。SET @@group_concat_max_len = 32000;


2
最后有关“ max_len”的评论是救生员!谢谢你的提示。
爱德华

1
如果您正在寻找更多动态枢轴查询,请检查以下内容:boynux.com/creating-pivot-reports-in-mysql
Boynux 2013年

如果产品名称为ProductA') from partners pt; truncate partners;
avatarofhope2 '19

@ avatarofhope2这是一个问题还是暗示?如果这给出了喷射角度,则正确的处理方法是什么?
MattSom
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.