Я пытаюсь преобразовать следующий сценарий Teradata в BigQuery:
drop table NK1_120golive_vol;
create volatile table NK1_120golive_vol
( ktnr bigint,
dex smallint)
on commit preserve rows;
insert into NK1_120golive_vol
sel a.ktnr, case when Nachfragemenge > 0 and (Nachfragemenge - Stornomenge) > 0 then 1
when (Nachfragemenge > 0 and (Nachfragemenge - Stornomenge) = 0) then 100
else 99 end dex
from (sel BPID, BPTypeID from VAT.AddressOverview where FirmID =11 ) as a (ktnr, Partnerart)
left join (sel BPID, sum(case when Rectype in(1) then KPIValueQty else 0 end) ,
sum(case when Rectype in (3,121) then KPIValueQty else 0 end)
from VAT.positions where (Reportingdate-OrderAge) between 1200101 and 1200630 group by 1 ) as kred (ktnr, Nachfragemenge, Stornomenge) on a.ktnr = kred.ktnr
left join vat.CustomerOverview c on a.ktnr = c.BPID
left join scat.cust120_au_alle_dexe e on a.ktnr = e.BPID
where Partnerart like 'Z001' and e.BPID is null or Partnerart like 'Z001' and e.dex between 14 and 100 and Nachfragemenge > 0
group by 1,2;
Вот мой перевод BigQuery:
create or replace temp table NK1_120golive_vol as (
with a as( select bpid as ktnr,
BPTypeID as Partnerart
from VAT.AddressOverview where FirmID =11),
b as( select bpid as ktnr,
sum(case when Rectype in(1) then KPIValueQty else 0 end) as Nachfragemenge,
sum(case when Rectype in (3,121) then KPIValueQty else 0 end) as Stornomenge
from VAT.positions ) ,
c as (select bpid, BPTypeID from vat.customeroverview) ,
e as ( select bpid, dex from scat.cust120_au_alle_dexe),
tmp10 as (select a.ktnr, case when Nachfragemenge > 0 and (Nachfragemenge - Stornomenge) > 0 then 1
when (Nachfragemenge > 0 and (Nachfragemenge - Stornomenge) = 0) then 100
else 99 end dex
from a
left join c on a.ktnr =c.bpid
left join e on a.ktnr = e.bpid
where Partnerart like 'Z001' and e.BPID is null or Partnerart like 'Z001' and e.dex between 14 and 100
and sum(case when Rectype in(1) then KPIValueQty else 0 end) >0))
select * from NK1_120golive_vol
Но я получаю следующую ошибку:
Синтаксическая ошибка: ожидалось "(" или "," или ключевое слово SELECT, но получено ")" в [20: 170]
Что я делаю не так?