I'm using GROUP_CONCAT to build up Quartiles and am unsure how to get it working on scores created from two columns.
My GROUP_CONCAT looks like this - but at the moment is ordering on 'gst.raw_gps' when it should be ordering on the values arrived at through '(100/gst.max_gps)*gst.raw_gps'
GROUP_CONCAT(
(100/gst.max_gps)*gst.raw_gps
ORDER BY gst.raw_gps
SEPARATOR ','
)
Any advice much appreciated
Then use the expression instead of the column name:
GROUP_CONCAT((100/gst.max_gps)*gst.raw_gps
ORDER BY (100/gst.max_gps)*gst.raw_gps
SEPARATOR ','
)
Related
Group concat is driving me nuts.
I have 1 table 2 columns
FRUIT QUANTITY
APPLE 4
ORANGE 6
I'd like to group concat these into one field
orange:4, apples:6
You can concat() both columns for each row, then group_concat() the results over all rows:
select group_concat(concat(fruit, ':', qty) separator ', ') res from mytable
You might want to consider adding an order by to group_concat() in order to get predictable, consistent results.
You can list multiple columns and other values in the GROUP_CONCAT() call, and it will concatenate them together.
SELECT GROUP_CONCAT(fruit, ':', qty SEPARATOR ', ') AS fruits
FROM yourTable
If you have lots of columns that you're joining like this, you could use CONCAT_WS() to concatenate them all together with the same delimiter. See GROUP_CONCAT multiple columns as an array or exolodable string
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
I have a mysql table where I some data are repeated in one column but have different value in another. I want to concat them an them and create new string.
I am getting data in following format:
See rows with checkbox option and vodaphoneissues and comments are repeated twice, yet have different values in another column.
I want to concatenate that value with comma separated format.
There is a function called GROUP_CONCAT.
I do not know names of your columns, let's say they are COLUMN1, COLUMN2, etc. The code will be:
SELECT COLUMN3, GROUP_CONCAT(COLUMN4) FROM your_table GROUP BY COLUMN3;
Use GROUP_CONCAT for that purposes
SELECT id, GROUP_CONCAT(DISTINCT column_name ORDER BY column_name SEPARATOR ', ')
FROM table
GROUP BY comments, versions;
Also pay attention to GROUP BY clouse as it groups by few columns:
GROUP BY comments, versions;
I want to fetch primary key of all rows of one table in a single query.
I implement it using query --
SELECT GROUP_CONCAT( id SEPARATOR ',' ) AS ids
FROM tbl_facebook_users
WHERE facebook_user_id
IN ( 336120419901063, 10205028697461204 )
It is giving result as:
Result :-
ids
-----------
[BLOB - 4B]
What is the solution to implement this feature and get correct result??
With the reference to this post : using group_concat in PHPMYADMIN will show the result as [BLOB - 3B]
CONVERT(GROUP_CONCAT( id SEPARATOR ',' ) USING 'utf8')
use this
Show BLOB contents
and don't use SEPARATOR in your query if you want SEPARATOR like ',' cause in the MySQL by default SEPARATOR is ','
Problem Solved by using following query :
select CONVERT(GROUP_CONCAT(id) USING utf8) as ids from tbl_facebook_users
It bydefault separate ids by comma ','
If you want to use any other separator (say ';') use it as --
select CONVERT(GROUP_CONCAT(id SEPARATOR ';' ) USING utf8) as ids from tbl_facebook_users
I am trying to get distinct 'productnumber' ordered by the 'createdate' but at the end I need only "productnumber" sorted by 'createdate' (I cannot use top as the 'productnumber' varies in a wide flexible range).
The distinct product number is needed to be concatenated with comma (,) for e.g.
123,245,787,875 (each number represents a productnumber)
The required query looks like somewhat (this query doesn't work and gives an error The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified):
SELECT stuff(
(
SELECT distinct ',' + productnumber
FROM Product
ORDER BY createdate
FOR XML path('')
)), 1, 1, '')
Could anyone help in how to go about to get the desired result.
As Mikael Eriksson suggested, using Group By clause solved the problem. Thanks Mikael
SELECT stuff((SELECT ',' + productnumber
FROM product a
ORDER BY a.createdate FOR xml path('')),1,1,'')
AS destn_str