简体   繁体   English

将行转置为列

[英]Transposing Rows to Columns

I am trying to come up with a single result set from two tables in a way that I have never done, and I am having a little bit of trouble figuring out how to do it or even what to search for in these forums. 我试图以从未有过的方式从两个表中得出一个结果集,而在这些论坛中弄清楚如何做甚至搜索什么时,我遇到了一些麻烦。 Consider the following hypothetical table data: 考虑以下假设表数据:

Table1
----------------------------------
ID  |  Name
----------------------------------
1   |  aa
2   |  bb
3   |  cc
4   |  dd
5   |  ee


Table2
----------------------------------
ID  |  Table1_ID  |  Value
----------------------------------
1   |  1          |  good
2   |  2          |  Dumb
3   |  3          |  Fat
4   |  4          |  Wet
5   |  5          |  High
6   |  1          |  Thin
7   |  2          |  Tall
8   |  3          |  Goofy
9   |  4          |  Rich
10  |  5          |  Funny

I am looking for a query or method that allows me to end up with the following result set: Code: 我正在寻找一个查询或方法,使我最终可以得到以下结果集:代码:

aa      |    bb     |    cc       |    dd      |    ee
---------------------------------------------------------------
good     |    Dumb   |    Fat     |    Wet     |    High
Thin     |    Tall   |    Goofy   |    Rich    |    Funny

Essentially, I want the ability to take the list of names from Table1, transpose them into column headers, then put all of Table2's values into their respective columns with the ability to sort on any column. 本质上,我希望能够从Table1中获取名称列表,将它们转置为列标题,然后将Table2的所有值放入各自的列中,并能够对任何列进行排序。 Is this possible? 这可能吗?

Of course this can be done in SQL. 当然,这可以在SQL中完成。 But it is tricky. 但这很棘手。 As the data is written in the question, you can group by t2.id in groups of 5. After that, the query is just conditional aggregation. 由于数据是写在问题中的,因此您可以按5个组将t2.id分组。之后,查询只是条件聚合。

select max(case when t2.table1_Id = 1 then value end) as aa,
       max(case when t2.table1_Id = 2 then value end) as bb,
       max(case when t2.table1_Id = 3 then value end) as cc,
       max(case when t2.table1_Id = 4 then value end) as dd,
       max(case when t2.table1_Id = 5 then value end) as ee
from table2 t2 
group by cast(t2.id - 1 / 5 as int);

Having values be implicitly related by their id s seems like a really, really bad database design. 使值通过其id隐式关联似乎是一个非常非常糟糕的数据库设计。 There should be some sort of entity id that combines them. 应该有某种将它们结合在一起的实体ID。

You've got two problems here: 1) Using values as column names can't be done in a clean way 2) You want to split table2.value in 2 rows: Which of the values should be on which row? 这里有两个问题:1)不能以干净的方式使用值作为列名2)您想将table2.value分成两行:哪一个值应该在哪一行? Gordon Linoff uses the table2.id field for this, but if it's auto increment and your data gets some adds/deletes later on that rhythm will get broken. Gordon Linoff为此使用了table2.id字段,但是如果它是自动递增的,并且稍后您的数据得到一些添加/删除,该节奏将被破坏。

There's been similar questions before. 以前也有类似的问题。 This one has an answer that gets pretty close: 这个答案非常接近:

mysql select dynamic row values as column names, another column as value mysql选择动态行值作为列名,另一列作为值

Here they generate the string for the query and make a prepared statement out of it. 在这里,它们为查询生成字符串,并从中生成准备好的语句。

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

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