Oracle SQL - 将一个多行查询列除以另一个

Posted

技术标签:

【中文标题】Oracle SQL - 将一个多行查询列除以另一个【英文标题】:Oracle SQL - Divide one multiple-row query column by another 【发布时间】:2018-09-24 17:46:52 【问题描述】:

我需要为我的 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 的解决方案,但我收到“字段'=' 是未知类型”的错误。经过数小时的搜索,我没有想法。这如何实现?

【问题讨论】:

使这两个查询成为子查询。将它们连接在一起并参考这些列。选择 AR_BALANCE / SALES_CALC from subqueryA inner join subquery B on a.month = b.month 【参考方案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

希望对您有所帮助。 迪奥尼斯。

【讨论】:

这非常有效。我以前从未在查询中使用过“WITH”子句,但我现在肯定会将它添加到我的工具箱中。非常感谢!

以上是关于Oracle SQL - 将一个多行查询列除以另一个的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Oracle SQL 中将整数除以产生浮点数?

求助,oracle多行数据合并成一行

求助,oracle多行数据合并成一行

Oracle中的多行插入查询(从一张表中选择多行并插入到另一张表中[重复]

Oracle sql使用子查询将多行结果分组为一行

oracle 一个字段转多行