0

我正在使用 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)

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

提前致谢。

4

1 回答 1

2

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

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
于 2012-07-05T06:19:48.197 回答