douvcpx6526 2016-06-10 13:14
浏览 60

将多个列组合成一个长列

I've looked on the boards and googled a bunch, but I cant find a scenario that fits mine. I have 4 columns

+--------+------+------+
| empnum | appn | rate |
+--------+------+------+
|   13   | 1111 |12.34 |
+--------+------+------+
|   13   | 2222 |14.44 |
+--------+------+------+
|   13   | 3333 |15.62 |
+--------+------+------+
|   13   | 4444 |16.12 |
+--------+------+------+

each column has the same employee number but different information. I'm trying to combine these 4 columns into 1. The only thing I could find was concatenation. But that won't work for me because these need to go into an array so I can build it in datatables. I would like it to look like this.

+------+-----+-----+-----+-----+-----+-----+-----+-----+
|empnum|appn1|rate1|appn2|rate2|appn3|rate3|appn4|rate4|
+------+-----+-----+-----+-----+-----+-----+-----+-----+
|  13  |1111 |12.34|2222 |14.44|3333 |15.62|4444 |16.12|
+------+-----+-----+-----+-----+-----+-----+-----+-----+

As of a week ago all of this information was coming from a flat file. A coworker created a relational table and that's when my brain exploded. We are working on Db2 and I tried to do this in php using a foreach to loop through the array and look for the empnum but I would get the same appn1 for every person. I'd like to do it sql I just can't think of how.

  • 写回答

2条回答 默认 最新

  • dongpan1308 2016-06-10 13:39
    关注

    What you want is to Pivot your data. DB2 doesn't have a PIVOT function, but you can use DECODE to achieve this. Here's some examples in another question.

    The result set would look something like:

    +------+-----+-----+-----+-----+-----+-----+
    |empnum|appn1111|appn2222|appn3333|appn4444|
    +------+--------+--------+--------+--------+
    |  13  |12.34   |14.44   |15.62   |16.12   |
    +------+--------+--------+--------+--------+
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)