string-aggregation

Concatenate multiple values to a single column using stuff() in SQL

余生长醉 提交于 2019-11-29 18:04:32
ID | NAME ----|-------- 1 |Ann 2 |Jake 1 |Julie 3 |Paul 2 |Shane 4 |Kumi I want to concatenate values using stuff() as below. And the single values should not be affected by the stuff() function. ID | NAME ----|-------- 1 |Ann,Julie 2 |Jake,Shane 3 |Paul 4 |Kumi How to do that? CREATE TABLE #A (ID INT, NAME VARCHAR(10)) INSERT INTO #A VALUES (1,'ANN'), (2,'JAKE'), (1,'JULIE'), (3,'PAUL'), (2,'SHANE'), (4,'KUMI') SELECT DISTINCT ID , STUFF((SELECT ','+NAME FROM #A T1 WHERE T1.ID=T2.ID FOR XML PATH('')),1,1,'') FROM #A T2 STUFF doesn't do concatenation,Its for XML which does this.Stuff only

Produce DISTINCT values in STRING_AGG

独自空忆成欢 提交于 2019-11-29 13:46:10
I'm using the STRING_AGG function in SQL Server 2017. I'd like to create the same effect as COUNT(DISTINCT <column>) . I tried STRING_AGG(DISTINCT <column>,',') but that is not legal syntax. I'd like to know if there is a T-SQL work-around. Here is my sample: WITH Sitings AS ( SELECT * FROM (VALUES (1, 'Florida', 'Orlando', 'bird'), (2, 'Florida', 'Orlando', 'dog'), (3, 'Arizona', 'Phoenix', 'bird'), (4, 'Arizona', 'Phoenix', 'dog'), (5, 'Arizona', 'Phoenix', 'bird'), (6, 'Arizona', 'Phoenix', 'bird'), (7, 'Arizona', 'Phoenix', 'bird'), (8, 'Arizona', 'Flagstaff', 'dog') ) F (ID, State, City,

how to concatenate strings?

只愿长相守 提交于 2019-11-29 11:11:29
I'm on Oracle 10g and have the following table structure: id, paragraph I want to group by id and concatenate the paragraphs. Each paragraph maybe 1500 characters or more. When I try the wm_concat function, it complains that the string buffer is too small. I actually tried many of the examples on Oracle's website and they all fail with the error the string buffer is too small. select id, wm_concat(paragraph) from paragraphs group by id how do I solve this? So, I'm guessing the error is ORA-06502 and I can see how you might think that this doesn't apply to you in this situation. However, this

Concat the second column value if the first column value is same

