本文介绍了Oracle APEX在渲染时计算利润的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含固定行的交互式网格,需要计算预呈现时的公式。
所以源查询是:
select kpi,monthly,yearly from kpi where project_id = :P1_PROJECT_ID;
我需要修改它,以便在呈现时计算KPI=‘Gross’的行。
它在网格中的外观如下:
我正在尝试编写SQL查询,但它不起作用。尽管有数据,但它返回NULL。我在这里做错了什么?
select kpi,
case when KPI='Gross'
then to_char(case when KPI='Profit' then to_number(replace(nvl(monthly,0),',','')) end /
case when KPI='Loss' then to_number(replace(nvl(monthly,0),',','')) end ,'999,999,999,999')
else to_char( monthly,'999,999,999,999') end as monthly,
case when KPI='Gross'
then to_char(case when KPI='Profit' then to_number(replace(nvl(yearly,0),',','')) end /
case when KPI='Loss' then to_number(replace(nvl(yearly,0),',','')) end ,'999,999,999,999')
else to_char( yearly,'999,999,999,999') end as yearly,
from kpi where project_id = :P1_PROJECT_ID;
to_char用于以逗号分隔的形式显示值。因此,当KPI=Gross时,它会将KPI=利润的列除以KPI=亏损和显示结果。
另外,KPI=Gros行中的结果也应该连接%。
顶点20.2
如何实现此目标?
推荐答案
您已接近,但您需要使用窗口函数。如果不使用窗口函数,查询将不会查看CASE语句中的其他行来计算总值列。
下面的查询是如何使用窗口函数正确计算总收入。我添加了ROUND
以将总额舍入为整数,但如果您需要小数点,则可以将其删除。WITH
kpi (pk,
kpi,
monthly,
yearly,
project_id)
AS
(SELECT 1, 'Revenue', 60000, 2000000, 1 FROM DUAL
UNION ALL
SELECT 2, 'Profit', 20, 30, 1 FROM DUAL
UNION ALL
SELECT 3, 'Loss', 10, 50, 1 FROM DUAL
UNION ALL
SELECT 4, 'Gross', NULL, NULL, 1 FROM DUAL)
SELECT k.kpi,
CASE k.kpi
WHEN 'Gross'
THEN
ROUND (
SUM (CASE k.kpi WHEN 'Profit' THEN k.monthly ELSE 0 END)
OVER (PARTITION BY project_id)
/ SUM (CASE k.kpi WHEN 'Loss' THEN k.monthly ELSE 0 END)
OVER (PARTITION BY project_id))
|| '%'
ELSE
TO_CHAR (k.monthly)
END AS monthly,
CASE k.kpi
WHEN 'Gross'
THEN
ROUND (
SUM (CASE k.kpi WHEN 'Profit' THEN k.yearly ELSE 0 END)
OVER (PARTITION BY project_id)
/ SUM (CASE k.kpi WHEN 'Loss' THEN k.yearly ELSE 0 END)
OVER (PARTITION BY project_id))
|| '%'
ELSE
TO_CHAR (k.yearly)
END AS yearly
FROM kpi k
WHERE project_id = 1;
KPI MONTHLY YEARLY
__________ __________ __________
Revenue 60000 2000000
Profit 20 30
Loss 10 50
Gross 2% 1%
这篇关于Oracle APEX在渲染时计算利润的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!