我有两个表,stats和stat_log。静日志是这样的:使用具有两列以上的聚合函数
user_id,stat_id,value,registered
而且记录的统计信息对于给定的时代价值。我需要给定用户的每个属性的每个最后值。
所以,我想是这样的,但值: select stat,max(registered) from stat_log where uid = 1 group by stat;
stat | max
------+------------
6 | 2009-10-08
1 | 2009-10-08
3 | 2009-10-08
5 | 2009-10-08
7 | 2009-10-08
4 | 2009-10-08
相反,我得到这个:
select stat,max(registered),value from stat_log where uid = 1 group by stat,value;
stat | max | value
------+------------+-------
4 | 2009-10-08 | 38
5 | 2009-10-08 | 118
1 | 2009-10-08 | 100
1 | 2009-10-07 | 101
6 | 2009-10-08 | 68
3 | 2009-10-08 | 110
7 | 2009-10-08 | 53
什么是正确的查询?这是PostgreSQL 8.3。
它是有效的,但它返回与其他用户,这两个日期等 – 2009-10-08 16:42:37
哎呀。莫名其妙地认为统计是主要关键。虽然“截然不同”(我不知道存在,直到我看到上述接受的答案!)编辑和修复查询似乎是一个更好的解决方案。 – iammichael 2009-10-08 17:25:50