SQL(HIVE -HUE)剔除的三种方式

  • 左右链接 is null法
SELECT pid_new
FROM
  (SELECT DISTINCT pid_new
   FROM sms_data.re_hit_data
   WHERE file_name LIKE "sf_DX-20221110-M约约出行常量对比%"
     AND is_ok="ok"
     AND hit_date="20221111") a
LEFT JOIN
  (SELECT DISTINCT phone_sec
   FROM report.sms_report_data abc
   WHERE etldate = "20221112"
    --  AND status=3
     AND abc.task_id IN
       ( SELECT DISTINCT task_id
        FROM report.sms_send_data
        WHERE bank_id IN ("150")
          AND etldate = "20221112" ) )b ON a.pid_new=b.phone_sec
WHERE b.phone_sec IS NULL

  • 建立临时表 not in 法

WITH q1 AS
  (SELECT DISTINCT pid_new a
   FROM sms_data.re_hit_data
   WHERE file_name LIKE "sf_DX-20221110-M约约出行常量对比%"
     AND is_ok="ok"
     AND hit_date="20221111"),
     q2 as
  (SELECT DISTINCT phone_sec
   FROM report.sms_report_data
   WHERE etldate = "20221112"
     AND status=3
     AND task_id IN
       (SELECT DISTINCT task_id
        FROM report.sms_send_data
        WHERE bank_id IN ("150")
          AND etldate = "20221112"))
SELECT a
FROM q1
WHERE a NOT IN
    (SELECT *
     FROM q2)


  • where 嵌套查询 not in 法
SELECT pid_new
FROM
  (SELECT DISTINCT pid_new
   FROM sms_data.re_hit_data
   WHERE file_name LIKE "sf_DX-20221110-M约约出行常量对比%"
     AND is_ok="ok"
     AND hit_date="20221111") a
WHERE a.pid_new NOT IN
    (SELECT *
     FROM
       (SELECT DISTINCT phone_sec
        FROM report.sms_report_data
        WHERE etldate = "20221112"
          AND status=3
          AND task_id IN
            (SELECT DISTINCT task_id
             FROM report.sms_send_data
             WHERE bank_id IN ("150")
               AND etldate = "20221112"))s)

上述三种方法中,一般不建议使用not in 。IS NULL 的执行速度最快。

SELECT pid_new
FROM
  (SELECT DISTINCT pid_new
   FROM sms_data.re_hit_data
   WHERE file_name LIKE "sf_DX-20221110-M约约出行常量对比%"
     AND is_ok="ok"
     AND hit_date="20221111") a
WHERE a.pid_new NOT IN
       (SELECT DISTINCT phone_sec
        FROM report.sms_report_data abc 
        WHERE etldate = "20221112"
          AND status=3
          AND abc.task_id IN (
        SELECT DISTINCT task_id
             FROM report.sms_send_data
             WHERE bank_id IN ("150")
              AND etldate = "20221112"
        ) aa
               ) cc

第四种方法是第三种方法的删减版,但是在hive中会报错,无语法错误。应该是不支持这种写法,至于原因还未找到,若有大佬,请指教。

你可能感兴趣的:(SQL,hive,sql,hadoop)