string-aggregation

Query to get multiple row into single row

耗尽温柔 提交于 2020-01-10 05:10:06
问题 I have a table in which following information are there: ITEM WH BATCH DOC NO CLD1 FN B1 3 CLD1 FN B1 3 CLD1 FN B2 3 CLD1 FN B2 3 CLD1 FN B3 3 CLD1 FN B4 3 This is the code which I have used to bring the above values: select T0.item,t0.wh,t0.batchnum from oibt t0 where t0.DOCNO = '3' and t0.Wh = 'FN' I need the output like this: ITEM WH BATCH CLD1 FN B1,B2,B3,B4 I have used STUFF & For XML coding too but I am not getting the desired output. 回答1: The following query should do what you want:

DB ORACLE QUERY

老子叫甜甜 提交于 2020-01-05 06:22:28
问题 I have a table where storing details ID NAME 1 A 2 A 1 A I need the output like ID Name Count 1,2 A 3 Please help to get the output like that in oracle select query 回答1: In Oracle, you can use listagg() , but it has no distinct option. So, use a subquery and two levels of aggregation: select listagg(id, ',') within group (order by id) as id, name, sum(cnt) from (select id, name, count(*) as cnt from t group by id, name ) x group by name; 来源: https://stackoverflow.com/questions/49276032/db

DB ORACLE QUERY

可紊 提交于 2020-01-05 06:22:22
问题 I have a table where storing details ID NAME 1 A 2 A 1 A I need the output like ID Name Count 1,2 A 3 Please help to get the output like that in oracle select query 回答1: In Oracle, you can use listagg() , but it has no distinct option. So, use a subquery and two levels of aggregation: select listagg(id, ',') within group (order by id) as id, name, sum(cnt) from (select id, name, count(*) as cnt from t group by id, name ) x group by name; 来源: https://stackoverflow.com/questions/49276032/db

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

戏子无情 提交于 2019-12-29 09:37:10
问题 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? 回答1: 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

'stuff' and 'for xml path('')' from SQL Server in Postgresql

ぃ、小莉子 提交于 2019-12-23 10:55:01
问题 I'm migrating some SQL Server 2008R2 queries to Postgresql 9.0 and I have some trouble with it. Here's the SQL Server query: stuff((select ', '+p.[NAME] as 'data()' from BPROVIDERS_PROVIDER p, BORDER_ARTICLEORDERPROVIDER aop where p.OID = aop.PROVIDER for xml path('')),1,1,'')) as pNAMES Reading SQL Server documentation I understand that this creates a comma separated list. I think that I can change stuff function to overlay function in Postresql'. Am I correct? The second problem comes with

What's the equivalent for LISTAGG in postgres?

天大地大妈咪最大 提交于 2019-12-23 06:53:09
问题 I have to replace the Oracle driver with the newest postgres. Postgres doesn't know the function LISTAGG. I have to concat values by comma separated. What's the equivalent for the Oracle function LISTAGG in Postgres? Thanks. 回答1: The equivalent function in Postgres is string_agg() select string_agg(col,',') from my_table string_agg : input values concatenated into a string, separated by delimiter 回答2: From Postgres 9.0 or later, I believe you could do something like this: SELECT c_id, STRING

How to concatenate multiple rows order by sequence in Oracle10g

你。 提交于 2019-12-20 02:50:17
问题 If I have a data like this: GROUP | SEQUENCE | COMMAND ------------------------------ ONE | 3 | <message2>MESSAGE</message2> ONE | 1 | <?xml version="1.0" encoding="UTF-8"?> ONE | 2 | <message1>MESSAGE</message1> TWO | 2 | <message2>MESSAGE</message2> TWO | 1 | <?xml version="1.0" encoding="UTF-8"?> ........ TWO | 10 | <message9>MESSAGE</message9> How can I concatenate the command to be like this: GROUP | COMMAND ----------------- ONE | <?xml version="1.0" encoding="UTF-8"?>,<message1>MESSAGE

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

本小妞迷上赌 提交于 2019-12-17 22:29:00
问题 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

comma-separated list as a result of select statement in Oracle [duplicate]

会有一股神秘感。 提交于 2019-12-17 16:30:21
问题 This question already has answers here : How can I combine multiple rows into a comma-delimited list in Oracle? [duplicate] (11 answers) Closed 5 years ago . I have a table named "person". It contains person's id and it's parent id (only one parent is possible). As a result of a query, I want a table with first column - a person id, and a second column - a list of it's children id's. How exactly to do this? I've read about listagg function, but I'm not sure if it is appropriate for my purpose