[英]transposing mysql temporary table rows to columns
I would like to transpose this table 我想换这张桌子
A | B | C ---------------- 1 | 10 | "XXX" 1 | 20 | "YYY" 2 | 40 | "XXX" 2 | 50 | "YYY" 3 | 10 | "XXX" 3 | 20 | "YYY" 3 | 60 | "ZZZ"
to this: 对此:
A | XXX | YYY | ZZZ | ---------------------- 1 | 10 | 20 | 0 | 2 | 40 | 50 | 0 | 3 | 10 | 20 | 60 |
I can only use temporary table and I've read in the help that self-referencing does not work. 我只能使用临时表,并且已经阅读了有关自动引用不起作用的帮助。 https://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html Any help?
https://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html有什么帮助吗?
If you know the values in column C
, then you can use conditional aggregation: 如果知道
column C
的值,则可以使用条件聚合:
select A,
coalesce(max(case when c = 'XXX' then B end),0) XXX,
coalesce(max(case when c = 'YYY' then B end),0) YYY,
coalesce(max(case when c = 'ZZZ' then B end),0) ZZZ
from yourtable
group by A
If not and you don't know the number of potential columns, then you'll need to use dynamic sql
. 如果不是,并且您不知道潜在的列数,则需要使用
dynamic sql
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.