string-aggregation

Why does the wm_concat not work here?

老子叫甜甜 提交于 2019-12-17 10:06:16
问题 I have this query : (SELECT OBJECT_ID from cr_object_group_entries_vw where object_group_id IN (SELECT ITEM FROM TABLE(CR_FN_SPLIT_STRING('28,56',',')))) that returns : But when I do : SELECT wm_concat(object_id) FROM (SELECT OBJECT_ID from cr_object_group_entries_vw where object_group_id IN (SELECT ITEM FROM TABLE(CR_FN_SPLIT_STRING('28,56',',')))) I get a blank result... what am I doing wrong? 回答1: You must avoid wm_concat function because it is undocumented and discovered as workaround at

I need result from select statement as multiple data of birth for a single id

老子叫甜甜 提交于 2019-12-13 16:55:06
问题 APPLICANT_ID DATE_OF_BIRTH 206209579 04/29/82 206209579 04/29/82 203276426 06/01/69 203276426 02/03/96 203276426 06/02/99 I need result as 206209579 04/29/82,04/29/82 203276426 06/01/69,02/03/96 please suggest its need full 回答1: In Oracle 11g and above, you can use listagg: SELECT applicant_id, LISTAGG(date_of_birth) WITHIN GROUP (ORDER BY date_of_birth) FROM my_table GROUP BY applicant_id 回答2: As an alternative, for Oracle version prior to 11g where LISTAGG is not supported, you could use

How to concatenate rows into one cell and add break line after each one using SQL Server

回眸只為那壹抹淺笑 提交于 2019-12-13 02:27:19
问题 I am using SQL Server 2017 and I am trying to create a query that concatenates the languages and Levels of Proficiency in one line for every Employee. The table that stores the info in my SQL Database is this for example: And the end result I would like to achieve is this: Using Stuff function and xml path I have managed to create a select query that shows this: But I can't find a way to insert a break line. The query will be used as a datasource for an AspxGridview. Any help? Thank you in

Create a User defined function like SQL server 2017 STRING_AGG on earlier versions

假装没事ソ 提交于 2019-12-12 15:39:46
问题 I try to create a generic function that can be used like this example of using the new string_agg built-in function on SQL Server 2017 the inside implementation can be something like the follow with tbl as( select a.Id, c.Desc from TableA a join TableB b on b.aId = a.Id join TableC c on c.Code = b.bCode ) select distinct ID , STUFF(( select ', ' + Desc from tbl t where t.ID = tbl.ID for xml path(''),TYPE).value('.','VARCHAR(MAX)'),1,2,'') Desc from tbl But how to receives the field key, the

How to concatenate text from multiple rows into a single text string in Oracle server?

自古美人都是妖i 提交于 2019-12-12 06:54:40
问题 I have a table with data like this: ID, END_DATE, CLOSE_DATE 1, 2018-05-18, 2018-05-15 2, 2018-05-18, 2018-05-14 3, 2018-05-18, 2018-05-11 4, 2018-05-18, 2018-05-10 I need the output when i query in Oracle server END_DATE CLOSE_DATE ID 2018-05-18 [2018-05-15,2018-05-14,2018-05-11,2018-05-10] [1,2,3,4] I have tried to use listagg, but its working either for Id or Close Date but not both. select (listagg(CLOSE_DATE,',') within group (order by CLOSE_DATE DESC)) CLOSE_DATE , END_DATE from TBL_S

SELECT returning multiple rows as string

点点圈 提交于 2019-12-12 00:39:11
问题 I have a table: Contracts: contractid | contract name "1" | "MAG:001" "2" | "MAG:002" -- and -- Devices: devid | serialnum | fk_contractid 10 | 1234 | 1 11 | 5678 | 1 12 | 4321 | 2 13 | 8765 | 2 devices.fk_contractid = contracts.contractid I need to make select which will give result: "MAG:001" | 1234, 5678 "MAG:002" | 4321, 8765 How can that be done in PL-SQL? 回答1: Assuming 11g (when listagg was introduced): select CONTRACT_NAME || '|' || LISTAGG(D.SERIALNUM, ',') within group (order by

Stuff Query with multiple joins

跟風遠走 提交于 2019-12-11 17:17:53
问题 SELECT S.enroll_no, Pm.periodname FROM studentattendencedetails AS SA LEFT JOIN studentattendencemaster AS SM ON SA.attendencemasterid = SM.id LEFT JOIN tbl_periodmaster AS Pm ON SA.periodid = Pm.id LEFT JOIN students AS S ON SA.studentid = S.id WHERE SA.isabsent = 2 AND s.enroll_no IN ('YYYYYYYYY','XXXXXXXXXX') ORDER BY S.enroll_no Output: Enroll_No PeriodName ----------------------- YYYYYYYYY 1 YYYYYYYYY 2 XXXXXXXXXX 6 XXXXXXXXXX 1 XXXXXXXXXX 7 I would like to display the period name with

String formatting using LISTAGG in Oracle. Escaping single quote ` ' `

試著忘記壹切 提交于 2019-12-11 06:58:30
问题 How can I format the output of listagg in Oracle to produce output(every field in the single quote) as 'student1', 'student2', 'student3' . I have gone through documentation and other question on listagg but can't find much. SQL Query to concatenate column values from multiple rows in Oracle SELECT LISTAGG(student_name,',') WITHIN GROUP (ORDER BY student_name) from students Thanks 回答1: You could use: SELECT LISTAGG('''' || student_name || '''',',') WITHIN GROUP (ORDER BY student_name) FROM

XML PATH SQL SERVER

与世无争的帅哥 提交于 2019-12-11 05:46:07
问题 I have data TIPE| NILAI PSX 10 GE 10 EG 10 SX 20 SXL 20 SZ 20 how to get output like psx/ge/eg = 10 sx/sxl/sz = 20 and this is my sytax ISNULL(CAST((SELECT CONVERT(VARCHAR,TIPE) + ' / ' FROM tabel FOR XML PATH('')) AS varchar(max)),'') 回答1: Try this DECLARE @tbl table (TIPE varchar(5), NILAI int); insert into @tbl values ('PSX', 10), ('GE', 10), ('EG', 10), ('SX', 20), ('SXL', 20), ('SZ', 20); SELECT DISTINCT STUFF((SELECT '/ '+TIPE FROM @tbl i WHERE i.NILAI=o.NILAI FOR XML PATH ('')),1,1,'')

PostgreSQL - JOIN on string_agg

白昼怎懂夜的黑 提交于 2019-12-11 04:26:04
问题 The title may be misleading because I just can't seem to find the right for it. But here it goes. I have three tables. Students student_id | name 1 Rhon Subjects subject_id | subject_name | student_id 1 Physics 1 2 Math 1 Grades grade_id | student_id | subject_id | grade 1 1 1 90 2 1 2 89 3 1 2 88 I would like to result to be like this: student_id | student_name | subject_name | grades 1 Rhon Physics 90 1 Rhon Math 88,89 My current query is: SELECT students.student_id, subjects.subject_id,