Net,MYSQL Web应用程序。 我们创建了将返回表的存储过程,但是现在为了显示目的想改变那个程序的结构。 那么如何使Mysql存储过程的TempTable结果采用以下格式? 行转换为列。
Phoenix Solution输出:
答案 0 :(得分:1)
试试这个sql
select max((CASE WHEN Agedreceivable='Current' then AgedeceivableAmt ELSE 0 END)) as Current,
max((CASE WHEN Agedreceivable='1-30' then AgedeceivableAmt ELSE 0 END)) as '1-30',
max((CASE WHEN Agedreceivable='31-60' then AgedeceivableAmt ELSE 0 END)) as '31-60',
max((CASE WHEN Agedreceivable='61-90' then AgedeceivableAmt ELSE 0 END)) as '61-90',
max((CASE WHEN Agedreceivable='>90' then AgedeceivableAmt ELSE 0 END)) as '>90'
from yourtable;
由于你没有提到你的entite表细节,我建议将这些记录分组到一些适当的字段,你将获得所需的输出,否则你可以关注@ peterm的解决方案。
示例小提琴FIDDLE
答案 1 :(得分:1)
尝试
SELECT
SUM(CASE WHEN Agedreceivable='Current' THEN AgedreceivableAmt END) Current,
SUM(CASE WHEN Agedreceivable='1-30' THEN AgedreceivableAmt END) '1-30',
SUM(CASE WHEN Agedreceivable='31-60' THEN AgedreceivableAmt END) '31-60',
SUM(CASE WHEN Agedreceivable='61-90' THEN AgedreceivableAmt END) '61-90',
SUM(CASE WHEN Agedreceivable='>90' THEN AgedreceivableAmt END) '>90'
FROM
(
SELECT @n := @n + 1 rownum, agedreceivable, agedreceivableamt
FROM temp, (SELECT @n := 0) z
) y
GROUP BY FLOOR((rownum - 1) / 5)
输出:
| CURRENT | 1-30 | 31-60 | 61-90 | >90 |
----------------------------------------
| 0 | 0 | 0 | 0 | 0 |
| 12993 | 0 | 0 | 0 | 0 |
这是 SQLFiddle 演示