简体   繁体   English

如何使用SQL将垂直数据转换为水平数据?

[英]How to transform vertical data into horizontal data with SQL?

I have a table "Item" with a number of related items, like so: 我有一个表“项目”,其中包含许多相关项目,如下所示:

ID   Rel_ID  Name  RelRank
---  ------  ----  -------
1    1       foo   1
2    1       bar   2
3    1       zam   3
4    2       foo2  1

I'm trying to get a query so items with the same Rel_ID would appear in the same row, like so: 我正在尝试获取查询,因此具有相同Rel_ID的项目将出现在同一行中,如下所示:

Rel_ID  Name1  Name2  Name3
------  -----  -----  -----
1       foo    bar    zam
2       foo2

I've tried selecting the table multiple times: 我尝试过多次选择表格:

SELECT k.Rel_ID, k.name 'Name1', k2.name 'Name2'
FROM item k, item k2
WHERE k.Rel_ID = k2.Rel_ID

But this fails. 但这失败了。 Surely there's a transformation or query that could drastically simplify the process, and I'm just missing it because I haven't used SQL in this way before. 当然有一个转换或查询可以大大简化过程,我只是错过它,因为我之前没有以这种方式使用SQL。 What am I missing? 我错过了什么?

[Edit: added RelRank column, which does appear in my data] [编辑:添加了RelRank列,确实出现在我的数据中]

Regardless of the database you are using, the concept of what you are trying to achieve is called "Pivot Table". 无论您使用何种数据库,您尝试实现的概念都称为“数据透视表”。

Here's an example for mysql: http://en.wikibooks.org/wiki/MySQL/Pivot_table 这是mysql的一个例子: http//en.wikibooks.org/wiki/MySQL/Pivot_table

Some databases have builtin features for that, see the links below. 有些数据库具有内置功能,请参阅下面的链接。

SQLServer: http://msdn.microsoft.com/de-de/library/ms177410.aspx SQLServer: http//msdn.microsoft.com/de-de/library/ms177410.aspx

Oracle: http://www.dba-oracle.com/t_pivot_examples.htm Oracle: http//www.dba-oracle.com/t_pivot_examples.htm

You can always create a pivot by hand. 您始终可以手动创建枢轴。 Just select all the aggregations in a result set and then select from that result set. 只需选择结果集中的所有聚合,然后从该结果集中进行选择。 Note, in your case, you can put all the names into one column using concat (i think that's group_concat in mysql), since you cannot know how many names are related to aa rel_id. 请注意,在您的情况下,您可以使用concat将所有名称放入一列(我认为这是mysql中的group_concat),因为您无法知道有多少名称与aa rel_id相关。

pseudo-select for your case (i don't know mysql): 伪选择你的情况(我不知道mysql):

select rel_id, group_concat(name) from item group by rel_id

I think you are looking for a mysql specific answer. 我想你正在寻找一个特定于mysql的答案。 Keep in mind that the syntax could vary across different data stores. 请记住,语法可能因不同的数据存储而异。

MySQL has a feature that makes this easy. MySQL有一个功能,使这很容易。

SELECT Rel_ID, GROUP_CONCAT(Name SEPARATOR ' ') As Names FROM Item GROUP BY Rel_ID;

that should work :-) 应该工作:-)

if the names that you listed are static,my below query that i runned sucessfully in sqlfiddle will work 如果你列出的名字是静态的,我在sqlfiddle中成功运行的下面的查询将有效

SELECT rel_id,
MAX (DECODE (rel_id, '1', DECODE (relrank, '1', name) , '2',DECODE (relrank, '1', name))) NAME1,
MAX (DECODE (rel_id, '1', DECODE (relrank, '2', name))) NAME2,
MAX (DECODE (rel_id, '1', DECODE (relrank, '3', name))) NAME3
FROM supportContacts
GROUP BY rel_id

heres the SQL fiddle 继承人的SQL小提琴

http://sqlfiddle.com/#!4/480e2/11 http://sqlfiddle.com/#!4/480e2/11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM