场景:同一张表中,需要找到表中某些特定商品在品类中sku数量和销售额所占的比例。
由于Hive 中不支持 in 用法 ( select xxx from table_namewhere product_id in (select xxx from table)这种写法不可)
子查询必须用 impala 的semi left join 写法
就是说把联表条件写成semi join,但是这样有个问题,没办法取到b的值
比如以下语句,要用b.product_id 做计算,就不能用LEFT SEMI JOIN,只能用JOIN
b表必须用JOIN,且不能算精确值,只能使用NDV算近似值
// 这种写法,只能算a表的product_id;如果要计算b表的内容联表必须用JOIN
// 且不能算b表的精确值,只能使用NDV算近似值
SELECT a.category_name AS category_name,
count(DISTINCT a.product_id) AS total_distinct_count,
NDV(DISTINCT b.product_id) AS distinct_ndv_count
FROM {ds} a LEFT SEMI
JOIN {ds} b ON(a.category_name = b.category_name
AND b.product_id IN {product_clause}
AND b.month >= '{date_start}'