如何将列结果转换为 SQL 中的列 headers?
How do i turn column results into column headers in SQL?
我有以下 tables:
Sample
id PK
Test
id PK
name varchar
Section
id PK
name varchar
TestSection
id PK
test_id FK
section_id FK
SampleTestResult
id PK
sample_id FK
test_section_id FK
result
当我执行以下查询时:
SELECT DISTINCT
s.id as sample_id, ts.name as test, str.result
FROM sample s
JOIN sample_test_result str ON s.id=str.sample_id
JOIN test_section ts ON str.test_section_id=ts.id
JOIN section sec ON ts.section_id=sec.id
我得到一个 table,看起来像这样:
| sample_id | test | result |
--------------------------------------
| 1 | t1 | fail |
| 1 | t2 | pos |
| 2 | t1 | neg |
| 2 | t3 | rpt |
| 3 | t2 | pos |
| 3 | t4 | pos |
不过我想以这样的 table 结束:
| sample_id | t1 | t2 | t3 | t4 |
--------------------------------------------
| 1 | fail | pos | NULL | NULL |
| 2 | neg | NULL | rpt | NULL |
| 3 | NULL | pos | NULL | pos |
如何转置 SQL 中的 table - 这在查询中可行吗?还是必须是 sql 视图?
使用MySQL,解决这个问题的典型方法是使用条件聚合 :
SELECT
s.id as sample_id,
MAX(CASE WHEN ts.name = 't1' THEN str.result END) as t1,
MAX(CASE WHEN ts.name = 't2' THEN str.result END) as t2,
MAX(CASE WHEN ts.name = 't3' THEN str.result END) as t3,
MAX(CASE WHEN ts.name = 't4' THEN str.result END) as t4
FROM
sample s
JOIN sample_test_result str ON s.id=str.sample_id
JOIN test_section ts ON str.test_section_id=ts.id
JOIN section sec ON ts.section_id=sec.id
GROUP BY s.id
您需要条件聚合:
SELECT s.id as sample_id,
MAX(CASE WHEN ts.name = 't1' THEN str.result END) as t1,
. . .
FROM sample s JOIN
sample_test_result str
ON s.id=str.sample_id JOIN
test_section ts
ON str.test_section_id=ts.id
JOIN section sec ON ts.section_id=sec.id
GROUP BY s.id;
我有以下 tables:
Sample
id PK
Test
id PK
name varchar
Section
id PK
name varchar
TestSection
id PK
test_id FK
section_id FK
SampleTestResult
id PK
sample_id FK
test_section_id FK
result
当我执行以下查询时:
SELECT DISTINCT
s.id as sample_id, ts.name as test, str.result
FROM sample s
JOIN sample_test_result str ON s.id=str.sample_id
JOIN test_section ts ON str.test_section_id=ts.id
JOIN section sec ON ts.section_id=sec.id
我得到一个 table,看起来像这样:
| sample_id | test | result |
--------------------------------------
| 1 | t1 | fail |
| 1 | t2 | pos |
| 2 | t1 | neg |
| 2 | t3 | rpt |
| 3 | t2 | pos |
| 3 | t4 | pos |
不过我想以这样的 table 结束:
| sample_id | t1 | t2 | t3 | t4 |
--------------------------------------------
| 1 | fail | pos | NULL | NULL |
| 2 | neg | NULL | rpt | NULL |
| 3 | NULL | pos | NULL | pos |
如何转置 SQL 中的 table - 这在查询中可行吗?还是必须是 sql 视图?
使用MySQL,解决这个问题的典型方法是使用条件聚合 :
SELECT
s.id as sample_id,
MAX(CASE WHEN ts.name = 't1' THEN str.result END) as t1,
MAX(CASE WHEN ts.name = 't2' THEN str.result END) as t2,
MAX(CASE WHEN ts.name = 't3' THEN str.result END) as t3,
MAX(CASE WHEN ts.name = 't4' THEN str.result END) as t4
FROM
sample s
JOIN sample_test_result str ON s.id=str.sample_id
JOIN test_section ts ON str.test_section_id=ts.id
JOIN section sec ON ts.section_id=sec.id
GROUP BY s.id
您需要条件聚合:
SELECT s.id as sample_id,
MAX(CASE WHEN ts.name = 't1' THEN str.result END) as t1,
. . .
FROM sample s JOIN
sample_test_result str
ON s.id=str.sample_id JOIN
test_section ts
ON str.test_section_id=ts.id
JOIN section sec ON ts.section_id=sec.id
GROUP BY s.id;