将行转换为列

时间:2017-01-09 10:20:44

标签: sqlite pivot

假设我有2个表:test_runs,test_run_status

test_runs:

|id  |name|    imported_at     |
|----|----|--------------------|
|1   | A  | 2017-01-07 00:00:00|
|2   | B  | 2017-01-07 00:00:00|
|3   | A  | 2017-01-08 00:00:00|
|4   | B  | 2017-01-08 00:00:00|
|5   | A  | 2017-01-09 00:00:00|
|6   | B  | 2017-01-09 00:00:00|

test_run_status:

|test_run_id | passed | failed|
|------------|--------|-------|
|1           | 20     | 0     |
|2           | 5      | 0     | 
|3           | 19     | 1     | 
|4           | 5      | 0     |
|5           | 18     | 2     |
|6           | 4      | 1     |

写完一个简单的连接查询后,我得到了结果

Select test_runs.name ,test_run_status.passed date(test_runs.imported_at)
from test_runs , test_run_status
where test_runs.id=test_run_status.test_run_id
order by test_runs.source_file;
|Name | Passed    |   Date    |
|-----|-----------|-----------|
| A   | 20        | 2017-01-07|
| A   | 19        | 2017-01-08|
| A   | 18        | 2017-01-09|
| B   | 5         | 2017-01-07|
| B   | 5         | 2017-01-08|
| B   | 4         | 2017-01-09|

但我希望将数据显示为

|Name | 2017-01-09 | 2017-01-08 | 2017-01-07|
|-----|------------|------------|-----------|
| A   |     20     |    19      |   18      |
| B   |     5      |    5       |   4       |

0 个答案:

没有答案