Hive - select data from group with at least one row containing

zaitsman

Suppose i have a table in hive like so:

|Id|Data |Data2 |Groupkey|
|1 |One  |      |Group1  |
|2 |Two  |Stuff |Group1  |
|3 |Shoes|Some  |Group2  |
|4 |four |Stuff |Group2  |
|5 |Three|Notme |Group3  |

For each group that contains 'Stuff' in Data2 i want to get the row that has Groupkey and Data from the row other than the Stuff, and Data2 from 'Stuff' row.

So resulting data set would look something like

|Group |Data |Data2|
|Group1|One  |Two  |
|Group2|Shoes|four |

I was hoping to get something going with a GROUP BY, i started going with

SELECT Data, Groupkey FROM (SELECT Data, GroupKey FROM MyTable GROUP BY Groupkey) WHERE Data2 <> 'Stuff' but this fails suggesting i need to include Data in the group by but that is not what i want to group by?

And i'm not sure how to select just the groups containing one row with certain data.

David דודו Markovitz
select      Groupkey                                            as `Group`
           ,min (case when Data2 <> 'Stuff' then Data end)      as Data
           ,min (case when Data2 =  'Stuff' then Data end)      as Data2

from        MyTable

group by    Groupkey

having      count (case when Data2 = 'Stuff' then 1 end) > 0
;

+--------+-------+-------+
| group  | data  | data2 |
+--------+-------+-------+
| Group1 | One   | Two   |
| Group2 | Shoes | four  |
+--------+-------+-------+

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

from data table, randomly select one row per group

From Dev

Select one row from each group in a large data.table based on a condition

From Dev

SQL select & Group if at least one exists

From Dev

select one row per group with ifelse in data.table

From Dev

Remove group from data.frame if at least one group member meets condition

From Dev

Select from a table values that have at least one row matching different requirements

From Dev

Select from a table values that have at least one row matching different requirements

From Dev

How To select All Rows from a SAS data set that Match at least one value in another SAS Data set

From Dev

Exclude group if at least one row within it matches the condition

From Dev

Retrieve all the group records if at least one row matches the condition in MySQL

From Dev

Select two rows, using data from one row

From Dev

Hive select a particular string from a json row

From Dev

How to select the rows that contain a specific value in at least one of the elements in a row?

From Dev

MySQL - Select row only if at least one of values not null or empty

From Dev

How to select the rows that contain a specific value in at least one of the elements in a row?

From Dev

jqgrid select only one row per group

From Dev

Select from dual as one row?

From Dev

jquery datatables: Select Extension: How to select first row on init and how to get at least one row selected

From Dev

jquery datatables: Select Extension: How to select first row on init and how to get at least one row selected

From Dev

How to select pandas row with maximum value in one column, from a group of rows that share two common columns?

From Dev

How do I select one row but no more from foreign key ? ---- Group BY ---

From Dev

Select top N rows, first max row from each group distinct by one column (Spring,Hibernate,JPQL)

From Dev

not able to select the data from a table in hive

From Dev

Group several rows of data into one row by column?

From Dev

Select rows where at least one value from the list of columns is not null

From Dev

Select users from table that have at least one entry in diffierent table

From Dev

MySQL Select data from a row

From Dev

Select Top row from each group

From Dev

mysql select 2 row from each group by

Related Related

  1. 1

    from data table, randomly select one row per group

  2. 2

    Select one row from each group in a large data.table based on a condition

  3. 3

    SQL select & Group if at least one exists

  4. 4

    select one row per group with ifelse in data.table

  5. 5

    Remove group from data.frame if at least one group member meets condition

  6. 6

    Select from a table values that have at least one row matching different requirements

  7. 7

    Select from a table values that have at least one row matching different requirements

  8. 8

    How To select All Rows from a SAS data set that Match at least one value in another SAS Data set

  9. 9

    Exclude group if at least one row within it matches the condition

  10. 10

    Retrieve all the group records if at least one row matches the condition in MySQL

  11. 11

    Select two rows, using data from one row

  12. 12

    Hive select a particular string from a json row

  13. 13

    How to select the rows that contain a specific value in at least one of the elements in a row?

  14. 14

    MySQL - Select row only if at least one of values not null or empty

  15. 15

    How to select the rows that contain a specific value in at least one of the elements in a row?

  16. 16

    jqgrid select only one row per group

  17. 17

    Select from dual as one row?

  18. 18

    jquery datatables: Select Extension: How to select first row on init and how to get at least one row selected

  19. 19

    jquery datatables: Select Extension: How to select first row on init and how to get at least one row selected

  20. 20

    How to select pandas row with maximum value in one column, from a group of rows that share two common columns?

  21. 21

    How do I select one row but no more from foreign key ? ---- Group BY ---

  22. 22

    Select top N rows, first max row from each group distinct by one column (Spring,Hibernate,JPQL)

  23. 23

    not able to select the data from a table in hive

  24. 24

    Group several rows of data into one row by column?

  25. 25

    Select rows where at least one value from the list of columns is not null

  26. 26

    Select users from table that have at least one entry in diffierent table

  27. 27

    MySQL Select data from a row

  28. 28

    Select Top row from each group

  29. 29

    mysql select 2 row from each group by

HotTag

Archive