选择一行的行,在另一行选择中选择
现有查询返回下表:
UserId | 扇区 | 值 |
---|---|---|
1 | 1 1 | 111 |
1 | 2 | 122 |
1 | 3 | 133 |
2 | 2 | 222 2 |
2 2 | 2 2 2 3 | 233 |
3 | 1 | 311 |
,但我想以下面的方式“重新格式化”它:
UserId | Sector 1 Sector 1 | Sector 2 | Sector 2 Sector 3 |
---|---|---|---|
1 | 111 | 122 | 122 |
2 | 222 | 233 | |
3 | 311 |
最大扇区数量是可变的。由于我是SQL的新手,因此我不确定这是DB类型的特定物质,因此对PostgreSQL有效的解决方案表示赞赏。
如果这是数据库中不应该完成的事情,也可以。我仍在弄清楚在数据库中该怎么做,什么不做。
我知道标题不好。如果您有更精确的建议,请提出一些建议。
An existing query returns the following table:
UserID | Sector | Value |
---|---|---|
1 | 1 | 111 |
1 | 2 | 122 |
1 | 3 | 133 |
2 | 2 | 222 |
2 | 3 | 233 |
3 | 1 | 311 |
But I would like to "reformat" it in the following way:
UserID | Sector 1 | Sector 2 | Sector 3 |
---|---|---|---|
1 | 111 | 122 | 122 |
2 | 222 | 233 | |
3 | 311 |
The maximum number of sectors is variable. Since i am new to SQL I am not sure weather this would be something DB Type Specific, so a solution which works for PostgreSQL is appreciated.
If this is something which should not be done in the database, it is also okay. I am still figuring out what to do in the database and what not.
Title is not good, I know. Please make some recommendation if you have an more precise one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您无法具有动态数量的列数。
这是您提供的示例数据的SQL。您必须安装TableFunc扩展名。
Unfortunately you cannot have dynamic number of columns.
Here is the SQL for the sample data you've provided. You must have tablefunc extension installed.