我需要为我的CFO计算过去12个会计期间的每日未清销售额(DSO)。 DSO的计算方式如下:
ar_balance / (credit_sales_sum / days_in_period)
这是我要查询的ar_balance的查询,按时间段分组:
SELECT EXTRACT(month FROM v_glacct_ybb_all_periods_cumm.glperiods_end_date) AS month,
SUM(ABS(v_glacct_ybb_all_periods_cumm.period_balance)) as ar_balance
FROM v_glacct_ybb_all_periods_cumm
WHERE (v_glacct_ybb_all_periods_cumm.glperiods_end_date BETWEEN
add_months(TRUNC(sysdate, 'month'), -12) AND TRUNC(sysdate, 'month'))
AND (acct='1100-00-10-00')
GROUP by v_glacct_ybb_all_periods_cumm.glperiods_end_date
ORDER by v_glacct_ybb_all_periods_cumm.glperiods_end_date
这给我的结果看起来像:
MONTH AR_BALANCE
----- -----------
9 $102,408.33
10 $163,715.86
11 $114,683.96
12 $198,223.75
1 $155,598.19
2 $123,121.00
etc etc
现在这是我的查询,以提取credit_sales_sum除以days_in_period,再按期间分组:
SELECT EXTRACT(month FROM v_glacct_ybb_all_periods_cumm.glperiods_end_date) AS month,
ROUND((sum(abs(v_glacct_ybb_all_periods_cumm.period_balance))) /
(EXTRACT(day FROM v_glacct_ybb_all_periods_cumm.glperiods_end_date)),2) AS sales_calc
FROM v_glacct_ybb_all_periods_cumm
WHERE(v_glacct_ybb_all_periods_cumm.glperiods_end_date BETWEEN add_months(TRUNC(sysdate, 'month'), -12)
AND TRUNC(sysdate, 'month'))
AND (acct='4080-00-10-00' OR acct='4090-00-10-00' OR acct='4095-00-10-00' OR acct='4096-00-10-00')
GROUP BY v_glacct_ybb_all_periods_cumm.glperiods_end_date
ORDER BY v_glacct_ybb_all_periods_cumm.glperiods_end_date
这给了我类似于第一个查询的多行结果:
MONTH SALES_CALC
----- ----------
9 $48,975.43
10 $44,167.89
11 $48,997.81
12 $49,493.17
1 $48,218.43
2 $52,523.84
etc etc
这现在导致我的问题。我不知道如何将第一个查询的AR_BALANCE列的结果除以第二个查询的SALES_CALC列,同时还按原样包括MONTH列。我已经尝试过this question的解决方案,但收到错误消息“字段'='是未知类型”。经过数小时的搜索,我失去了主意。如何做到这一点?
答案 0 :(得分:1)
如果我错了,请纠正我。 如果您需要指定两个中间查询结果并获取MONTH列,则不只是取消注释脚本中的查询并删除我的测试输入数据即可。
with ar_bal_tab as (
/*
SELECT EXTRACT(month FROM v_glacct_ybb_all_periods_cumm.glperiods_end_date) AS month,
SUM(ABS(v_glacct_ybb_all_periods_cumm.period_balance)) as ar_balance
FROM v_glacct_ybb_all_periods_cumm
WHERE (v_glacct_ybb_all_periods_cumm.glperiods_end_date BETWEEN
add_months(TRUNC(sysdate, 'month'), -12) AND TRUNC(sysdate, 'month'))
AND (acct='1100-00-10-00')
GROUP by v_glacct_ybb_all_periods_cumm.glperiods_end_date
ORDER by v_glacct_ybb_all_periods_cumm.glperiods_end_date
*/
select 9 as month, 102408.33 as ar_balance from dual
union all
select 10 as month, 163715.86 as ar_balance from dual
union all
select 11 as month, 114683.96 as ar_balance from dual
union all
select 12 as month, 198223.75 as ar_balance from dual
union all
select 1 as month, 155598.19 as ar_balance from dual
union all
select 2 as month, 123121.00 as ar_balance from dual
),
sales_calc_tab as (
/*
SELECT EXTRACT(month FROM v_glacct_ybb_all_periods_cumm.glperiods_end_date) AS month,
ROUND((sum(abs(v_glacct_ybb_all_periods_cumm.period_balance))) /
(EXTRACT(day FROM v_glacct_ybb_all_periods_cumm.glperiods_end_date)),2) AS sales_calc
FROM v_glacct_ybb_all_periods_cumm
WHERE(v_glacct_ybb_all_periods_cumm.glperiods_end_date BETWEEN add_months(TRUNC(sysdate, 'month'), -12)
AND TRUNC(sysdate, 'month'))
AND (acct='4080-00-10-00' OR acct='4090-00-10-00' OR acct='4095-00-10-00' OR acct='4096-00-10-00')
GROUP BY v_glacct_ybb_all_periods_cumm.glperiods_end_date
ORDER BY v_glacct_ybb_all_periods_cumm.glperiods_end_date
*/
select 9 as month, 48975.43 as sales_calc from dual
union all
select 10 as month ,44167.89 as sales_calc from dual
union all
select 11 as month ,48997.81 as sales_calc from dual
union all
select 12 as month ,49493.17 as sales_calc from dual
union all
select 1 as month ,48218.43 as sales_calc from dual
union all
select 2 as month, 52523.84 as sales_calc from dual
)
select
abt.month,
ar_balance / sales_calc as DSO
from ar_bal_tab abt,
sales_calc_tab sct
where abt.month = sct.month
希望有帮助。 迪奥尼斯。