假设我有下表:
+---------+------+-------------+-----+-------+--------+
| invoice | item | description | qty | price | amount |
+---------+------+-------------+-----+-------+--------+
| 1234 | L | labour | 1 | 50 | 50 |
| 1234 | P | parts | 2 | 100 | 200 |
| 9865 | L | labour | 1 | 25 | 25 |
| 9865 | P | parts | 1 | 25 | 25 |
| 5555 | P | parts | 2 | 100 | 200 |
+---------+------+-------------+-----+-------+--------+
我想要一个选择查询,它将 2 行转换为每个唯一发票编号的列,以便每个发票编号只有一行。
所以对于上面的例子,我期望以下:
+---------+-------+--------------+------+--------+---------+-------+--------------+------+--------+---------+
| invoice | item1 | description1 | qty1 | price1 | amount1 | item2 | description2 | qty2 | price2 | amount2 |
+---------+-------+--------------+------+--------+---------+-------+--------------+------+--------+---------+
| 1234 | L | labour | 1 | 50 | 50 | P | parts | 2 | 100 | 200 |
| 9865 | L | labour | 1 | 25 | 25 | P | parts | 1 | 25 | 25 |
| 5555 | P | parts | 2 | 100 | 200 | NULL | NULL | NULL | NULL | NULL |
+---------+-------+--------------+------+--------+---------+-------+--------------+------+--------+---------+
注意我正在寻找静态/预定义的列数,而不是基于行项目的动态。