-1

I have small issue in hive logic. I need to find longest customer in my data set.My dataset contains values as below

customer       time
 cust 1        8:01
 cust 1        8:02
 cust 2        8:02
 cust 2        8:03
 cust 2        8:04
 cust 2        8:05
 cust 3        8:02
 cust 3        8:03

Here in my example, cust 2 has the longest customer based on the total time he/she has engaged. cust 1 total count is 2, cust 2 count is 4 and cust 3 count is 2. My issue between 8:01 to 8:05, the longest duration belongs to cust 2 . Hence cust 2 should come as result. How can i implement this logic.

Please help

Note: need solution without limit operation

BigD
  • 850
  • 2
  • 17
  • 40

2 Answers2

1

Something like this:

select customer
from t
where time >= '8:01' and time <= '8:05'
group by customer
order by count(*) desc
limit 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

I think this will give the desired

    Select distinct customer from table where 
     (distinct time) = (select max(distinct time) from table)

or if you want desired as per group reduction

         Select  customer from( 
           (Select customer,(max(time)-
         min(time)) as time_diff
          from table group by customer
         order by time_diff desc LIMIT 1)
          ;
Himanshu
  • 3,830
  • 2
  • 10
  • 29