Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 自联接因where子句而失败_Sql_Csv_Self Join_Windows Subsystem For Linux_Apache Drill - Fatal编程技术网

Sql 自联接因where子句而失败

Sql 自联接因where子句而失败,sql,csv,self-join,windows-subsystem-for-linux,apache-drill,Sql,Csv,Self Join,Windows Subsystem For Linux,Apache Drill,我使用ApacheDrill在一个CSV文件中使用自引用联接对年初至今的数据进行求和。缩短的查询是 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-co

我使用ApacheDrill在一个CSV文件中使用自引用联接对年初至今的数据进行求和。缩短的查询是

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 ...
;
查询在没有where子句的情况下工作。当where子句包含在同一数据集上时,我得到以下错误:

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子句应用于它自然所属的位置

是否可以重写此自联接以维护where子句

这是在Ubuntu16.04上使用win10上的WSL和apache drill版本。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
;

我还没有像这样查询csv文件,所以这更多的是一个尝试的建议

完成a和b的where子句来帮助编译器如何

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


问题是?@JoakimDanielson谢谢你。我试图详细说明这个问题。答对了!非常感谢。查询优化器似乎还没有按预期处理此场景。我应该在apache drill上提交一个问题吗?该查询也适用于a.年在'2017',2018'和b.年在a.年。我不确定这是否是一个错误,或者您是否必须这样定义查询。在postgres和ms sql中,在'2017',2018'中有a.年就足够了。我还没有在mysql上测试。@kometen最初的连接条件是什么&在哪里?你在说什么?如果您还没有将a.year强制为等于b.year的内容,但是这些添加的内容是可以的,那么您最初的查询就是错误的,本质上是交叉连接。
WHERE a.Year = ‘2018’ AND b.Year = a.Year