我试图在数据透视表中显示游戏结果。我的结果表如下所示:
StageID StringID ShooterID FinalScore
------------------------------------------
1 1 10041 6.579
1 3 10040 18.008
1 2 10040 13.639
1 1 10040 9.895
1 3 10051 15.464
1 2 10051 23.842
1 1 10051 21.018
1 3 10041 13.601
1 2 10041 8.151
2 1 10041 49.5
2 2 10041 59.5
2 3 10041 49.5
2 1 10040 69.5
2 3 10040 39.5
2 1 10051 109.5
2 2 10051 89.5
2 3 10051 99.5
2 2 10040 59.5
... many more rows
使用动态sql来转移数据。这是我的剧本:
SET @sql = NULL;
SELECT group_concat(DISTINCT
concat( 'sum(CASE WHEN `StageID` = ', `StageID` ,' THEN FinalScore END) Stage',`StageID`)
) INTO @sql
FROM
`results`;
SET @sql = concat('SELECT ShooterID, ', @sql ,'
FROM
`results`
WHERE
matchID = 118
GROUP BY ShooterID');
prepare stmt FROM @sql;
execute stmt;
DEALLOCATE prepare stmt;
结果如下:
ShooterID Stage0 Stage99 Stage1 Stage2
------------------------------------------
10039 NULL NULL 38.259 188.5
10040 NULL NULL 41.542 168.5
10041 NULL NULL 28.331 158.5
10042 NULL NULL 59.975 355.5
10043 NULL NULL 49.233 198.5
10047 NULL NULL 33.156 218.5
10048 NULL NULL 33.214 358.5
10051 NULL NULL 60.324 298.5
10052 NULL NULL 30.742 278.5
10053 NULL NULL 62.487 268.5
问题在于我回到了2列(Stage0和Stage99)并且我不明白为什么。结果表中只有两个阶段。有什么想法吗?
更新:调试此脚本时,我确定Workbench在group_concat()期间创建了以下字符串:
'SELECT ShooterID,
sum(CASE WHEN `StageID` = 0 THEN FinalScore END) - max(CASE WHEN `StageID` = 0 THEN FinalScore END) Stage0,
sum(CASE WHEN `StageID` = 99 THEN FinalScore END) - max(CASE WHEN `StageID` = 99 THEN FinalScore END) Stage99,
sum(CASE WHEN `StageID` = 1 THEN FinalScore END) - max(CASE WHEN `StageID` = 1 THEN FinalScore END) Stage1,
sum(CASE WHEN `StageID` = 2 THEN FinalScore END) - max(CASE WHEN `StageID` = 2 THEN FinalScore END) Stage2\n FROM\n `wp_plinker_results`\n WHERE\n matchID = 118\n GROUP BY ShooterID'
我有什么办法可以阻止Workbench这样做吗?
答案 0 :(得分:0)
您在select语句中引用matchID
(示例中的列是StringID
),但是该语句作为expecetd(在给定的小数据子集上),请参见此处: sqlfiddle。正如@hjpotter指出的那样,原因可能在于数据(StringID=118
)本身,所以也许你可以提供更多数据?