Convert column value into CSV in SQL - sql

Convert column value into CSV in SQL:

You need string_agg, So it should be something like this-
SELECT
sr_number, name,
string_agg(city, ', ') WITHIN GROUP (ORDER BY city) AS city
FROM
table_name
GROUP BY
sr_number, name;

Related

BigQuery from column to rows by separator

I want to generate table and add all values per distinct id to one row using BigQuery
Example:
id label
000756f4-1af2-439b-b607-ce7384a6b8ee fast
000756f4-1af2-439b-b607-ce7384a6b8ee streaming
000756f4-1af2-439b-b607-ce7384a6b8ee other
0007bac4-1bed-4bf0-8b55-d21216723ef5 issue
000a03d2-f88c-4150-aa96-40b9fdaccb17 fast
000a03d2-f88c-4150-aa96-40b9fdaccb17 other
I would like to receive such table:
id label
000756f4-1af2-439b-b607-ce7384a6b8ee fast, streaming, other
0007bac4-1bed-4bf0-8b55-d21216723ef5 issue
000a03d2-f88c-4150-aa96-40b9fdaccb17 fast, other
Is it possible to achieve it with BigQuery?
You can just use string_agg():
select id, string_agg(label, ', ') as labels
from t
group by id;
Note that the ordering is arbitrary (and might even vary from one run to another). You might want to include an order by as well:
select id, string_agg(label, ', ' order by label) as labels
from t
group by id;
Update
Use string_agg:
select id, string_agg(label, ', ')
from mytable
group by id
Original answer
Use array_agg and array_to_string:
select id, array_to_string(array_agg(label), ', ')
from mytable
group by id

SQL Group By subquery in postgresql

I want to group by the foolowing table but I also need to group the column ID as depitected on the image.
SELECT SUM(ml),sku,name FROM consumos
GROUP BY sku,name
ORDER BY name
Any ideas?
Best regards
Looks like you want a JSON array. This can be done using jsonb_agg():
select name, sku, ml, jsonb_agg(id)
from the_table
group by name, sku, ml;
In standard SQL, this would look like:
select name, sku, ml,
'[' || listagg(id, ',') within group (order by id) || ']' as ids
from t
group by name, sku, ml
order by name, count(*);
However, not all databases support that standard operator || for string concatenation. And not all databases call their string aggregation operator listagg(). So you might need to tweak the query for your database.
EDIT:
In Postgres, this would be:
select name, sku, ml,
'[' || string_agg(id, ',' order by id) || ']' as ids
from t
group by name, sku, ml
order by name, count(*);

How to use LISTAGG and WHERE together

So I am trying to use a LISTAGG function while using a WHERE statement.
What I want to do is search by the WHERE statement and the LISTAGG returns a list with in relations to other columns.
In other words, when I use the WHERE statement with LISTAGG, I only get the value that I'm searching for. The other values associated with the other columns don't show up.
My script is kind of like this:
WITH TEST AS
(
SELECT DISTINCT
LOCATION,
ID,
LISTAGG (TOMATOCOUNT, ', ')
WITHIN GROUP (ORDER BY TOMATOCOUNT)
OVER (PARTITION BY LOCATION, ID) TOMATOTYPES,
FROM TOMATOLAND
)
SELECT
*
FROM TEST
WHERE (:TOMATOTYPE = TOMATO
OR :TOMATOTYPE IS NULL)
Filter on the result of LISTAGG strikes me as a bit backwords. Generally, if you're going to aggregate several items into a list, you're doing it at the last minute, for display purposes. ideally you would filter prior to the aggregation.
The following will return one row for each location/ID that contains the specified tomatotype.
SELECT DISTINCT
location,
id,
LISTAGG (tomatotype, ', ')
WITHIN GROUP (ORDER BY tomatocount)
OVER (PARTITION BY location, id)
tomatotypes
FROM tomatoland
WHERE (location, id) IN (SELECT location, id
FROM tomatoland
WHERE :tomatotype = tomatotype)
OR :tomatotype IS NULL
I think you want something like this:
WITH TEST AS (
SELECT LOCATION, ID,
LISTAGG(TOMATOTYPE, ', ') WITHIN GROUP (ORDER BY TOMATOCOUNT) OVER (PARTITION BY LOCATION, ID) as TOMATOTYPES
FROM TOMATOLAND
)
SELECT *
FROM TEST
WHERE ', ' || :TOMATOTYPE || ', ' LIKE '%,' || TOMATOTYPES || ', %' OR
:TOMATOTYPE IS NULL

SQL Inner Join related_Mahavir

I have 3 columns ID,NAME,VALUE as 1,2,3,4,5/A,B,C,A,B/9,9,9,9,9 so I want total SUM for particular name e.g. A-18, B-18, C-9...please answer the query in SQL form.
SELECT name, SUM(value) FROM whatevertable GROUP BY name
I assume you are asking to sum the columns & get an output based on your column Name.
Select Name + '-' + ltrim(rtrim(str( sum(Value) ))) from
tablename group by Name

How to Convert Column data into row data with comma separated value using SQL

I have a table Like as Follows
enter image description here
I want Out put like
enter image description here
how to Achieve this using SQL in Oracle database
You are looking for listagg(). The only caveat is that you need to specify the ordering for the values:
select stdname, listagg(marks, ', ') within group (order by ?) as marks
from t
group by stdname;
If you want them in order of the marks:
select stdname, listagg(marks, ', ') within group (order by marks desc) as marks
from t
group by stdname;