我有一个按平均值排名的结果集。
LOCATION JOB TILE AVG RANK
SEATTLE PRESIDENT 10.2 1
SEATTLE VP 8.5 2
SEATTLE SN. VP 5.4 3
CHICAGO SALESMAN 7.6 1
CHICAGO ENGINEER 3.2 2
CHICAGO PRESIDENT 1.5 3
我需要的下一部分是每个LOCATION组的第一个和最后一个排名平均值加上JOB TITLE。 例如:
LOCATION JOBTITLE AVG
SEATTLE PRESIDENT 10.2
SEATTLE SN. VP 5.4
CHICAGO SALESMAN 7.6
CHICAGO PRESIDENT 1.5
使用Redshift SQL有一种简单的方法吗?仅仅是一个FYI,AVG字段是在查询中完成的聚合。
答案 0 :(得分:0)
您可以计算每个位置的最小和最大平均值,并将其与平均值连接到原始查询,以获得所需的行。
with averages as (<your query>)
,minandmax as (select location, min(avgcol) minavg, max(avgcol) maxavg
from averages
group by location)
select a.location,a.job_title, a.avgcol
from averages a
join minandmax m on m.location = a.location and (m.maxavg=a.avgcol or m.minavg=a.avgcol)
答案 1 :(得分:0)
您可以使用窗口功能执行此操作:
with t as (
<your query here>
)
select t.*
from (select t.*,
max(rank) over (partition by location) as maxr
from t
) t
where rank in (1, maxr);