十年热恋 提交于 2019-11-28 14:50:33
I have a query like below and listed output of it: SELECT DISTINCT TRACKING_NUM,TITLE_OF_DOC_SEC FROM some_table WHERE TRACKING_NUM IS NOT NULL; o/p: TRACKING_NUM TITLE_OF_DOC_SEC 007 Email Flow 007 Test Bug 53306 007 Title 1119 007 Title Test 007 test bug 009 1156 089 Title 21173 098 test Doc Section I want to re write the query so that get an output to be like this: TRACKING_NUM TITLE_OF_DOC_SEC 007 Email Flow,Test Bug 53306,Title 1119,Title Test,test bug 009 1156 089 Title 21173 098 test Doc Section can anyone help? Use Listagg() in 11g or WM_Concat() in 10g: SELECT LISTAGG(TITLE_OF_DOC_SEC

How to write a query that does something similar to MySQL's GROUP_CONCAT in Oracle?

隐身守侯 提交于 2019-11-28 14:29:29
When I run the following SQL query in my Oracle database: SELECT p.pkt_nazwa, u.us_nazwa FROM punkty p, kategorie_uslug ku, usluga u WHERE ku.pkt_id = p.pktk_1_id AND ku.us_id = u.usk_1_id ORDER BY p.pkt_nazwa; I get the following output: NAME | SERVICE --------------------|------------------- Baita De Mario | WC Baita De Mario | Kuchnia Baita De Mario | Nocleg Bistro-Cafe | Bar Bistro-Cafe | Dyskoteka Bistro-Cafe | Kuchnia How would I go about to get the following output? NAME | SERVICES --------------------|------------------- Baita De Mario | WC, Kuchnia, Nocleg Bistro-Cafe | Bar, Dyskoteka

string_agg for sql server pre 2017

爷,独闯天下 提交于 2019-11-28 12:11:50
Can anyone help me make this query work for sql server 2014. This is working on Postgresql and probably on sql server 2017. On Oracle it is listagg instead of string_agg. Here is the sql: select string_agg(t.id,',') AS id from Tabel t I checked on the site some xml option should be used but I could not understand it. In SQL Server pre-2017, you can do: select stuff( (select ',' + cast(t.id as varchar(max)) from tabel t for xml path ('') ), 1, 1, '' ); The only purpose of stuff() is to remove the initial comma. The work is being done by for xml path . 来源: https://stackoverflow.com/questions

Output a comma-separated list in a column in SQL Server [duplicate]

拥有回忆 提交于 2019-11-28 11:12:49
问题 Possible Duplicate: Simulating group_concat MySQL function in SQL Server 2005? What's the best way to achieve this? Edit: in MSSQL 2005 Table a | b ------------ x | 1 x | 4 y | 6 y | 1 Query: SELECT ? FROM Table so that output is : a | ListOfB ------------ x | 1, 4 y | 6, 1 回答1: In SQL Server you can use FOR XML PATH : select distinct a, stuff( ( select ','+ cast(b as varchar(10)) from table1 t2 where t1.a = t2.a for XML path('') ),1,1,'') ListOfB from table1 t1 See SQL Fiddle with Demo 来源:

Produce DISTINCT values in STRING_AGG

狂风中的少年 提交于 2019-11-28 07:19:52
问题 I'm using the STRING_AGG function in SQL Server 2017. I'd like to create the same effect as COUNT(DISTINCT <column>) . I tried STRING_AGG(DISTINCT <column>,',') but that is not legal syntax. I'd like to know if there is a T-SQL work-around. Here is my sample: WITH Sitings AS ( SELECT * FROM (VALUES (1, 'Florida', 'Orlando', 'bird'), (2, 'Florida', 'Orlando', 'dog'), (3, 'Arizona', 'Phoenix', 'bird'), (4, 'Arizona', 'Phoenix', 'dog'), (5, 'Arizona', 'Phoenix', 'bird'), (6, 'Arizona', 'Phoenix'

how to concatenate strings?

孤街醉人 提交于 2019-11-28 04:21:17
问题 I'm on Oracle 10g and have the following table structure: id, paragraph I want to group by id and concatenate the paragraphs. Each paragraph maybe 1500 characters or more. When I try the wm_concat function, it complains that the string buffer is too small. I actually tried many of the examples on Oracle's website and they all fail with the error the string buffer is too small. select id, wm_concat(paragraph) from paragraphs group by id how do I solve this? 回答1: So, I'm guessing the error is

MSSQL - GROUP_CONCAT

天涯浪子 提交于 2019-11-28 02:28:22
Here is the sample data : IdProduit Localisation Qte_EnMain 4266864286880063006 E2-R40-B-T 13.00000 4266864286880063006 E2-R45-B-T 81.00000 4266864286880063007 E2-R45-C-T 17.00000 4266864286880063008 E2-R37-B-T 8.00000 And this is what i would like to have IdProduit AllLocalisation 4266864286880063006 E2-R40-B-T (13), E2-R45-B-T (81) 4266864286880063007 E2-R45-C-T (17) 4266864286880063008 E2-R37-B-T (8) I watched all the examples of GROUP_CONCAT on the forum and I tried several tests. I don't really understand STUFF(). Here is what i would like to do : SELECT a.IdProduit, GROUP_CONCAT( CONCAT