RANK 函数不适用于 posgresql 中的 CTE 表?
我正在尝试根据两列的评论和分区(strig_full_nm and exam_year_nb)对CTE表(A)输出进行排名。
以下是我的代码:
\`WITH a AS
(
select rr.risk_full_nm ,e.exam_year_nb,COUNT(DISTINCT e.exam_dmn_xtrnl_id) as "reviews"
from examwrksp.dsh_exam_base e
left join star.firm f ON f.firm_id = e.crd_nb --firm
left join examwrksp.dsh_mlstn_event_dates d ON d.exam_id = e.exam_id --exam milestones
left join examwrksp.dsh_scope_risk_cntnt rr on rr.exam_id = e.exam_id
where e.exam_year_nb \>= '2018' and e.exam_ctgry_cd = 'CYCLE' and e.exam_st_ds in ('Open','Closed') and e.dstrt_nm != 'MAP Group' and e.exam_type_ds not like 'Funding%'
and e.exam_dmn_prmry_fl = 'Y' and f.main_ofc = 'Y' and d.crd_nb is null
and rr.scope_actv_st is not null and rr.scope_actv_st = 'ACTIVE' and rr.unit_type_label_tx not in ('Discovery Review','Risk Identification Review')
GROUP BY rr.risk_full_nm ,e.exam_year_nb
)
select a.risk_full_nm,a.exam_year_nb,a.reviews,
RANK () OVER(PARTITION BY a.risk_full_nm,a.exam_year_nb ORDER BY a.reviews desc)
from a
order by reviews desc\`
我希望我的等级列会排名我的记录,但我得到了每行中的“ 1”。我想念什么?
I'm trying to rank my CTE table (a) output based on reviews and partition by two columns (risk_full_nm and exam_year_nb).
Below is my code:
\`WITH a AS
(
select rr.risk_full_nm ,e.exam_year_nb,COUNT(DISTINCT e.exam_dmn_xtrnl_id) as "reviews"
from examwrksp.dsh_exam_base e
left join star.firm f ON f.firm_id = e.crd_nb --firm
left join examwrksp.dsh_mlstn_event_dates d ON d.exam_id = e.exam_id --exam milestones
left join examwrksp.dsh_scope_risk_cntnt rr on rr.exam_id = e.exam_id
where e.exam_year_nb \>= '2018' and e.exam_ctgry_cd = 'CYCLE' and e.exam_st_ds in ('Open','Closed') and e.dstrt_nm != 'MAP Group' and e.exam_type_ds not like 'Funding%'
and e.exam_dmn_prmry_fl = 'Y' and f.main_ofc = 'Y' and d.crd_nb is null
and rr.scope_actv_st is not null and rr.scope_actv_st = 'ACTIVE' and rr.unit_type_label_tx not in ('Discovery Review','Risk Identification Review')
GROUP BY rr.risk_full_nm ,e.exam_year_nb
)
select a.risk_full_nm,a.exam_year_nb,a.reviews,
RANK () OVER(PARTITION BY a.risk_full_nm,a.exam_year_nb ORDER BY a.reviews desc)
from a
order by reviews desc\`
I'd expect my rank column to be ranking my records but I'm getting "1" in every single row. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我分析了您的疑问。您的查询中有一些错误的逻辑代码。我向您解释:
上获得(按
语句进行分区。在查询中,您对两个字段进行了分组,a.risk_full_nm
和a.exam_year_nb
分组这些字段后,所有结果的行都不同,因为您必须重复(同一)。我使用示例查询来解释:
在此查询中,我想获得每个用户的登录名。是不同的。
当我们从“摸索”字段列表中删除“ ID”字段时,我们返回了正确计算的正确数据并计数()。例如:
I analyzed your queries. You have some incorrect logic codes in your queries. I explain to you:
over (partition by
statement. In your query, you have grouped two fields,a.risk_full_nm
anda.exam_year_nb
. After grouping these fields all rows of resulting data are different because you have not to duplicated (same) rows. So,RANK()
function will be calculated again each for all rows. For best understanding, I explain to you using sample queries.For example:
In this query, I wanted to get counts of logins for each user. This is incorrect syntax. So, I used the unique primary key field 'id' for grouping and my rows will be are different. So for each of these rows function count() gets only 1 value.
When we remove the 'id' field from the groping field lists, we returned the correct data and count() calculated correctly. For example: