我是hive的新手,正在尝试执行包含nonequi join的查询。 但是,当我尝试在配置单元0.7中执行时,由于无条件连接条件而出现错误。
查询:
SELECT * INTO dbo.tbl_B
FROM ( select
Id,
Name,
Age,
SellingPrice,
row_number() OVER ( PARTITION BY Id,Name
ORDER BY Age asc) AS row_no
from tbl_A
LEFT JOIN dbo.tbl_C
ON A.[Id]=C.[Id]
and A.SellingPrice <= C.BuyingPrice
) AA
WHERE row_no = 1 ;
请提供一个无需使用nonequi join即可重写此查询的想法。
答案 0 :(得分:0)
将非等价联接条件移动到where子句:
SELECT *
FROM ( select
Id,
Name,
Age,
SellingPrice,
row_number() OVER ( PARTITION BY Id,Name ORDER BY Age asc) AS row_no
from tbl_A
LEFT JOIN dbo.tbl_C
ON A.[Id]=C.[Id]
where (A.SellingPrice <= C.BuyingPrice) OR (C.BuyingPrice is null)
) AA
WHERE row_no = 1 ;
OR (C.BuyingPrice is null)
是允许左连接所必需的,如果没有此条件,则左连接将转换为内部连接,因为仅非等式条件本身将过滤出空值,请参见here。
答案 1 :(得分:0)
Solution not working for me
query used:
CREATE TABLE IMART_PREP.TST (STRT_DT DATE , END_DT DATE, ID INT , SALARY INT );
INSERT INTO IMART_PREP.TST VALUES ( '2018-01-01' , '2018-01-31' ,1, 1000);
INSERT INTO IMART_PREP.TST VALUES ( '2018-02-01' , '2018-02-28' , 1, 2000);
INSERT INTO IMART_PREP.TST VALUES ( '2018-03-01' , '2018-03-30' , 1, 3000);
INSERT INTO IMART_PREP.TST VALUES ( '2018-04-01' , '2018-04-30' , 1, 4000);
SELECT * FROM IMART_PREP.TST;
CREATE TABLE IMART_PREP.TST_1 (STRT_DT DATE , END_DT DATE, ID INT , SALARY INT );
INSERT INTO IMART_PREP.TST_1 VALUES ( '2018-01-01' , '2018-01-31', 1, 1000);
SELECT *
FROM IMART_PREP.TST A , IMART_PREP.TST_1 B
WHERE ( (A.ID = B.ID ) AND (A.STRT_DT BETWEEN B.STRT_DT AND B.END_DT ) ) OR (B.STRT_DT IS NULL );