Search code examples
hivehiveqlhadoop2

Hive how to combine multiple records within a group based on condition


i have dataset as following.

client-Id   Name    HasCar     HasHome
A01          ABC      Y          N
A01          ABC      N          N
B01          EFG      N          N
B01          EFG      N          Y

From here I need to derive a Single row for each customer whether he has car or home. the expected output should look like below

Client-Id    Name    HasCar     HasHome
A01          ABC      Y          N
B01          EFG      N          Y

This needs to be done using a Hive-QL


Solution

  • Use max() aggregation:

    select client-Id,Name, max(HasCar) HasCar,  max(HasHome) HasHome
      from your_table 
     group by client-Id,Name