mySQL 5.5 - 在与 UNION 链接的两个表之后将值转为列 [英] mySQL 5.5 - pivot values to columns after two tables linked with UNION

查看:38
本文介绍了mySQL 5.5 - 在与 UNION 链接的两个表之后将值转为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有最后一个问题,我无法用我的 sql 代码进行排序.基本上我想透视表并以与此不同的格式显示:http://sqlfiddle.com/#!9/98436/1

I have a final issue that I can not get sorted with my sql code. Basically I want to pivot table and display in the different format to this one: http://sqlfiddle.com/#!9/98436/1

我尝试做的是旋转 LOC_ID 和 LOC_ID_b 以获得平面文件.问题是要让代码工作我按 LOC_ID 和 LOC_ID_b 分组,但不知道如何绕过它.

What I try to do is to pivot LOC_ID and LOC_ID_b to get flat file. The problme is that to get the code working I group by LOC_ID and LOC_ID_b and not sure how to get around it.

我尝试达到的结果如下:

The result I try to achieve is the following:

PN    AAA  q    AAA c   BBB q   BBB c   CCC q   CCC c
A1                      2       1       
RRR             1                               1
T1     1        1               
HHH    3                                3   

推荐答案

您可以使用条件聚合.如果您的已知 loc_id 数量有限,您可以像这样编码

You can use conditional aggregation. If you have a limited number of known loc_ids you could code like this

Select pn,
    max(case when src = 'h'  and loc_id = 'AAA' then val else 0 end) as AAA_qty,
    max(case when src = 'r'  and loc_id = 'AAA' then val else 0 end) as AAA_count,
    max(case when src = 'h'  and loc_id = 'BBB' then val else 0 end) as BBB_qty,
    max(case when src = 'r'  and loc_id = 'BBB' then val else 0 end) as BBB_count,
    max(case when src = 'h'  and loc_id = 'CCC' then val else 0 end) as CCC_qty,
    max(case when src = 'r'  and loc_id = 'CCC' then val else 0 end) as CCC_count 
    from (select 'h' as src, pn, loc_id, sum(qty) val from history group by src,pn,loc_id 
    union
    select 'r' as src, pn, loc_id, count(*) val from rota group by src,pn,loc_id
) s 
group by pn
order by pn;

如果您不知道或不想在添加或删除 loc_ids 时更改您的代码,您需要动态 sql

If you don't know or don't want to be change your code when loc_ids are added or deleted you need dynamic sql

set @sql = (
select concat( 
        'Select pn,',
        group_concat(
        concat('max(case when src = ' ,char(39),'h',char(39),'  and loc_id = ', char(39),loc_id,char(39), ' then val else 0 end) as ' , 
        concat(loc_id,'_qty,')
        ,
        'max(case when src = ' ,char(39),'r',char(39),'  and loc_id = ', char(39),loc_id,char(39), ' then val else 0 end) as ' , 
        concat(loc_id,'_count')
        )
        ) 

      ,' from ('
      ,
      'select ', char(39),'h',char(39),' as src, pn, loc_id, sum(qty) val from history group by src,pn,loc_id'
       ,
        ' union '
        , 
        'select ',char(39),'r',char(39),' as src, pn, loc_id, count(*) val from rota group by src,pn,loc_id
) s 
group by pn
order by pn;'
        )


from
(
select  loc_id from history
union
select loc_id from rota
) a
);

prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;

+-----+---------+-----------+---------+-----------+---------+-----------+
| pn  | AAA_qty | AAA_count | BBB_qty | BBB_count | CCC_qty | CCC_count |
+-----+---------+-----------+---------+-----------+---------+-----------+
| A1  |       0 |         0 |       2 |         1 |       0 |         0 |
| HHH |       0 |         0 |       0 |         0 |       3 |         0 |
| RRR |       0 |         0 |       0 |         0 |       0 |         1 |
| T1  |       1 |         1 |       0 |         0 |       0 |         0 |
+-----+---------+-----------+---------+-----------+---------+-----------+
4 rows in set (0.00 sec)

这篇关于mySQL 5.5 - 在与 UNION 链接的两个表之后将值转为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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