Sql 从一系列日期中获取所有前5个id(一个数组)
我需要查询表A中的前5个值。如下图所示Sql 从一系列日期中获取所有前5个id(一个数组),sql,postgresql,postgresql-8.1,Sql,Postgresql,Postgresql 8.1,我需要查询表A中的前5个值。如下图所示 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 ----
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后,查询同一个表以获取从20140101到20141015的其他日期的id的所有值
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,如果你们想建议的话,我不能使用分区
编辑
不返回任何内容。我的数组是否正确?您可以尝试从dt>20140101和dt<20141015以及row_num的表格中选择某物。您可以尝试从dt>20140101和dt<20141015以及row_num的表格中选择某物。我认为我们不需要将所有内容都命名为q??值实际上是一个数组。对不起,我已经编辑了我的问题。提前谢谢,我不认为我们需要把所有东西都命名为q??值实际上是一个数组。对不起,我已经编辑了我的问题。谢谢你能告诉我们更多细节吗?你到底想要什么?id是一个数组,如果你能告诉我们更多细节的话?你到底想要什么?如果你注意到的话,id是一个数组
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
select id, count(value) as occurrence, date
from t
where id in (
select id
from (
select id, count(value) as occurrence, date
from t
group by id, date
order by occurrence desc
limit 5
) s
) q
group by id, date