如何在Hive中编写子查询

时间:2014-07-11 05:19:01

标签: subquery hive

我被困在一个查询上。 我有像

这样的数据
  Uid, resp-date,camp-type

   1       201403    A
   1       201406    A
   1       201406    B
   1       201406    B
   1       201407    A
   1       201407    B
   2       201402    A
   2       201406    A
   2       201406    B

想要创建上个月提供的#of prod等指标, 计数逻辑是: 如果产品是A,则计算过去n个月内提供的产品数量 如果产品为B,则计算过去n个月提供的产品数量+当月提供的产品类型A数量

  Uid, resp-date,prod-type,#offered-last1month, #offered-last2month
    1       201403    A                0                  0
    1       201406    A                0                  0
    1       201406    B                1                  1
    1       201406    B                1                  1
    1       201407    A                3                  3
    1       201407    B                4                  4
     2       201402    A               0                  0
     2       201406    A               0                  0                     
     2       201406    B               1                  1

查询:

Select m.uid, m.respdate,m.prodtype, 
           case when m.prodtype ='A' then ca.num-mails-1month
                     when m.prodtype ='B' then cb.num-mails-1month
           end as mails-last1month
from m

 Left outer Join 
  ( select uid, count(* ) as num-mails-1month from 
      (
       What is a way around to write a sub query that refers m.resp-date
      ) 
  ) ca
 on ca.uid =m.uid

  Left outer Join 
     ( select uid, count(* ) as num-mails-1month from 
        (

         )

       ) cb
       on cb.uid =m.uid

0 个答案:

没有答案