SQL:将多行组合到基于非主键的单行
我有这样的表:
|Id | order_id | product_id | quantity |
|1 | A | 111 | 2 |
|2 | A | 112 | 3 |
|3 | A | 113 | 1 |
|4 | C | 112 | 2 |
|5 | B | 111 | 5 |
|6 | B | 112 | 1 |
我希望该表格为:
| order_id | quantity_111 | quantity_112 | quantity_113 |
| A | 2 | 3 | 1 |
| C | null | 2 | null |
| B | 5 | 1 | null |
在其中我们将使用产品ID制作数量列,我们只有3个固定的产品ID。
请帮助我找出SQL查询以实现此输出。
I've a table like this:
|Id | order_id | product_id | quantity |
|1 | A | 111 | 2 |
|2 | A | 112 | 3 |
|3 | A | 113 | 1 |
|4 | C | 112 | 2 |
|5 | B | 111 | 5 |
|6 | B | 112 | 1 |
And I'm expecting the table as:
| order_id | quantity_111 | quantity_112 | quantity_113 |
| A | 2 | 3 | 1 |
| C | null | 2 | null |
| B | 5 | 1 | null |
Where we will make quantity columns with product ids, we only have 3 fixed product ids.
Kindly help me figure out the SQL query to achieve this output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以做一个穷人的枢纽。例如:
You can do a poor man's pivot. For example: