我有一个包含大量列的表myTable
,请记住这个表太大了,其中一列是几何点,我们称之为mySortColumn
。我需要按计数mySortColumn
对我的选择进行排序。
一个例子可能是这个
myTable
id, mySortColumn
----------------
1, ASD12321F
2, ASD12321G
3, ASD12321F
4, ASD12321G
5, ASD12321H
6, ASD12321F
我有一个可以做我想要的查询,问题是时间。实际上需要30秒,看起来像这样:
SELECT
id,
mySortColumn
FROM
myTable
JOIN (
SELECT
mySortColumn,
ST_Y(mySortColumn) AS lat,
ST_X(mySortColumn) AS lng,
COUNT(*)
FROM myTable
GROUP BY mySortColumn
HAVING COUNT(*) > 1
) AS myPosition ON (
ST_X(myTable.mySortColumn) = myPosition.lng
AND ST_Y(myTable.mySortColumn) = myPosition.lat
)
WHERE
<some filters>
ORDER BY COUNT DESC
结果必须是:
id, mySortColumn
----------------
1, ASD12321F
3, ASD12321F
6, ASD12321F
2, ASD12321G
4, ASD12321G
5, ASD12321H
我希望你能帮助我。
答案 0 :(得分:1)
你在这里:
select * from myTable order by count(1) over (partition by mySortColumn) desc;
有关aggregate over ()
构造的更多信息,请查看:
http://www.postgresql.org/docs/9.4/static/tutorial-window.html