作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我已经查看了很多关于旋转的搜索,并且我进行了旋转的第一部分,但我很难弄清楚如何进行第二和第三旋转。我将在当前状态和一些解释之后显示最终目标表
我的数据是运行 HTML 片段时输出的内容。
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
</head>
<body bgcolor="#FFFFFF">
<table border="1">
<tr>
<td>ID</td>
<td>Title</td>
<td>5ba. Initiation Phase Start Date Planned</td>
<td>5bb. Initiation Phase Start Date Actual</td>
<td>5bc. Initiation Phase End Date Planned</td>
<td>5bd. Initiation Phase End Date Actual</td>
<td>5ca. Preliminary Design Phase Start Date Planned</td>
<td>5cb. Preliminary Design Phase Start Date Actual</td>
<td>5cc. Preliminary Design Phase End Date Planned</td>
<td>5cd. Preliminary Design Phase End Date Actual</td>
<td>5da. Design and Development Phase Start Date Planned</td>
<td>5db. Design and Development Phase Start Date Actual</td>
<td>5dc. Design and Development Phase End Date Planned</td>
<td>5dd. Design and Development Phase End Date Actual</td>
<td>5ea. Qualification Phase Start Date Planned</td>
<td>5eb. Qualification Phase Start Date Actual</td>
<td>5ec. Qualification Phase End Date Planned</td>
<td>5ed. Qualification Phase End Date Actual</td>
</tr>
<tr>
<td>109581</td>
<td>Proj 1</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>113075</td>
<td>Proj 2</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>132510</td>
<td>Proj 3</td>
<td>2017-10-2</td>
<td>2017-10-2</td>
<td>2017-10-13</td>
<td>2017-10-13</td>
<td>2017-10-16</td>
<td>2017-10-16</td>
<td>2017-12-29</td>
<td>0000-0-0</td>
<td>2018-3-1</td>
<td>0000-0-0</td>
<td>2018-8-31</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>132511</td>
<td>Proj 4</td>
<td>2017-10-2</td>
<td>2017-10-12</td>
<td>2017-10-13</td>
<td>2017-10-13</td>
<td>2017-10-16</td>
<td>2017-10-16</td>
<td>2017-12-29</td>
<td>2017-12-29</td>
<td>2018-1-2</td>
<td>2018-1-2</td>
<td>2018-5-11</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>114171</td>
<td>Proj 5</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>114200</td>
<td>Proj 6</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
</table>
</body>
</html>
所以一开始我尝试只旋转列名,但这行不通。
表格的结束状态需要如下所示
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
</head>
<body bgcolor="#FFFFFF">
<table border="1">
<tr>
<td>ID</td>
<td>Title</td>
<td>Phase</td>
<td>Start Date Planned</td>
<td>Start Date Actual</td>
<td>End Date Planned</td>
<td>End Date Actual</td>
</tr>
<tr>
<td>109581</td>
<td>Proj 1</td>
<td>Initiation</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>109581</td>
<td>Proj 1</td>
<td>Prelimenary Design</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>109581</td>
<td>Proj 1</td>
<td>Design and Development</td>
<td>2017-10-2</td>
<td>2017-10-13</td>
<td>2017-10-13</td>
<td>2017-10-16</td>
</tr>
<tr>
<td>109581</td>
<td>Proj 1</td>
<td>Qualification Phase</td>
<td>2017-10-12</td>
<td>2017-10-13</td>
<td>2017-10-13</td>
<td>2017-10-16</td>
</tr>
<tr>
<td>123456</td>
<td>Proj 2</td>
<td>Initiation</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>123456</td>
<td>Proj 2</td>
<td>Prelimenary Design</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
<td>0000-0-0</td>
</tr>
<tr>
<td>123456</td>
<td>Proj 2</td>
<td>Design and Development</td>
<td>2017-10-2</td>
<td>2017-10-13</td>
<td>2017-10-13</td>
<td>2017-10-16</td>
</tr>
<tr>
<td>123456</td>
<td>Proj 2</td>
<td>Qualification Phase</td>
<td>2017-10-12</td>
<td>2017-10-13</td>
<td>2017-10-13</td>
<td>2017-10-16</td>
</tr>
</table>
</body>
</html>
因此,当我执行第一个数据透视表时,我意识到我的格式仍然不是很有用,因为它只是使列成为行,而日期仍然不是所需的列。
这是第一个开始旋转的查询
SELECT
ID
,Title
,`5ba. Initiation Phase Start Date Planned` Date
, 'Initiation Phase Start Date Planned' Phase
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5bb. Initiation Phase Start Date Actual`
,'Initiation Phase Start Date Actual'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5bc. Initiation Phase End Date Planned`
,'Initiation Phase End Date Planned'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5bd. Initiation Phase End Date Actual`
,'Initiation Phase End Date Actual'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5ca. Preliminary Design Phase Start Date Planned`
,'Preliminary Design Phase Start Date Planned'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5cb. Preliminary Design Phase Start Date Actual`
,'Preliminary Design Phase Start Date Actual'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5cc. Preliminary Design Phase End Date Planned`
,'Preliminary Design Phase End Date Planned'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5cd. Preliminary Design Phase End Date Actual`
,'Preliminary Design Phase End Date Actual'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5da. Design and Development Phase Start Date Planned`
,'Design and Development Phase Start Date Planned'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5db. Design and Development Phase Start Date Actual`
,'Design and Development Phase Start Date Actual'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5dc. Design and Development Phase End Date Planned`
,'Design and Development Phase End Date Planned'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5dd. Design and Development Phase End Date Actual`
,'Design and Development Phase End Date Actual'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5ea. Qualification Phase Start Date Planned`
,'Qualification Phase Start Date Planned'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5eb. Qualification Phase Start Date Actual`
,'Qualification Phase Start Date Actual'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5ec. Qualification Phase End Date Planned`
,'Qualification Phase End Date Planned'
FROM RandD_Playbook
UNION ALL
SELECT
ID
,Title
,`5ed. Qualification Phase End Date Actual`
,'Qualification Phase End Date Actual'
FROM RandD_Playbook
我希望得到一些关于这方面的指导,以及按照我被问到的方式整合表格的最佳方式。
最佳答案
您需要选择 Phase
作为常量和四个相应的列。每个阶段一个子查询,由 UNION ALL 组合:
SELECT ID, Title,
'Initiation' as Phase,
`5ba. Initiation Phase Start Date Planned` as `Start Date Planned`,
`5bb. Initiation Phase Start Date Actual` as `Start Date Actual`,
`5bc. Initiation Phase End Date Planned` as `End Date Planned`,
`5bd. Initiation Phase End Date Actual` as `End Date Actual`
FROM RandD_Playbook
UNION ALL
SELECT ID, Title,
'Preliminary Design' as Phase,
`5ca. Preliminary Design Phase Start Date Planned` as `Start Date Planned`,
`5cb. Preliminary Design Phase Start Date Actual` as `Start Date Actual`,
`5cc. Preliminary Design Phase End Date Planned` as `End Date Planned`,
`5cd. Preliminary Design Phase End Date Actual` as `End Date Actual`
FROM RandD_Playbook
UNION ALL
SELECT ID, Title,
'Design and Development' as Phase,
`5da. Design and Development Phase Start Date Planned` as `Start Date Planned`,
`5db. Design and Development Phase Start Date Actual` as `Start Date Actual`,
`5dc. Design and Development Phase End Date Planned` as `End Date Planned`,
`5dd. Design and Development Phase End Date Actual` as `End Date Actual`
FROM RandD_Playbook
UNION ALL
SELECT ID, Title,
'Qualification' as Phase,
`5ea. Qualification Phase Start Date Planned` as `Start Date Planned`,
`5eb. Qualification Phase Start Date Actual` as `Start Date Actual`,
`5ec. Qualification Phase End Date Planned` as `End Date Planned`,
`5ed. Qualification Phase End Date Actual` as `End Date Actual`
FROM RandD_Playbook
ORDER BY Title
样本数据的结果:
| ID | Title | Phase | Start Date Planned | Start Date Actual | End Date Planned | End Date Actual |
|--------|--------|------------------------|--------------------|-------------------|------------------|-----------------|
| 109581 | Proj 1 | Initiation | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 109581 | Proj 1 | Qualification | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 109581 | Proj 1 | Design and Development | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 109581 | Proj 1 | Preliminary Design | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 113075 | Proj 2 | Design and Development | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 113075 | Proj 2 | Qualification | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 113075 | Proj 2 | Preliminary Design | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 113075 | Proj 2 | Initiation | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 132510 | Proj 3 | Design and Development | 2018-03-01 | 0000-00-00 | 2018-08-31 | 0000-00-00 |
| 132510 | Proj 3 | Preliminary Design | 2017-10-16 | 2017-10-16 | 2017-12-29 | 0000-00-00 |
| 132510 | Proj 3 | Initiation | 2017-10-02 | 2017-10-02 | 2017-10-13 | 2017-10-13 |
| 132510 | Proj 3 | Qualification | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 132511 | Proj 4 | Qualification | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 132511 | Proj 4 | Design and Development | 2018-01-02 | 2018-01-02 | 2018-05-11 | 0000-00-00 |
| 132511 | Proj 4 | Initiation | 2017-10-02 | 2017-10-12 | 2017-10-13 | 2017-10-13 |
| 132511 | Proj 4 | Preliminary Design | 2017-10-16 | 2017-10-16 | 2017-12-29 | 2017-12-29 |
| 114171 | Proj 5 | Initiation | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 114171 | Proj 5 | Qualification | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 114171 | Proj 5 | Design and Development | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 114171 | Proj 5 | Preliminary Design | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 114200 | Proj 6 | Preliminary Design | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 114200 | Proj 6 | Qualification | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 114200 | Proj 6 | Design and Development | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
| 114200 | Proj 6 | Initiation | 0000-00-00 | 0000-00-00 | 0000-00-00 | 0000-00-00 |
关于MySQL 将列转为行并转回列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48837256/
我是一名优秀的程序员,十分优秀!