Один и тот же точный онлайн-запрос в Invantive Control for Excel дает другой результат при выполнении с клиентом

У меня есть следующий запрос, чтобы сделать отчет о доходах на основе групп областей из Exact Online:

select itemgroupcode 
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied 
,      name 
,      financialyear 
,      financialperiod 
,      sum(quantity) 
       aantal 
,      sum(amountdc) 
       omzet  
,      sum(quantity2jaar) 
       aantal2014 
,      sum(omzet2jaar) 
       omzet2014 
,      sum(quantity1jaar) 
       aantal2015 
,      sum(omzet1jaar)
       omzet2015 
,      sum(quantityhuidigejaar) 
       aantal2016 
,      sum(omzethuidigejaar)
       omzet2016
from   ( select substr(act.postcode, 1, 2) 
         ,       case 
                 when financialyear = year(getdate()) - 0
                 then amountdc 
                 else 0 
                 end
                 * -1 
                 omzethuidigejaar 
         ,       case 
                 when financialyear = year(getdate()) - 1 
                 then amountdc 
                 else 0 
                 end
                 * -1 
                 omzet1jaar
         ,       case 
                 when financialyear = year(getdate()) - 2 
                 then amountdc 
                 else 0 
                 end
                 * -1  
                 omzet2jaar
         ,       case 
                 when financialyear = year(getdate()) - 0
                 then quantity 
                 else 0 
                 end
                 quantityhuidigejaar
         ,       case 
                 when financialyear = year (getdate()) - 1 
                 then quantity 
                 else 0 
                 end
                 quantity1jaar 
         ,       case 
                 when financialyear = year(getdate()) - 2 
                 then quantity 
                 else 0 
                 end
                 quantity2jaar 
         ,       case 
                 when substr(act.postcode, 1, 2) >= '10'
                      and substr(act.postcode, 1, 2) < '20' 
                 then '1000-1999'
                 when substr(act.postcode, 1, 2) >= '20' 
                      and substr(act.postcode, 1, 2) < '30' 
                 then '2000-2999' 
                 when substr(act.postcode, 1, 2) >= '30' 
                      and substr(act.postcode, 1, 2) < '40' 
                 then '3000-3999' 
                 when substr(act.postcode, 1, 2) >= '40' 
                      and substr(act.postcode, 1, 2) < '50'
                 then '4000-4999' 
                 when substr(act.postcode, 1, 2) >= '50' 
                      and substr(act.postcode, 1, 2) < '60'
                 then '5000-5999' 
                 when substr(act.postcode, 1, 2) >= '60'
                      and substr(act.postcode, 1, 2) < '70' 
                 then '6000-6999' 
                 when substr(act.postcode, 1, 2) >= '70'
                      and substr(act.postcode, 1, 2) < '80'
                 then '7000-7999' 
                 when substr(act.postcode, 1, 2) >= '80' 
                      and substr(act.postcode, 1, 2) <= '89'
                 then '8000-8999' 
                 when substr(act.postcode, 1, 2) >= '90' 
                      and substr(act.postcode, 1, 2) <= '99'
                 then '9000-9999'
                 else 'unknown' 
                 end
                 postcodegebied
         ,       -1 * tle.amountdc
         ,       tle.financialperiod
         ,       tle.financialyear
         ,       act.country
         ,       act.name
         ,       itm.code
         ,       itm.description
         ,       tle.quantity
         ,       itm.itemgroupdescription
         ,       itm.itemgroupcode
         from    transactionlines tle
         join    exactonlinerest.crm.accounts act 
         on      act.code = tle.accountcode
         join    exactonlinerest.financial.glaccounts glt 
         on      glt.code = tle.glaccountcode
         --
         -- Type 110: grootboekrekening van het type omzet
         --
         and     glt.type = 110 
         join    exactonlinerest.logistics.items itm
         on      tle.itemcode = itm.code 
         --
         -- zodat er alleen transacties worden meegenomen die op een artikel geboekt zijn.
         --
         where   tle.itemcode is not null
       ) tle2
group 
by     itemgroupcode
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied
,      name 
,      financialyear 
,      financialperiod
order 
by     itemgroupcode 
,      financialyear 
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied
,      name 
,      financialperiod

При выполнении этого запроса на машине клиента я ожидаю такого же результата. Но результат другой. Каждая сумма и количество умножаются на восемь в зависимости от того, выполняется ли запрос от имени пользователя Exact Online «[email protected]» или «[email protected]».

Как это может происходить и как я могу это исправить?


person Bram Reemers    schedule 20.12.2016    source источник
comment
Сколько отделов вы запрашиваете? Может быть, это умножает количество строк?   -  person Patrick Hofman    schedule 20.12.2016


Ответы (1)


В вашем запросе отсутствует ряд оптимизаций и не включены полные условия соединения.

Обратите внимание, что уникальный естественный или бизнес-ключ, например, элемента — это и подразделение (компания), и код элемента, поэтому всегда включайте в соединение оба. Когда вы забываете включить деление, где это применимо, вы умножаете количество строк, если используете идентичные (копии) деления. С не копиями вы получите разные результаты.

