1

I use beeline, which is a JDBC client based on SQLLine, to run Hive queries. I would like to select from a liste of values like:

SELECT DISTINCT * FROM (a,b,b,c,d,..z)

is that posssible?

leftjoin
  • 36,950
  • 8
  • 57
  • 116
HISI
  • 4,557
  • 4
  • 35
  • 51

3 Answers3

2
SELECT DISTINCT *
FROM (
    SELECT 12
    UNION SELECT 23
    UNION SELECT 34
    UNION SELECT 12
    UNION SELECT 23
) AS t1;

PS: also refer to very closely related question How can I select from list of values in SQL Server

arhak
  • 2,488
  • 1
  • 24
  • 38
  • 1
    Is there something like `select * from [a,b,c,d,e, f...z]` because I have a lot of values and it painful to write such a query – HISI Feb 21 '18 at 13:50
  • 1
    `DISTINCT` is superfluous here. A`UNION` operation always returns unique rows. But, a `UNION ALL` with `DISTINCT` would make sense. – Kaushik Nayak Feb 22 '18 at 03:38
1

Also you can use stack, it works much faster than UNION:

hive> select id from
    > (
    > select stack(5, --the number of elements
    > 10 ,
    > 20 ,
    > 30 ,
    > 40 ,
    > 50
    > ) as (id)
    > )s;
OK
10
20
30
40
50
Time taken: 4.88 seconds, Fetched: 5 row(s)
leftjoin
  • 36,950
  • 8
  • 57
  • 116
0

You may try split and explode

 SELECT DISTINCT * FROM 
 (
 select explode( split('a,b,b,c,d,e,f' ,',')) c
 ) as t
 ORDER BY c
Kaushik Nayak
  • 30,772
  • 5
  • 32
  • 45