psql table 基于计数的子选择

psql table subselection based on count

我有一个 table,我计算每个不同 ID 的行数。 如果行数 >= 6,那么我想从原来的 table 中 select 只有具有从查询返回的 id 的行。

我设法编写了代码的第一部分,如下所示。

select * from (select id, count(*) from flights group by id) as fcount where count >= 6

现在,我如何 select 从 table 飞行匹配第一个 selection 给出的 id 的所有行。

提前致谢

试试这个。

SELECT *
    FROM (
        SELECT f.*
            ,count(*) OVER (PARTITION BY ID) AS fcount
        FROM flights f
        ) s
    WHERE fcount >= 6

或者这个。

SELECT *
FROM Flights fo
WHERE EXISTS (
        SELECT 1
        FROM flights fi
        WHERE fi.id = fo.id
        GROUP BY id
        HAVING COUNT(*) >= 6
        )