oracle sql 用条件语句进行排名和计数
oracle sql ranking and count with conditional statements
我坚持基于特定条件对行进行分类的过程。我希望在你们的支持下度过难关!
基本上,我试图通过应用一些特定的公式来为 table 创建两个新列。这些列用于对每年的数量进行排名,也用于通过指定每个 prac 是否包含在 2014
或 not
中的 top 3 claims
中进行分类。在下面的示例中,light blue
中突出显示的列是我要创建的列。
我的查询如下 - 现在我在为上面的最后两行编写代码时遇到问题 from
,group by
和 having
似乎在这些括号内不起作用。请帮忙!!
select
fiscal_year,
prac,
count(*) "CLAIMS",
sum(paid_amount) "COST",
row_number() over (group by fiscal_year order by count(*) desc) "Annual Rank by Claims",
case
when row_number() over (having fiscal_year = '2014' order by count(*) desc) < 4
then 'YES'
else 'NO'
end "PRAC TOP 3 IN CLAIMS 2014"
from mys.mv_claim
where category = 'V'
group by fiscal_year,
prac
/
缺少原始数据,以给定的(中间)输出为起点 - 第一条语句(导致 MV_Claim_Ranked)可以替换为您的选择来自 mys.mv_claim:
WITH
MV_Claim_Ranked AS (
SELECT
fiscal_year,
prac,
claims,
cost,
annual_rank_by_claim,
RANK() OVER (PARTITION BY fiscal_year ORDER BY claims DESC) annual_rank_by_claim_calc,
prac_top_3_in_claims_2014
FROM MV_Claim_Grouped
)
SELECT
T1.*,
CASE WHEN (SELECT annual_rank_by_claim_calc
FROM MV_Claim_Ranked T2
WHERE T1.prac = T2.prac
AND T2.fiscal_year = 2014) < 4
THEN 'YES' ELSE 'NO' END AS prac_top_3_in_claims_2014_calc
FROM MV_Claim_Ranked T1
ORDER BY prac, fiscal_year
;
您的合计数据仅供参考。
查看实际效果:SQL Fiddle。
请发表评论,如果这需要调整/进一步的细节。
我从未见过 GROUP BY 和 HAVING 用于分区规范;我怀疑这是一个语法错误。我认为您的查询需要看起来更像:
WITH t as (
select
fiscal_year,
prac,
count(*) "CLAIMS",
sum(paid_amount) "COST"
from mys.mv_claim
where category = 'V'
group by fiscal_year, prac
)
SELECT
t.*,
rank() over (partition by fiscal_year order by claims desc) "Annual Rank by Claims",
case
when prac in(select prac from (select * from t where fiscal_year = 2014 order by claims desc ) where rownum <= 3)
then 'YES'
else 'NO'
end "PRAC TOP 3 IN CLAIMS 2014"
from t
我使用排名而不是行号,因为两个相同的声明计数将产生相同的排名值。然后排名将跳过下一个排名(两个 999 的索赔计数将分别排名 1,一个 998 的索赔计数将排名 3。如果您希望它排名 2,请使用 dense_rank)
如果您对此有任何错误,请告诉我,因为我唯一不确定的是检索前 3 个实践的子选择。这可以通过左连接来完成(我对此更有信心)
我坚持基于特定条件对行进行分类的过程。我希望在你们的支持下度过难关!
基本上,我试图通过应用一些特定的公式来为 table 创建两个新列。这些列用于对每年的数量进行排名,也用于通过指定每个 prac 是否包含在 2014
或 not
中的 top 3 claims
中进行分类。在下面的示例中,light blue
中突出显示的列是我要创建的列。
from
,group by
和 having
似乎在这些括号内不起作用。请帮忙!!
select
fiscal_year,
prac,
count(*) "CLAIMS",
sum(paid_amount) "COST",
row_number() over (group by fiscal_year order by count(*) desc) "Annual Rank by Claims",
case
when row_number() over (having fiscal_year = '2014' order by count(*) desc) < 4
then 'YES'
else 'NO'
end "PRAC TOP 3 IN CLAIMS 2014"
from mys.mv_claim
where category = 'V'
group by fiscal_year,
prac
/
缺少原始数据,以给定的(中间)输出为起点 - 第一条语句(导致 MV_Claim_Ranked)可以替换为您的选择来自 mys.mv_claim:
WITH
MV_Claim_Ranked AS (
SELECT
fiscal_year,
prac,
claims,
cost,
annual_rank_by_claim,
RANK() OVER (PARTITION BY fiscal_year ORDER BY claims DESC) annual_rank_by_claim_calc,
prac_top_3_in_claims_2014
FROM MV_Claim_Grouped
)
SELECT
T1.*,
CASE WHEN (SELECT annual_rank_by_claim_calc
FROM MV_Claim_Ranked T2
WHERE T1.prac = T2.prac
AND T2.fiscal_year = 2014) < 4
THEN 'YES' ELSE 'NO' END AS prac_top_3_in_claims_2014_calc
FROM MV_Claim_Ranked T1
ORDER BY prac, fiscal_year
;
您的合计数据仅供参考。
查看实际效果:SQL Fiddle。
请发表评论,如果这需要调整/进一步的细节。
我从未见过 GROUP BY 和 HAVING 用于分区规范;我怀疑这是一个语法错误。我认为您的查询需要看起来更像:
WITH t as (
select
fiscal_year,
prac,
count(*) "CLAIMS",
sum(paid_amount) "COST"
from mys.mv_claim
where category = 'V'
group by fiscal_year, prac
)
SELECT
t.*,
rank() over (partition by fiscal_year order by claims desc) "Annual Rank by Claims",
case
when prac in(select prac from (select * from t where fiscal_year = 2014 order by claims desc ) where rownum <= 3)
then 'YES'
else 'NO'
end "PRAC TOP 3 IN CLAIMS 2014"
from t
我使用排名而不是行号,因为两个相同的声明计数将产生相同的排名值。然后排名将跳过下一个排名(两个 999 的索赔计数将分别排名 1,一个 998 的索赔计数将排名 3。如果您希望它排名 2,请使用 dense_rank)
如果您对此有任何错误,请告诉我,因为我唯一不确定的是检索前 3 个实践的子选择。这可以通过左连接来完成(我对此更有信心)