0

网络,MYSQL 网络应用程序。我们已经创建了将返回表的存储过程,但现在我们想要更改该过程的结构以用于显示目的。那么如何使Mysql存储过程的TempTable结果为以下格式呢?行转换为列。

在此处输入图像描述

凤凰解决方案输出:

在此处输入图像描述

4

2 回答 2

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演示

于 2013-06-04T07:15:36.643 回答
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;

由于您没有提到您的实体表详细信息,我建议您按一些适当的字段对这些记录进行分组,您将获得所需的输出,否则您可以遵循@peterm 的解决方案。

一个 Fiddle FIDDLE示例

于 2013-06-04T06:15:38.080 回答