HIVE GROUP_CONCAT with ORDER BY

Nageswaran picture Nageswaran · Feb 28, 2018 · Viewed 7.5k times · Source

I have a table like

Source table

I expect the output like this (group concat the results in one record, and the group_concat should sort the results by value DESC).

Destination table

Here is the query I tried,

SELECT id,
       CONCAT('{',CONCAT_WS(',',GROUP_CONCAT(CONCAT('"',key, '":"',value, '"'))), '}') AS value
FROM
    table_name
GROUP BY id

I want the value in the destination table should be sorted (descending order) by source table value.

To do that, I tried doing GROUP_CONCAT(... ORDER BY value).

Looks like Hive does not support this. Is there any other way to achieve this in hive?

Answer

Abbas Gadhia picture Abbas Gadhia · Feb 28, 2018

Try out this query.

Hive does not support the GROUP_CONCAT function, but instead you can use the collect_list function to achieve something similar. Also, you will need to use analytic window functions because Hive does not support ORDER BY clause inside the collect_list function

select
  id,
  -- since we have a duplicate group_concat values against the same key
  -- we can pick any one value by using the min() function
  -- and grouping by the key 'id'
  -- Finally, we can use the concat and concat_ws functions to 
  -- add the commas and the open/close braces for the json object
  concat('{', concat_ws(',', min(g)), '}') 
from
  (
    select
      s.id,
      -- The window function collect_list is run against each row with 
      -- the partition key of 'id'. This will create a value which is 
      -- similar to the value obtained for group_concat, but this 
      -- same/duplicate value will be appended for each row for the 
      -- same key 'id'
      collect_list(s.c) over (partition by s.id 
        order by s.v desc 
      rows between unbounded preceding and unbounded following) g 
    from
      (
        -- First, form the key/value pairs from the original table.
        -- Also, bring along the value column 'v', so that we can use
        -- it further for ordering
        select
          id,
          v,
          concat('"', k, '":"', v, '"') as c 
        from
          table_name -- This it th
      )
      s
  )
  gs 
-- Need to group by 'id' since we have duplicate collect_list values
group by
  id