我有下表备份,它结合了小时,人员,备份类型和备份日期的数据:
data | type | persons | hours
==============================================
2015-06-12 15 4 8
2015-06-12 15 4 8
2015-06-12 15 4 8
2015-06-12 15 4 8
2015-06-13 14 2 4
2015-06-13 14 2 4
我试图编写一个返回以下结果的查询:
data | type | persons | hours
==============================================
2015-06-12 15 4 32
2015-06-13 14 2 8
我的查询如下:
SELECT "data", "type", SUM("hours"), "persons" FROM "backup" GROUP BY "type", "data", "persons" ORDER BY "data"
但它确实按我想要的方式工作:(
修改
使用此查询后:
SELECT "data", "type", "persons", SUM("hours") AS "hours" FROM "backup" WHERE "project" = '2000030' GROUP BY "type", "data", "persons" ORDER BY "data"
SQL返回此数据:
data | type | persons | hours
==========================================
2011-12-16 | 2003774 | 9 | 12
2011-12-16 | 2003774 | 10 | 12
但应该有:
data | type | persons | hours
==========================================
2011-12-16 | 2003774 | 9 | 54
在DB中我有9条记录(每人一条记录),每个人的单元格为9,每小时单元格为9.我需要将每个日期和每种类型的备份(支持)的小时数相加。
答案 0 :(得分:1)
正确的查询是
SELECT "data", "type", "persons", SUM("hours") FROM "backup" GROUP BY "type", "data", "persons" ORDER BY "data"
"人"和SUM("小时")被切换。根据预期结果,这是唯一的问题。
更新: 如果您希望每种类型有一行,请尝试以下操作:
SELECT "data", "type", MAX("persons"), SUM("hours") FROM "backup" GROUP BY "type", "data" ORDER BY "data"