SQL 将行转置为列
我有一个有趣的难题,我相信可以用纯 SQL 来解决。我有类似于以下的表格:
responses:
user_id | question_id | body
----------------------------
1 | 1 | Yes
2 | 1 | Yes
1 | 2 | Yes
2 | 2 | No
1 | 3 | No
2 | 3 | No
questions:
id | body
-------------------------
1 | Do you like apples?
2 | Do you like oranges?
3 | Do you like carrots?
我想得到以下输出
user_id | Do you like apples? | Do you like oranges? | Do you like carrots?
---------------------------------------------------------------------------
1 | Yes | Yes | No
2 | Yes | No | No
我不知道会有多少问题,而且它们将是动态的,所以我不能只为每个问题编写代码。我正在使用 PostgreSQL,我相信这称为转置,但我似乎找不到任何说明在 SQL 中执行此操作的标准方法的内容。我记得在大学的数据库课上做过这件事,但那是在 MySQL 中,老实说我不记得我们是如何做到的。
我假设它将是联接和 GROUP BY 语句的组合,但我什至不知道如何开始。
有人知道该怎么做吗?非常感谢!
编辑1:我找到了一些有关使用交叉表<的信息/a> 这似乎是我想要的,但我无法理解它。更好文章的链接将不胜感激!
I have an interesting conundrum which I believe can be solved in purely SQL. I have tables similar to the following:
responses:
user_id | question_id | body
----------------------------
1 | 1 | Yes
2 | 1 | Yes
1 | 2 | Yes
2 | 2 | No
1 | 3 | No
2 | 3 | No
questions:
id | body
-------------------------
1 | Do you like apples?
2 | Do you like oranges?
3 | Do you like carrots?
and I would like to get the following output
user_id | Do you like apples? | Do you like oranges? | Do you like carrots?
---------------------------------------------------------------------------
1 | Yes | Yes | No
2 | Yes | No | No
I don't know how many questions there will be, and they will be dynamic, so I can't just code for every question. I am using PostgreSQL and I believe this is called transposition, but I can't seem to find anything that says the standard way of doing this in SQL. I remember doing this in my database class back in college, but it was in MySQL and I honestly don't remember how we did it.
I'm assuming it will be a combination of joins and a GROUP BY
statement, but I can't even figure out how to start.
Anybody know how to do this? Thanks very much!
Edit 1: I found some information about using a crosstab which seems to be what I want, but I'm having trouble making sense of it. Links to better articles would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用途:
这是标准的透视查询,因为您要将数据从行“透视”为列数据。
Use:
This is a standard pivot query, because you are "pivoting" the data from rows to columnar data.
我实现了一个真正的动态函数来处理这个问题,而无需对任何特定类别的答案进行硬编码或使用外部模块/扩展。它还可以完全控制列排序并支持多个键和类/属性列。
您可以在这里找到它: https://github.com/jumpstarter-io/colpivot
示例解决了这个特定的问题:
输出:
I implemented a truly dynamic function to handle this problem without having to hard code any specific class of answers or use external modules/extensions. It also gives full control over column ordering and supports multiple key and class/attribute columns.
You can find it here: https://github.com/jumpstarter-io/colpivot
Example that solves this particular problem:
This outputs:
您可以使用 crosstab 函数以这种方式解决此示例
首先,您必须安装 tablefunc 扩展。从 9.1 版本开始,您可以使用创建扩展来完成此操作:
You can solve this example with the crosstab function in this way
First, you must install tablefunc extension. Since 9.1 version you can do it using create extension:
我编写了一个函数来生成动态查询。
它为交叉表生成 sql 并创建一个视图(如果存在,则首先删除它)。
然后您可以从视图中进行选择以获得结果。
这是函数:
这是我如何使用它:
示例:
你奔跑的拳头:
比:
I wrote a function to generate the dynamic query.
It generates the sql for the crosstab and creates a view (drops it first if it exists).
You can than select from the view to get your results.
Here is the function:
And here is how I use it:
Example:
Fist you run:
Than:
contrib/tablefunc/
中有一个这样的示例。There is an example of this in
contrib/tablefunc/
.