我正在使用MySQL。这是我的桌子

Level     Programmingtime    Clientname     projectid
0             128              abc             3
1             32               abc             3
2             126              abc             3
3             541              abc             3
4             452              abc             3
1             32               abc             3


但是现在我必须以类似的格式在水晶报表中显示此数据

projectid  level0              level1      level2   level3   level4
3          128                 32+32=(64)     126      541      452
           (programmingtime)


请告诉我这种困难的逻辑的语法是什么。

提前致谢。

最佳答案

由于级别是已知的,因此您可以简单地执行以下操作:

SELECT
    projectid,
    SUM(CASE WHEN level = 0 THEN programmingtime ELSE 0 END) AS level0,
    SUM(CASE WHEN level = 1 THEN programmingtime ELSE 0 END) AS level1,
    SUM(CASE WHEN level = 2 THEN programmingtime ELSE 0 END) AS level2,
    SUM(CASE WHEN level = 3 THEN programmingtime ELSE 0 END) AS level3,
    SUM(CASE WHEN level = 4 THEN programmingtime ELSE 0 END) AS level4
FROM
    tbl
GROUP BY
    projectid

07-24 22:14