create table results (
  id          serial primary key,
  run         text,
  tps         float4
);

-- explain
-- with base_tps as (
--   select run, tps from results
-- )
-- select run from (
--   select
--     run,
--     count(*) as runs,
-- 
--     (select tps from base_tps b where b.run = r.run) AS base_tps
-- 
-- from results r
-- group by
--     run
-- order by
--     run
-- ) results_agg order by 1;


create view results_agg as
with base_tps as (
  select run, tps from results
)
select
    run,
    count(*) as runs,

    (select tps from base_tps b where b.run = r.run) AS base_tps

from results r
group by
    run
order by
    run;

explain (costs off) SELECT run FROM results_agg ORDER BY 1;
