我需要查询tableA中排名前5的值。如下面从一系列日期中获取所有排名前5的id(数组)
select id, count(occurrence), date
from
(select id, unnest(value) as occurrence, date from tableA) as a
group by id, occurrence, date
order by occurrence desc
limit 5
id | occurrence | date
-----+-------+----------
330 | 11 | 20141015
400 | 11 | 20141015
390 | 10 | 20141015
240 | 10 | 20141015
501 | 10 | 20141015
并收到的ID后,查询同一个表A来获取ID的所有值用于其它日期的范围是从20140101到20141015.
expected result:
id | occurrence | date
-----+-------+----------
330 | 11 | 20141015
400 | 11 | 20141015
390 | 10 | 20141015
240 | 10 | 20141015
501 | 10 | 20141015
330 | 0 | 20141014
400 | 1 | 20141014
390 | 10 | 20141014
240 | 15 | 20141014
501 | 10 | 20141014
330 | 11 | 20141013
400 | 11 | 20141013
390 | 11 | 20141013
240 | 19 | 20141013
501 | 10 | 20141013
但我究竟该怎么办那?
我的PostgreSQL的版本是8.1,我不能使用分区(如果任何U想暗示)
编辑
select id, count(value) as occurrence, date
from tableA
where id = ANY(
select array(
select id
from (
select date, unnest(id) as id
from tableA where date>='20140101' and date<='20141015'
)as a
)
)
group by id, date
不返回任何东西。我的阵列是否正确?
我怀疑这个答案是否正确... – momokjaaaaa
你能给我们更多的细节吗?你到底想要什么? –
id是一个数组,如果你注意到 – momokjaaaaa