在 MySQL 和 Dynamic Jasper 中进行高级 table 旋转
Advanced table pivoting in MySQL and Dynamic Jasper
我有一个 MySQL Table 以时间 table 的方式存储我的 classes。列是固定的,行是 id, day,class, classid, 1,2,3,4,5,6,7,8
这是我的 table 结构
_______________________________________________________________________________
| id | day | class | class_id | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|____|_____|_______|__________|_____|_____|_____|_____|_____|_____|_____|_____|
| 1 | Mon | FIA | 1 | Eng | Che | Mat | Geo | Cre | Kis | His | Bio |
| 1 | Tue | FIA | 1 | Geo | Cre | His | Che | His | Kis | Bio | Mat |
| 1 | Wed | FIA | 1 | Mat | Eng | Geo | Geo | Cre | Bio | Cre | Bio |
| 1 | Thu | FIA | 1 | Eng | Che | Mat | Eng | His | Kis | His | Geo |
| 1 | Fri | FIA | 1 | Geo | Bio | Eng | Geo | Che | Mat | His | Bio |
| 1 | Mon | FIB | 1 | Cre | Che | Mat | Eng | Cre | Kis | Eng | Che |
| 1 | Tue | FIB | 1 | Eng | Che | Bio | Geo | Bio | Mat | His | Eng |
| 1 | Wed | FIB | 1 | Eng | Eng | Mat | Mat | Cre | Eng | Geo | Bio |
| 1 | Thu | FIB | 1 | Cre | Cre | Mat | Geo | Eng | Kis | Mat | Eng |
| 1 | Fri | FIB | 1 | Mat | Che | Eng | Eng | Mat | Che | Mat | Bio |
| 1 | Mon | FIC | 1 | Eng | Che | Che | Geo | Cre | Kis | His | Mat |
| 1 | Tue | FIC | 1 | Che | Eng | Mat | His | Mat | Che | Che | Bio |
| 1 | Wed | FIC | 1 | Cre | Che | His | Che | Bio | Kis | Bio | Mat |
| 1 | Thu | FIC | 1 | Eng | Mat | Mat | Geo | Cre | Kis | His | Mat |
| 1 | Fri | FIC | 1 | Eng | Che | His | Geo | Cre | Kis | His | Bio |
|____|_____|_______|__________|_____|_____|_____|_____|_____|_____|_____|_____|
我想做的是为所有 class 创建一个摘要时间table。我正在用动态碧玉创建它,但最重要的是我得到了正确的查询。
这就是我期待实现的目标。
_____________________________________________________________________________________________________________________________________________________________________
| | Mon | Tue | Wed | Thu | Fri |
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
_____________________________________________________________________________________________________________________________________________________________________
|F1A |Eng|Che|Mat|Geo|Cre|Kis|His|Bio|Geo|Cre|His|Che|His|Kis|Bio|Mat|Mat|Eng|Geo|Geo|Cre|Bio|Cre|Bio|Eng|Che|Mat|Eng|His|Kis|His|Geo|Geo|Bio|Eng|Geo|Che|Mat|His|Bio|
|F1B |Cre|Che|Mat|Eng|Cre|Kis|Eng|Che|Eng|Che|Bio|Geo|Bio|Mat|His|Eng|Eng|Eng|Mat|Mat|Cre|Eng|Geo|Bio|Cre|Cre|Mat|Geo|Eng|Kis|Mat|Eng|Mat|Che|Eng|Eng|Mat|Che|Mat|Bio|
|F1C |Eng|Che|Che|Geo|Cre|Kis|His|Mat|Che|Eng|Mat|His|Mat|Che|Che|Bio|Cre|Che|His|Che|Bio|Kis|Bio|Mat|Eng|Mat|Mat|Geo|Cre|Kis|His|Mat|Eng|Che|His|Geo|Cre|Kis|His|Bio|
_____________________________________________________________________________________________________________________________________________________________________
您可以忽略最上面的天数列,我只需要一个查询,该查询将为我提供从星期一到星期五的每一天的列 122345678。
这 可以 在 SQL 中完成,尽管这可能不是完成这项工作的最佳工具,正如其他人已经评论的那样。
逻辑是使用条件聚合...和大量输入。这是 3 列(col1
、col2
和 col3
)和两天('Mon'
和 'Tue'
)的示例:
select class,
max(case when day = 'Mon' then col1 end) mon_1,
max(case when day = 'Mon' then col2 end) mon_2,
max(case when day = 'Mon' then col3 end) mon_3,
max(case when day = 'Tue' then col1 end) tue_1,
max(case when day = 'Tue' then col2 end) tue_2,
max(case when day = 'Tue' then col3 end) tue_3
from mytable
group by class_id, class
然后您可以为其他日期和列展开相同的逻辑。
我有一个 MySQL Table 以时间 table 的方式存储我的 classes。列是固定的,行是 id, day,class, classid, 1,2,3,4,5,6,7,8 这是我的 table 结构
_______________________________________________________________________________
| id | day | class | class_id | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|____|_____|_______|__________|_____|_____|_____|_____|_____|_____|_____|_____|
| 1 | Mon | FIA | 1 | Eng | Che | Mat | Geo | Cre | Kis | His | Bio |
| 1 | Tue | FIA | 1 | Geo | Cre | His | Che | His | Kis | Bio | Mat |
| 1 | Wed | FIA | 1 | Mat | Eng | Geo | Geo | Cre | Bio | Cre | Bio |
| 1 | Thu | FIA | 1 | Eng | Che | Mat | Eng | His | Kis | His | Geo |
| 1 | Fri | FIA | 1 | Geo | Bio | Eng | Geo | Che | Mat | His | Bio |
| 1 | Mon | FIB | 1 | Cre | Che | Mat | Eng | Cre | Kis | Eng | Che |
| 1 | Tue | FIB | 1 | Eng | Che | Bio | Geo | Bio | Mat | His | Eng |
| 1 | Wed | FIB | 1 | Eng | Eng | Mat | Mat | Cre | Eng | Geo | Bio |
| 1 | Thu | FIB | 1 | Cre | Cre | Mat | Geo | Eng | Kis | Mat | Eng |
| 1 | Fri | FIB | 1 | Mat | Che | Eng | Eng | Mat | Che | Mat | Bio |
| 1 | Mon | FIC | 1 | Eng | Che | Che | Geo | Cre | Kis | His | Mat |
| 1 | Tue | FIC | 1 | Che | Eng | Mat | His | Mat | Che | Che | Bio |
| 1 | Wed | FIC | 1 | Cre | Che | His | Che | Bio | Kis | Bio | Mat |
| 1 | Thu | FIC | 1 | Eng | Mat | Mat | Geo | Cre | Kis | His | Mat |
| 1 | Fri | FIC | 1 | Eng | Che | His | Geo | Cre | Kis | His | Bio |
|____|_____|_______|__________|_____|_____|_____|_____|_____|_____|_____|_____|
我想做的是为所有 class 创建一个摘要时间table。我正在用动态碧玉创建它,但最重要的是我得到了正确的查询。 这就是我期待实现的目标。
_____________________________________________________________________________________________________________________________________________________________________
| | Mon | Tue | Wed | Thu | Fri |
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
_____________________________________________________________________________________________________________________________________________________________________
|F1A |Eng|Che|Mat|Geo|Cre|Kis|His|Bio|Geo|Cre|His|Che|His|Kis|Bio|Mat|Mat|Eng|Geo|Geo|Cre|Bio|Cre|Bio|Eng|Che|Mat|Eng|His|Kis|His|Geo|Geo|Bio|Eng|Geo|Che|Mat|His|Bio|
|F1B |Cre|Che|Mat|Eng|Cre|Kis|Eng|Che|Eng|Che|Bio|Geo|Bio|Mat|His|Eng|Eng|Eng|Mat|Mat|Cre|Eng|Geo|Bio|Cre|Cre|Mat|Geo|Eng|Kis|Mat|Eng|Mat|Che|Eng|Eng|Mat|Che|Mat|Bio|
|F1C |Eng|Che|Che|Geo|Cre|Kis|His|Mat|Che|Eng|Mat|His|Mat|Che|Che|Bio|Cre|Che|His|Che|Bio|Kis|Bio|Mat|Eng|Mat|Mat|Geo|Cre|Kis|His|Mat|Eng|Che|His|Geo|Cre|Kis|His|Bio|
_____________________________________________________________________________________________________________________________________________________________________
您可以忽略最上面的天数列,我只需要一个查询,该查询将为我提供从星期一到星期五的每一天的列 122345678。
这 可以 在 SQL 中完成,尽管这可能不是完成这项工作的最佳工具,正如其他人已经评论的那样。
逻辑是使用条件聚合...和大量输入。这是 3 列(col1
、col2
和 col3
)和两天('Mon'
和 'Tue'
)的示例:
select class,
max(case when day = 'Mon' then col1 end) mon_1,
max(case when day = 'Mon' then col2 end) mon_2,
max(case when day = 'Mon' then col3 end) mon_3,
max(case when day = 'Tue' then col1 end) tue_1,
max(case when day = 'Tue' then col2 end) tue_2,
max(case when day = 'Tue' then col3 end) tue_3
from mytable
group by class_id, class
然后您可以为其他日期和列展开相同的逻辑。