Учитывая, что вы получаете в 8 раз больше и наличие 3 соединений, я предполагаю, что вы иногда работаете с 2 идентичными/копирующими подразделениями, выбранными в Exact Online, и это приводит к тому, что мощность (2, 3) = в 8 раз выше значений.

Кроме того, вы извлекаете много строк транзакций главной книги, которые вы не используете в агрегировании, таких как строки трехлетней давности или старше. Лучше исключить те, что раньше, с таким пунктом, как financialyear >= ....

Правильный запрос должен быть примерно таким:

select itemgroupcode 
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied 
,      name 
,      financialyear 
,      financialperiod 
,      sum(quantity) 
       aantal 
,      sum(amountdc) 
       omzet  
,      sum(quantity2jaar) 
       aantal2014 
,      sum(omzet2jaar) 
       omzet2014 
,      sum(quantity1jaar) 
       aantal2015 
,      sum(omzet1jaar)
       omzet2015 
,      sum(quantityhuidigejaar) 
       aantal2016 
,      sum(omzethuidigejaar)
       omzet2016
from   ( select substr(act.postcode, 1, 2) 
         ,       case 
                 when financialyear = year(getdate()) - 0
                 then amountdc 
                 else 0 
                 end
                 * -1 
                 omzethuidigejaar 
         ,       case 
                 when financialyear = year(getdate()) - 1 
                 then amountdc 
                 else 0 
                 end
                 * -1 
                 omzet1jaar
         ,       case 
                 when financialyear = year(getdate()) - 2 
                 then amountdc 
                 else 0 
                 end
                 * -1  
                 omzet2jaar
         ,       case 
                 when tle.financialyear = year(getdate()) - 0
                 then tle.quantity 
                 else 0 
                 end
                 quantityhuidigejaar
         ,       case 
                 when tle.financialyear = year (getdate()) - 1 
                 then tle.quantity 
                 else 0 
                 end
                 quantity1jaar 
         ,       case 
                 when tle.financialyear = year(getdate()) - 2 
                 then tle.quantity 
                 else 0 
                 end
                 quantity2jaar 
         ,       case 
                 when substr(act.postcode, 1, 2) >= '10'
                      and substr(act.postcode, 1, 2) < '20' 
                 then '1000-1999'
                 when substr(act.postcode, 1, 2) >= '20' 
                      and substr(act.postcode, 1, 2) < '30' 
                 then '2000-2999' 
                 when substr(act.postcode, 1, 2) >= '30' 
                      and substr(act.postcode, 1, 2) < '40' 
                 then '3000-3999' 
                 when substr(act.postcode, 1, 2) >= '40' 
                      and substr(act.postcode, 1, 2) < '50'
                 then '4000-4999' 
                 when substr(act.postcode, 1, 2) >= '50' 
                      and substr(act.postcode, 1, 2) < '60'
                 then '5000-5999' 
                 when substr(act.postcode, 1, 2) >= '60'
                      and substr(act.postcode, 1, 2) < '70' 
                 then '6000-6999' 
                 when substr(act.postcode, 1, 2) >= '70'
                      and substr(act.postcode, 1, 2) < '80'
                 then '7000-7999' 
                 when substr(act.postcode, 1, 2) >= '80' 
                      and substr(act.postcode, 1, 2) <= '89'
                 then '8000-8999' 
                 when substr(act.postcode, 1, 2) >= '90' 
                      and substr(act.postcode, 1, 2) <= '99'
                 then '9000-9999'
                 else 'unknown' 
                 end
                 postcodegebied
         ,       -1 * tle.amountdc
         ,       tle.financialperiod
         ,       tle.financialyear
         ,       act.country
         ,       act.name
         ,       itm.code
         ,       itm.description
         ,       tle.quantity
         ,       itm.itemgroupdescription
         ,       itm.itemgroupcode
         from    transactionlines tle
         join    exactonlinerest..accounts act 
         on      act.code = tle.accountcode
         and     act.division = tle.division
         join    exactonlinerest..glaccounts glt 
         on      glt.code = tle.glaccountcode
         --
         -- Type 110: GL Account of type Revenue.
         --
         and     glt.type     = 110 
         and     glt.division = tle.division
         join    exactonlinerest.logistics.items itm
         on      itm.code     = tle.itemcode
         and     itm.division = tle.division
         --
         -- Only transaction lines with an item.
         --
         where   tle.itemcode is not null
         --
         -- Only journal for revenues.
         --
         and     tle.journalcode = '70'
         --
         -- Optimization: not interested in older transactions than 2 years.
         --
         and     tle.financialyear >= year(getdate()) - 2
       ) tle2
group 
by     itemgroupcode
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied
,      name 
,      financialyear 
,      financialperiod
order 
by     itemgroupcode 
,      financialyear 
,      itemgroupdescription 
,      code 
,      description 
,      country 
,      postcodegebied
,      name 
,      financialperiod
person Guido Leenders    schedule 20.12.2016