string-aggregation

How to get the values seperated by comma in a single column using SQL

亡梦爱人 提交于 2021-02-11 15:41:42
问题 I have the below table and expected result. Please let me know if it is possible to achieve the result. Please refer the picture attached. 回答1: You can use listagg() : select e.id, e.name, e.sal, listagg(d.dept, ',') within group (order by d.dept_id) as depts, listagg(d.dept_id, ',') within group (order by d.dept_id) as dept_ids, from employee e left join department d on e.name = d.name group by e.id, e.name, e.sal; Some comments on the data model. Your department table should have a dept_id

SQL Server: Combine multiple rows into one row from a join table?

徘徊边缘 提交于 2021-02-08 11:42:24
问题 I have an SQL query that looks like this. SELECT ID, Data1, DataFromTable2 FROM Table1 LEFT JOIN Table2 on Table1.ID = Table2.ID WHERE ID = 1 I'm trying to join the two tables where Table2 (which contains multiple rows with the same ID). And I want to the multiple rows into one row. I found a post here that combines the multiple rows into one. SQL Server: combining multiple rows into one row DECLARE @combinedString VARCHAR(MAX) SELECT @combinedString = COALESCE(@combinedString + ', ', '') +

SQL Server: Combine multiple rows into one row from a join table?

心已入冬 提交于 2021-02-08 11:42:09
问题 I have an SQL query that looks like this. SELECT ID, Data1, DataFromTable2 FROM Table1 LEFT JOIN Table2 on Table1.ID = Table2.ID WHERE ID = 1 I'm trying to join the two tables where Table2 (which contains multiple rows with the same ID). And I want to the multiple rows into one row. I found a post here that combines the multiple rows into one. SQL Server: combining multiple rows into one row DECLARE @combinedString VARCHAR(MAX) SELECT @combinedString = COALESCE(@combinedString + ', ', '') +

Teradata: Results with duplicate values converted into comma delimited strings

*爱你&永不变心* 提交于 2021-01-29 22:17:01
问题 I have a typical table where each row represents a customer - product holding. If a customer has multiple products, there will be multiple rows with the same customer Id. I'm trying to roll this up so that each customer is represented by a single row, with all product codes concatenated together in a single comma delimited string. The diagram below illustrates this After googling this, I managed to get it to work using the XMLAGG function - but this only worked on a small sample of data, when

Teradata: Results with duplicate values converted into comma delimited strings

一曲冷凌霜 提交于 2021-01-29 21:07:07
问题 I have a typical table where each row represents a customer - product holding. If a customer has multiple products, there will be multiple rows with the same customer Id. I'm trying to roll this up so that each customer is represented by a single row, with all product codes concatenated together in a single comma delimited string. The diagram below illustrates this After googling this, I managed to get it to work using the XMLAGG function - but this only worked on a small sample of data, when

String_agg in sql server 2016

风格不统一 提交于 2020-08-06 08:03:27
问题 Here is my code in sql server 2016 insert into @entdef_queries(entitydefid,squery) select A.entitydefid , ( select String_agg(cols,ioperator) from ( Select case when lower(b.metricdatatype) like 'string%' or lower(b.metricdatatype) like '%char%' or lower(b.metricdatatype) ='bit' or lower(b.metricdatatype) like 'date%' then ' lower("'+ b.metricname +'") ' + b.metriccondition +' '''+ b.value1 +''' ' when lower(b.metricdatatype) not like 'string%' and lower(b.metricdatatype) like '%char%' and

Postgres to fetch the list having comma separated values

强颜欢笑 提交于 2020-06-01 05:17:05
问题 I am working on Postgres SQL and having below join query, when I execute this query I get two or 3 records for each employee, because each employee has 3 different types of email address 1) Home Address 2) Office address 3) social address. select * from root.employee c full outer join root.employee_email ce on c.employee_id = ce.employee_id order by c.employee_id limit 1000 offset 0; What I want is that employee_email.email column to give the comma separated value in the output, how can we do

Postgres to fetch the list having comma separated values

自作多情 提交于 2020-06-01 05:15:09
问题 I am working on Postgres SQL and having below join query, when I execute this query I get two or 3 records for each employee, because each employee has 3 different types of email address 1) Home Address 2) Office address 3) social address. select * from root.employee c full outer join root.employee_email ce on c.employee_id = ce.employee_id order by c.employee_id limit 1000 offset 0; What I want is that employee_email.email column to give the comma separated value in the output, how can we do

CONCAT(column) OVER(PARTITION BY…)? Group-concatentating rows without grouping the result itself

末鹿安然 提交于 2020-05-11 04:46:07
问题 I need a way to make a concatenation of all rows (per group) in a kind of window function like how you can do COUNT(*) OVER(PARTITION BY...) and the aggregate count of all rows per group will repeat across each particular group. I need something similar but a string concatenation of all values per group repeated across each group. Here is some example data and my desired result to better illustrate my problem: grp | val ------------ 1 | a 1 | b 1 | c 1 | d 2 | x 2 | y 2 | z And here is what I