我有这个问题:
SELECT AVG(value), EXTRACT(YEAR FROM date_input)::text || EXTRACT(MONTH FROM date_input)::text AS tgl FROM app_inputdata WHERE dataid='95' AND date_input BETWEEN '2013-01-01' AND '2016-12-31' GROUP BY tgl ORDER BY tgl
,此查询的结果如下:
+-----+--------+
| avg | tgl |
+-----+--------+
|23.1 | 20141 |
|12.1 | 201412 |
|1.7 | 20142 |
+-----+--------+
如您所见,tgl
201412
出现在第二行,它应出现在最后一行。我在想它是因为tgl
是字符串类型,那么如何将其排序为整数?
答案 0 :(得分:0)
尝试使用日期组件:
SELECT AVG(value),
EXTRACT(YEAR FROM date_input)::text || EXTRACT(MONTH FROM date_input)::text AS tgl
FROM app_inputdata
WHERE dataid='95' AND
date_input BETWEEN '2013-01-01' AND '2016-12-31'
GROUP BY tgl
ORDER BY MAX(EXTRACT(YEAR FROM date_input)), MAX(EXTRACT(MONTH FROM date_input))