简体   繁体   English

SELECT项目列表中的子查询的SQL替代方法

[英]SQL alternative to sub-query in SELECT Item list

I have RDBMS table and Queries which are working perfectly. 我有完美的RDBMS表和查询。 I have offloaded data from RDBMS to HIVE table.To run the existing queries on HIVE, we need first to make them compatible to HIVE. 我已将数据从RDBMS卸载到HIVE表。要在HIVE上运行现有查询,我们首先需要使它们与HIVE兼容。

Let's take below example with sub-query in select item list. 让我们在选择项列表中以子查询为例。 It is syntactically valid and working fine on RDBMS system. 它在语法上是有效的,并且在RDBMS系统上运行良好。 But It Will not work on HIVE As per HIVE manual , Hive supports subqueries only in the FROM and WHERE clause. 但它不适用于HIVE根据HIVE手册 ,Hive仅支持FROM和WHERE子句中的子查询。

Example 1 : 例1:

SELECT t1.one
    ,(SELECT t2.two 
        FROM TEST2 t2 
        WHERE t1.one=t2.two) t21
    ,(SELECT t3.three 
        FROM TEST3 t3 
        WHERE t1.one=t3.three) t31
FROM TEST1 t1 ;

Example 2: 例2:

SELECT a.*
   , CASE
          WHEN EXISTS
                 (SELECT 1
                    FROM tblOrder O
                      INNER JOIN tblProduct P
                              ON O.Product_id = P.Product_id
                   WHERE O.customer_id        = C.customer_id
                     AND P.Product_Type IN (2, 5, 6, 9)
                 )
          THEN 1
          ELSE 0
   END AS My_Custom_Indicator
   FROM tblCustomer C
   INNER JOIN tblOtherStuff S
       ON C.CustomerID = S.CustomerID ;

Example 3 : 例3:

Select component_location_id, component_type_code,
  ( select clv.LOCATION_VALUE 
  from stg_dev.component_location_values clv 
  where  identifier_code = 'AXLE' 
       and component_location_id = cl.component_location_id ) as AXLE,
  ( select clv.LOCATION_VALUE 
  from stg_dev.component_location_values clv 
  where identifier_code = 'SIDE' 
       and component_location_id = cl.component_location_id ) as SIDE
  from stg_dev.component_locations cl ;

I want to know the possible alternative of sub-queries in select item list to make it compatible to hive. 我想知道选择项列表中可能的子查询替代方法,以使其与hive兼容。 Apparently I will be able to transform existing queries in HIVE format. 显然,我将能够转换HIVE格式的现有查询。

Any help and guidance is highly appreciated ! 任何帮助和指导都非常感谢!

The query you provided could be transformed to a simple query with LEFT JOIN s. 您提供的查询可以转换为使用LEFT JOIN的简单查询。

SELECT
  t1.one, t2.two AS t21, t3.three AS t31
FROM
  TEST1 t1
  LEFT JOIN TEST2 t2
    ON t1.one = t2.two
  LEFT JOIN TEST3 t3
    ON t1.one = t3.three

Since there is no limitation in the subqueries, the joins will return the same data. 由于子查询没有限制,连接将返回相同的数据。 (The subqueries should return only one or no row for each row in TEST1.) (对于TEST1中的每一行,子查询应该只返回一行或没有行。)

Please note, that your original query could not handle 1..n connections. 请注意,您的原始查询无法处理1..n连接。 In most DBMS, subqueries in the SELECT list should return only with a resultset with one columns and one or no row. 在大多数DBMS中,SELECT列表中的子查询应仅返回具有一列和一行或没有行的结果集。

Based on HIVE manual : 基于HIVE手册

SELECT t1.one, t2.two, t3.three 
FROM TEST1 t1,TEST2 t2, TEST3 t3
WHERE t1.one=t2.two AND t1.one=t3.three;
    SELECT t1.one,t2.two,t3.three FROM TEST1 t1 INNER 
   JOIN TEST2 t2 ON t1.one=t2.two INNER JOIN TEST3 t3 
   ON t1.one=t3.three WHERE t1.one=t2.two AND t1.one=t3.three;
SELECT t1.one,t2.two as t21,t3.three as t31 FROM TEST1 t1 
INNER JOIN TEST2 t2 ON t1.one=t2.two
INNER JOIN TEST3 t3 ON t1.one=t3.three

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM