Using SQL I'd like to convert a table that looks like this
id
col11
col2
1
a
b
1
c
d
2
e
f
2
g
h
Into something that looks like this:
id
combined
1
[{col1: a, col2:b}, {col1: c, col2:d}]
1
[{col1: e, col2:f}, {col1: g, col2:h}]
We can try to use json_build_object function to build a JSON object out of a variadic argument list then use json_agg function.
SELECT id,
json_agg(json_build_object('col1',col11,
'col2',col2)) combined
FROM t
GROUP BY id
sqlfiddle
Related
I have a table with id and different values. I want to have my output something which would looks like this
id value
----------
1 t
1 f
2 t
3 f
4 f
4 f
Expected output
id value
---------
3 f
4 f
If we look at the output, my condition here to check if my id has all f as value then return f, if it has all t then don't, and if any one of the id has t also don't include row in output.
How to achieve this ?
create subquery and exclude the values accordingly. i think hiveql supports where clause subqueries.
select id, value
from your_data_source
where id not in
(select id from your_data_source where value='t' group by id)
group by id, value
I have a presto with a column of string arrays I would like to convert to a table of each element in the array mapped to its number of occurrences.
A, B, C, D, E, F are all strings
set
---------
[A,B,C,D]
[A,C,E,F]
string|count
-------------
A 2
B 1
C 2
D 1
E 1
F 1
Well, you can use unnest() and aggregate:
select char, count(*)
from t cross join
unnest(t.set) as u(char)
group by char
My input is:
a b c
-------
A 5 3
A 4 2
B 3 1
B 5 3
I would like to get all a values having the same values in b and c, so the output should be as:
{A,B} 5 3
I am using the group by, but I am not achieving my goal.
In standard SQL, this would look like:
select b, c, listagg(a, ',') within group (order by a)
from t
group by b, c;
Not all databases support listagg(), but most have a method for concatenating strings.
In Hive, you would use collect_list() or collect_set():
select b, c, collect_list(a, ',')
from t
group by b, c;
You can convert the array back to a string, but I recommend keeping it as an array.
I'm trying to extract elements from a simple JSON array in a PostgreSQL table like:
id(int) vtags(jsonb)
-------- -----------------------
1 {"tags": ["a","b","c"]}
2 {"tags": ["x","y"]}
I would like to devise a SELECT statement to produce an output like:
id tags
--- -----------------------
1 a
1 b
1 c
2 x
2 y
Use jsonb_array_elements() to unnest the elements of the array:
select t.id,
jt.tag
from the_table t
cross join jsonb_array_elements_text(jt.vtags -> 'tags') as jt(tag)
order by t.id;
I have a table looking like this
NAME DATE
--------------------
A 2014.01.01
A 2014.01.02
B 2014.01.05
B 2014.01.06
B 2014.01.07
C 2014.01.08
C 2014.01.10
C 2014.01.11
B 2014.01.13
B 2014.01.15
B 2014.01.18
How can I extract the first occurrence of each 'NAME' sequence?
Meaning I would like to have the following returned:
NAME DATE
A 2014.01.01
B 2014.01.05
C 2014.01.08
B 2014.01.13
Just use min():
select name, min(date) as date
from table t
group by name;
A basic group by and use of the MIN function should do it:
SELECT NAME, MIN(DATE) FROM Table GROUP BY NAME
SQL Server provides a number of different aggregate functions that allow you to do this type of thing.