【问题标题】:Budget and actual rows not merged on full outer join预算和实际行未在完全外连接上合并
【发布时间】:2019-03-19 09:40:00
【问题描述】:

以下查询:

select coalesce(to_number(bl.division_code), bud.division) division
,      coalesce(bud.glaccountcode, bl.costcenter_costanalysis_period_periods_year_years_balance_code_attr) glaccountcode
,      coalesce(bud.costcenter, bl.costcenter_code_attr) costcenter
,      coalesce(bud.costunit, bl.code_attr) costunit
,      coalesce(bud.reportingyear, bl.costcenter_costanalysis_period_periods_year_reportingyear_attr) reportingyear
,      coalesce(bud.reportingperiod, bl.costcenter_costanalysis_period_reportingperiod_attr) reportingperiod
,      case when bud.amountdc > 0 then 456 else null end budgetamountdc label 'Budget (anonymized, EUR)'
,      case when bl.balance > 0 then 123 else null end actualsamountdc label 'Actuals (anonymized, EUR)'
,      case
       when bl.division_code is null
       then 'budget'
       when bud.division is null
       then 'balancelines'
       else 'both'
       end
       label 'Source'
from   exactonlinexml..balancelinesperperiodcostanalysis bl
full
outer
join   exactonlinerest..budgets bud
on     bud.division        = to_number(bl.division_code)
and    bud.glaccountcode   = bl.costcenter_costanalysis_period_periods_year_years_balance_code_attr
and    bud.costcenter      = bl.costcenter_code_attr
and    bud.costunit        = bl.code_attr
and    bud.reportingyear   = bl.costcenter_costanalysis_period_periods_year_reportingyear_attr
and    bud.reportingperiod = bl.costcenter_costanalysis_period_reportingperiod_attr

将总帐帐户上的实际交易与相关预算详细关联起来:

  • 确切的在线公司 (division_code)
  • 财政年度 (reportingyear)
  • 财务期(reportingperiod)
  • 总帐帐户 (glaccountcode)
  • 成本中心 (costcenter)
  • 成本单位 (costunit)

我希望这些维度的每个组合最多有一行数据。但是,对于某些组合,将返回 2 行。其中一行有一个标签“预算”,而另一行有一个“平衡线”。

似乎它们没有在合并中合并在一起:

2019年第1期余额行中gl账户5050的内容为一行,有一定金额(不等于0)。

2019年第1期预算中GL账户5050的内容也是一行,有一定金额(不等于0)。

我似乎无法找到为什么行没有通过完全外连接和合并合并在一起。

我做错了什么?

【问题讨论】:

    标签: exact-online invantive-sql


    【解决方案1】:

    您正在使用 6 个维度的连接。这些维度中的每一个都是余额和预算的主键的一部分。但是,其中一些维度可以包含空值。 Null 是 SQL 逻辑的特殊之处,它定义了一个未知类型的未知值(可以有各种类型的未知值)。 SQL 使用三值逻辑 (Mr. Brouwer says hello)。查看查询的输出

    select 1=1 are_they_equal_bool1
    ,      1=0 are_they_equal_bool2
    ,      null=null are_they_equal_bool3
    

    它清楚地表明 null=null 评估为“灰色”,意思是 null:不知道它是真还是假:

    您需要补偿联接中的空值。在这种情况下,您可能会将成本中心和成本单位的 NULL 定义为与成本中心和/或成本单位无关的财务金额。在这种情况下,余额行中的空值和预算中的空值具有相同的语义。

    干净的路由是适应原来的join条件:

    and    bud.costcenter      = bl.costcenter_code_attr
    and    bud.costunit        = bl.code_attr
    

    在两列中都有 null 以具有相同的含义:

    and    ( ( bud.costcenter is null and bl.costcenter_code_attr is null )
             or 
             bud.costcenter      = bl.costcenter_code_attr 
           )
    and    ( ( bud.costunit is null and bl.code_attr is null )
             or 
             bud.costunit      = bl.code_attr 
           )
    

    【讨论】:

      猜你喜欢
      • 2014-09-23
      • 2017-06-09
      • 2018-11-20
      • 1970-01-01
      • 2015-11-15
      • 2016-12-19
      • 2018-04-22
      • 2018-08-27
      • 2017-09-12
      相关资源
      最近更新 更多