mysql 查询中的数据透视表

发布于 2024-11-09 15:35:01 字数 1681 浏览 0 评论 0原文

我们有下面给出的查询,输出如下:

mysql> select count(1) as cnt ,card.Program_ID program_id ,fd.load_type load_type ,
    -> if(fd.load_type =0 ,concat('unload_cl_proram_',card.program_id) ,concat('load_cl_proram_',card.program_id)) as load_desc
    -> from transaction_log_details fd inner join card card
    -> on fd.Card_ID = card.Card_ID where card.Date_Created>='2011-01-01' and card.Date_Created<='2011-06-01'
    -> and card.Program_ID in (4,5,6,7,8)
    -> group by card.Program_ID,fd.load_type;
+-----+------------+-----------+--------------------+
| cnt | program_id | load_type | load_desc          |
+-----+------------+-----------+--------------------+
|   5 |          4 | 0         | unload_cl_proram_4 |
|  14 |          4 | 1         | load_cl_proram_4   |
|  55 |          5 | 0         | unload_cl_proram_5 |
|  91 |          5 | 1         | load_cl_proram_5   |
|  13 |          6 | 0         | unload_cl_proram_6 |
|  29 |          6 | 1         | load_cl_proram_6   |
| 175 |          7 | 0         | unload_cl_proram_7 |
| 411 |          7 | 1         | load_cl_proram_7   |
|  61 |          8 | 0         | unload_cl_proram_8 |
| 161 |          8 | 1         | load_cl_proram_8   |
+-----+------------+-----------+--------------------+
10 rows in set (0.00 sec)


Now we want the output as follows: (some thing like pivot).
unload_cl_proram_4  load_cl_proram_4    unload_cl_proram_5  load_cl_proram_5    unload_cl_proram_6  load_cl_proram_6    unload_cl_proram_7  load_cl_proram_7    unload_cl_proram_8  load_cl_proram_8    
5               14          55          91          13          29        175   411 61  161 

We have the query which is given below and the output is as follows:

mysql> select count(1) as cnt ,card.Program_ID program_id ,fd.load_type load_type ,
    -> if(fd.load_type =0 ,concat('unload_cl_proram_',card.program_id) ,concat('load_cl_proram_',card.program_id)) as load_desc
    -> from transaction_log_details fd inner join card card
    -> on fd.Card_ID = card.Card_ID where card.Date_Created>='2011-01-01' and card.Date_Created<='2011-06-01'
    -> and card.Program_ID in (4,5,6,7,8)
    -> group by card.Program_ID,fd.load_type;
+-----+------------+-----------+--------------------+
| cnt | program_id | load_type | load_desc          |
+-----+------------+-----------+--------------------+
|   5 |          4 | 0         | unload_cl_proram_4 |
|  14 |          4 | 1         | load_cl_proram_4   |
|  55 |          5 | 0         | unload_cl_proram_5 |
|  91 |          5 | 1         | load_cl_proram_5   |
|  13 |          6 | 0         | unload_cl_proram_6 |
|  29 |          6 | 1         | load_cl_proram_6   |
| 175 |          7 | 0         | unload_cl_proram_7 |
| 411 |          7 | 1         | load_cl_proram_7   |
|  61 |          8 | 0         | unload_cl_proram_8 |
| 161 |          8 | 1         | load_cl_proram_8   |
+-----+------------+-----------+--------------------+
10 rows in set (0.00 sec)


Now we want the output as follows: (some thing like pivot).
unload_cl_proram_4  load_cl_proram_4    unload_cl_proram_5  load_cl_proram_5    unload_cl_proram_6  load_cl_proram_6    unload_cl_proram_7  load_cl_proram_7    unload_cl_proram_8  load_cl_proram_8    
5               14          55          91          13          29        175   411 61  161 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文