我正在尝试创建一个 SQL 语句,以将可变数量的行(最多 5 行)转换为单行。一小部分数据如下所示:
+--------------+--------+-------+-------+
| ID | ItemID | Style | Qty |
+--------------+--------+-------+-------+
| 00001-5A-2XL | F911-5 | F911 | 1.566 |
| 00001-5A-2XL | 233-5 | 233 | 0.236 |
| 00001-5A-3XL | F911-5 | F911 | 1.697 |
| 00001-5A-3XL | 233-5 | 233 | 0.237 |
| 00001-5A-4XL | F911-5 | F911 | 1.833 |
| 00001-5A-4XL | 233-5 | 233 | 0.239 |
| 00001-5A-L | F911-5 | F911 | 1.307 |
| 00001-5A-L | 233-5 | 233 | 0.234 |
+--------------+--------+-------+-------+
我想将数据旋转为如下所示:
+--------------+------+--------+-----+--------+----+--------+----+--------+----+--------+
| ID | S1 | S1_Qty | S2 | S2_Qty | S3 | S3_Qty | S4 | S4_Qty | S5 | S5_Qty |
+--------------+------+--------+-----+--------+----+--------+----+--------+----+--------+
| 00001-5A-2XL | F911 | 1.566 | 233 | 0.236 | | | | | | |
| 00001-5A-3XL | F911 | 1.566 | 233 | 0.237 | | | | | | |
| 00001-5A-4XL | F911 | 1.566 | 233 | 0.239 | | | | | | |
| 00001-5A-L | F911 | 1.566 | 233 | 0.234 | | | | | | |
+--------------+------+--------+-----+--------+----+--------+----+--------+----+--------+
在进行旋转时,我有点迷茫,尤其是当我想将 theStyle
和 the都旋转Quantity
到我的列中时。最多有 5 种样式(行)需要翻译成最多 5 列。
任何建议,指针等将不胜感激。