自连接失败,出现子句

时间:2018-07-26 11:26:46

标签: sql csv self-join windows-subsystem-for-linux apache-drill

我正在使用apache演练通过自引用联接将CSV文件中的YTD(年初至今)求和。 (缩短的)查询是

select
  ... fields from table a ...
  a.PeriodAmount,
  sum(cast(b.PeriodAmount as dec(18,3))) as YTDAmount
from dfs.`/home/foo/data/a.csv` a
  left join dfs.`/home/foo/data/a.csv` b
on
  ... join-conditions ...
*** where a.Year = '2018' ***
group by
  ... group-conditions ...
order by
  ... order-conditions ...
;

查询工作没有子句。当在同一数据集上包含子句时,出现以下错误:

Error: UNSUPPORTED_OPERATION ERROR: This query cannot be planned possibly due to either a cartesian join or an inequality join
[Error Id: b62e6b63-eda7-4a52-8f95-2499a1f5c278 on foo:31010] (state=,code=0)

我可以通过删除where子句并执行子查询来避免该错误:

from (select * from dfs.`/home/foo/data/a.csv` where Year = '2017') a
  from (select * from dfs.`/home/foo/data/a.csv` where Year = '2017') b

但是我不确定这是否是正确的方法。它使查询更容易出错,因为必须将相同的条件应用于一个以上的子查询,而不是将其作为自然所属的where-子句。

此自联接是否可以重写,以便保留从句?

这是在ubuntu 16.04上,在Win10上使用WSL,而Apache钻是ver。 1.13。

完成(进行钻探)查询:

select
  a.Dep_id,
  a.Dep,
  substr(a.Post_id, 1, 4) as Kap,
  a.Post_id,
  substr(a.Post_id, 5, 2) as Post,
  a.Art_id,
  a.Art,
  a.V_id,
  a.Reg,
  a.Dep_V_id,
  a.Dep_V,
  concat(substr(a.Periode, 1, 4), '-', substr(a.Periode, 5, 2), '-15') as PeriodDate,
  a.Period,
  a.Year,
  a.PeriodAmount,
  sum(cast(b.PeriodAmount as dec(18,3))) as YTDAmount
from dfs.`/home/foo/data/a.csv` a
  left join dfs.`/home/foo/data/a.csv` b
on
  a.Dep_id = b.Dep_id
  and a.Post_id = b.Post_id
  and a.Post_id is not null
  and a.Art_id = b.Art_id
  and a.V_id = b.V_id
  and a.Reg = b.Reg
  and a.Dep_V_id = b.Dep_V_id
  and a.Dep_id = b.Dep_id
  and b.Period <= a.Period
  and a.Year = b.Year
  and a.Post_id = b.Post_id
  and a.Art_id = b.Art_id
where a.Year in ('2018') and b.Year in (a.Year)
group by
  a.Dep_id,
  a.Dep,
  a.Post_id,
  a.Art_id,
  a.Art,
  a.V_id,
  a.Reg,
  a.Dep_V_id,
  a.Dep_V,
  a.Dep_id,
  a.Period,
  a.Year,
  a.PeriodAmount
order by
  a.Year,
  a.Dep_id,
  a.Post_id,
  a.Art_id,
  a.V_id,
  a.Reg,
  a.Dep_V_id,
  a.Dep_id,
  a.Period,
  a.PeriodAmount
;

1 个答案:

答案 0 :(得分:1)

我没有像这样查询过csv文件,因此建议您尝试一下。

如何完成a和b的where子句以帮助像这样的编译器

WHERE a.Year = ‘2018’ AND b.Year = ‘2018’

WHERE a.Year = ‘2018’ AND b.Year = a.Year