Presto equivalent of MySQL group_concat - mysql

I'm new to Presto and looking to get the same functionality as the group_concat function in MySQL. Are the following two equivalent? If not, any suggestions for how I can recreate the group_concat functionality in Presto?
MySQL:
select
a,
group_concat(b separator ',')
from table
group by a
Presto:
select
a,
array_join(array_agg(b), ',')
from table
group by a
(Found this as a suggested Presto workaround here when searching group_concat functionality.)

Try using this in place of group_concat in Presto ::
select
a,
array_join(array_agg(b), ',')
from table
group by a

Also, if you're looking for unique values only – an equivalent to group_concat(distinct ... separator ', ') – try this:
array_join(array_distinct(array_agg(...)), ', ')

There's no function as of this answer, though the feature has been requested.
The closest equivalent is mentioned in your question.
WITH tmp AS (
SELECT 'hey' AS str1
UNION ALL
SELECT ' there'
)
SELECT array_join(array_agg(str1), ',', '') AS joined
FROM tmp

Related

get number between two string by using substring

I m trying to get number in between "sims_7009_alaira", i want 7009.
SELECT sno,dbase, SUBSTRING_INDEX(dbase, 'sims_', -1)temp
FROM school
How should i do that in SQL
Give this a try:
select substring_index(SUBSTRING_INDEX(dbase, '_', 2),'_',-1) from school;
Check this here:
SQL Fiddle
Just use substring_index() two times:
SELECT sno, dbase, substring_index(substring_index(dbase, 'sims_', -1), '_alaira', 1) as number FROM school
Do this instead:
SELECT sno,dbase, SUBSTRING_INDEX(SUBSTRING_INDEX(dbase, "_", 2),'_',-1) temp
FROM school;
For more insight see this.

How to change MySQL GROUP_CONCAT to PostgreSQL string_agg?

I'm migrating some MySQL code to Postgres and having a heck of a time converting the following line:
GROUP_CONCAT(
DISTINCT id,':',foo,':',bar ORDER BY id
) as id,
This results in a comma separated list of strings like:
id:foo:bar,id2:foo2:bar2
The 'DISTINCT' is there to avoid duplicates.
I've read the equivalent of GROUP_CONCAT in Postgres is string_agg, but I can't figure out how to make it work the same way.
Edit: I may have almost answered my own question. I came up with the solution of using CONCAT. The problem with this is that now sorting is done by the concatenated string rather than by the id:
string_agg(DISTINCT CONCAT(
id::text, ':', foo, ':', bar, ':'
), ',') as id
If I try to add 'ORDER BY id' I get an error.
You can do something like below.
select
string_agg(DISTINCT CONCAT(
id::text, ':', foo, ':', bar, ':'
), ',') as id from (select * from table t order by id) as al

Mysql Subquery as input second query

I want to calculate distance from mysql record, firstly I get all cordinates and saved as LineString object, but I have error. What is wrong with my sql?
WITH tmp AS
(SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') FROM track WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate)
SELECT ST_Length(ST_GeomFromText(tmp));
Firstly, MySQL doesn't support the WITH clause; secondly, you should define variables like 10.4 User-Defined Variables.
You can change you sql to this;)
SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') INTO #tmp
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;
SELECT ST_Length(ST_GeomFromText(#tmp));
Or just with one query:
SELECT ST_Length(ST_GeomFromText(GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ')))
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;

Select substring group by

I have data in column which I want to select with substring_index and group by the result of substring. Is it possible to make in one query?:
Example
code:
R0001.10
R0001.20
R0002.10
R0002.30
If use
SELECT SUBSTRING_INDEX(code, '.', 1) FROM products;
It goes like this:
R0001
R0001
R0002
R0002
But when I use
SELECT SUBSTRING_INDEX(code, '.', 1) FROM products GROUP BY code;
It gave some strange result
01
01
010210000
0103020
etc.
There is no issue using GROUP BY clause check SQL Fiddle. Might be other issue.

Finding language in string (doing substring?) with MySQL

I have a table with languages which s_name value looks like this:
'en_UK'
'en_US'
'de_CH'
'de_AT'
I want to get all the distinct languages, without the country part. So for example, in case I just had those of the example, I would need to get:
en
de
What would be the best way of doing so?
I have this right now:
SELECT DISTINCT SUBSTRING(name,1,2)
FROM my_languages
Try this:
SELECT DISTINCT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
OR
SELECT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
GROUP BY langName
Check this link MySQL STRING Functions
Here is a simple way:
select distinct left(s_name, 2)
from t
This assumes the language name is the left two characters.
This will work in Oracle as well as in MySql I think:
SELECT SUBSTR('en_UK',INSTR('en_UK','_')+1) country FROM dual
/