为同一个表编写子查询以访问不同的记录

vx6bjr1n  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(245)

我正在试图找到的记录数,是独家不在零售商\u id 27,28,29。但此查询在配置单元中不起作用。你能告诉我哪里出错了吗。
在同一个表上尝试子查询,但似乎不起作用。

Select count(Distinct T.hhid) 
  from transactions 
 where hhid NOT IN (Select hhid 
                      from transactions 
                     where retailer_id IN (4,5,6,7,8,13,17,20,21,25,31) 
                       and transaction_date between '2018-08-01' and '2019-07-31'
                    ) as T 
 where T.retailer_id IN (27, 28, 29) 
   and transaction_date between '2018-08-01' and '2019-07-31'

ERROR : missing EOF at 'as' near ')'

如果你能在sparksql中提出一些同样适用于我的建议。

piv4azn7

piv4azn71#

您的查询在很多方面都没有意义:
这个 as T 最后是在错误的地方。你具体想做什么 T 代表?
你的两份名单 retailer_id 似乎是多余的。在一种情况下,您选择的记录
retailer_id IS NOT IN (4,5,6,7,8,13,17,20,21,25,31) 然后添加一个 WHERE 声明 retailer_id IS IN (27, 28, 29)`. 为什么两者都有?
我想你可能想重新开始,确定你的选择标准是什么,然后从那里开始。

7xllpg7q

7xllpg7q2#

尝试下面的脚本-

SELECT COUNT(Distinct T.hhid) 
FROM transactions T 
WHERE hhid NOT IN (
    Select hhid 
    from transactions 
    where retailer_id IN (4,5,6,7,8,13,17,20,21,25,31) 
    and transaction_date between '2018-08-01' and '2019-07-31'
) 
AND T.retailer_id IN (27, 28, 29) 
AND transaction_date between '2018-08-01' and '2019-07-31'

相关问题