如何获得满足多行条件的列

时间:2017-05-01 23:09:02

标签: sql hive

如果我们的数据类似于我将所有交易数据分组为月份和每月的item_count:

cust_id    item_month item_count
    64        1          1
    64        2          1
    64        3          1
    65        1          1
    66        2          1
    66        3          2

原始表是

cust_id  item_name transaction_date
  64       bag      2017-01-01
  64       bag      2017-02-05
  64       bag      2017-03-07
  65       bottle   2017-01-10
  66       gloves   2017-02-11
  66       bag      2017-03-05
  66       kite     2017-03-02

我想只获得cust_ids,在3个月(1,2和3)的每一个中我们都有item_count> = 1。我们怎么做到的?基本上在上述情况下答案是64。

SELECT 
  t1.cust_id
(SELECT 
   cust_id
FROM 
   table
WHERE item_count>=1 AND item_month=1) t1
JOIN 
(SELECT
   cust_id
FROM 
   table
WHERE item_count>=1 AND item_month=2) t2
ON t1.cust_id=t2_cust_id
JOIN
(SELECT
    cust_id
 FROM 
   table
 WHERE item_count>=1 AND item_month=3) t3
ON t1.cust_id=t3.cust_id

有更有效的方法吗?也许直接来自transaction_data?

2 个答案:

答案 0 :(得分:1)

尝试此查询:

SELECT cust_id
FROM table
WHERE item_count >= 1
GROUP BY cust_id
HAVING COUNT(DISTINCT item_month) = 3

答案 1 :(得分:0)

我不明白你在哪里得到64 ..但是这里是你如何在3个月内找到价值的客户。

SELECT cust_id
FROM table
where item_month in (1,2,3)
GROUP BY cust_id
HAVING count( distinct item_month ) = 3