如何通过查询获取主组内最频繁值(MODE)的COUNT/frequency?

How to get the COUNT/frequency of the most frequent value(MODE) within the main group by query?

这是我的查询:

WITH subtable AS (
SELECT
    member_casual,
    seasons,
    start_station_name || ' to ' || end_station_name AS route
FROM
    bike_data
)

SELECT
    member_casual,
    seasons,
    MODE() WITHIN GROUP (ORDER BY route) AS most_frequent_route, 
    COUNT(*) AS total_service_used_count
FROM
    subtable
GROUP BY
    member_casual,
    seasons;

这是结果:

当我想将 most_frequent_route 的 frequency/count 作为新列包含在此 groupby 结果中时,困难就来了。在这里问之前,我问过一个nice的前辈,他说:

Put the main query into a CTE, then in the new outer query you can write a scalar subquery that performs the count over the subtable where the subtable rows match those from the grouping CTE.

老实说,我不太理解这个建议的核心逻辑,所以我不能把所有这些放在一起作为一个完整的查询。任何人都可以给我一些如何让它工作的例子吗?提前致谢!

可能这位好心的前辈是这个意思:

WITH subtable_a AS (
SELECT
    member_casual,
    seasons,
    start_station_name || ' to ' || end_station_name AS route
FROM
    bike_data
),
subtable_b as
(
SELECT
    member_casual,
    seasons,
    MODE() WITHIN GROUP (ORDER BY route) AS most_frequent_route, 
    COUNT(*) AS total_service_used_count
FROM
    subtable_a
GROUP BY
    member_casual,
    seasons
)
select sb.*, 
(
  SELECT count(*) 
  from subtable_a sa 
  where sa.member_casual = sb.member_casual
  and sa.seasons = sb.seasons
  and sa.route = sb.most_frequent_route
) as most_frequent_route_count
from subtable_b sb;

您的 subtable 变为 subtable_a,您的主查询变为 subtable_b(将主查询放入 CTE)并在新的主(外部)查询中 select全部来自 subtable_b 和标量子查询列 - 括号中的内部大写 SELECT count(*) - 提取 most_frequent_route_count.