如何按字段排序我的查询,仍然按db2中该字段的子集进行分组?

时间:2010-02-24 13:26:00

标签: db2

很抱歉,标题令人困惑。这是我的查询

Select MONTH(DATE(TIMESTAMP)), SUM(FIELD1), SUM(FIELD2) from TABLE WHERE TIMESTAMP BETWEEN '2009-07-26 00:00:00' AND '2010-02-24 23:59:59' GROUP BY MONTH(DATE(TIMESTAMP))

这将让我从查询中获取月份编号。问题是,现在它正在分类1,2,3,4 ......当它跨越两个不同的年份。我需要能够按月对该查询进行排序。

如果我在查询结束时添加“ORDER BY TIMESTAMP”,我会收到此错误:

SELECT TIMESTAMP列或SELECT列表中的表达式无效。 SQLCODE = -122

此外,我更改了此问题的字段名称以清楚地表明该字段实际上并未被称为TIMESTAMP

1 个答案:

答案 0 :(得分:2)

您需要按月分组。:

SELECT YEAR(YourField),
Month(YourField),
SUM(Field1),
SUM(Field2)
FROM Table
WHERE...
GROUP BY
YEAR(YourField),
Month(YourField)
ORDER BY
YEAR(YourField),
Month(YourField)