2016-07-22 83 views
0

语境HIVEQL:为什么我的SELECT查询不起作用?

我正在使用Hive并希望将Query_1与Query_2合并。两者都是分开工作:

--> Query_1 
SELECT DISTINCT 
table_a.number, 
table_a.country, 
table_a.brand 
FROM db_y.table_b,db_x.table_a 
WHERE table_a.date = '20160718' 
AND CAST (table_a.brand as DOUBLE) IS NOT NULL 
AND table_a.number = table_b.number 
AND table_a.country = table_b.country 
AND table_a.brand = table_b.brand 
991  413  7040482 
991  413  7040484 
991  413  7040486 


--> Query_2 
SELECT DISTINCT 
    table_a.number, 
    table_a.country, 
    table_a.brand 
    FROM db_x.table_a,db_x.table_c 
    WHERE table_a.date = '20160719' 
    AND table_a.brand = substring(table_c.brand,2,7) 
    AND table_a.country = substring(table_c.country,2,3) 
    AND table_a.number = substring(table_c.number,2,3) 
    907  298  0004130 --> found in table_b 
    907  298  0004138 
    907  410  7024257 

问题:

下面,合并后的查询Query_3不起作用,为什么呢?

--> Query_3 
SELECT DISTINCT 
    table_a.number, 
    table_a.country, 
    table_a.brand 
    FROM db_y.table_b,db_x.table_a,db_x.table_c 
    WHERE table_a.date = '20160718' 
    AND table_a.number = table_b.number 
    AND table_a.country = table_b.country 
    AND table_a.brand = table_b.brand 
    AND table_b.brand = substring(table_c.brand,2,7) 
    AND table_b.country = substring(table_c.country,2,3) 
    AND table_b.number = substring(table_c.number,2,3); 

这里是Query_3的替代查询工作:

SELECT DISTINCT 
    table_a.number, 
    table_a.country, 
    table_a.brand 
    FROM db_x.table_a,(SELECT DISTINCT 
    table_b.number, 
    table_b.country, 
    table_b.brand 
    FROM db_y.table_b,db_x.table_c 
    WHERE table_b.brand = substring(table_c.brand,2,7) 
    AND table_b.country = substring(table_c.country,2,3) 
    AND table_b.number = substring(table_c.number,2,3)) subq 
    WHERE table_a.date = '20160718' 
    AND table_a.number = subq.number 
    AND table_a.country = subq.country 
    AND table_a.brand = subq.brand; 

但我真的想了解,为什么Query_3是错误的。

信息:

  • 在我的电脑,它会阻止96%的减少步
  • 在我朋友的一个(比我更好的容量),则返回0的结果(虽然我们期待结果)

谢谢。

+0

'MySQL'或'hive'?你可能有不同的数据类型,在最后三个条件中改为'table_a'而不是'table_b'。 – dnoeth

+1

修复您的查询以使用明确的'JOIN'语法。如果你这样做,你可能会发现一个问题。 。 。而且它至少会使读者更清楚地阅读它。 –

+0

@dnoeth:这里使用了Hive。我改变它,它现在运行在我的电脑上,但我得到0结果。 –

回答

0

在第一个查询3,你说

table_a.country = table_b.country 
and table_b.country = substring(table_c.country,2,3) 

这基本上就意味着table_a.country =子(table_c.country,2,3)。

在第二个查询3中,您只是将table_a.country与table_b.country进行比较。您的派生表将table_b.country加入substring(table_c.country,2,3),但它只返回table_b.country。这就是你要加入table_a.country的列。这同样适用于您正在子串化的table_c的所有列。

我希望一切都有道理......