Concatenate field from multiple rows into single field

mf.cummings

Barriers: I can't CREATE anything. No GTT's, PROCs, etc. I'm querying an Exadata data lake and whatever script I create will be executed from Toad or SQL Developer.

I have a SELECT query that returns 3 rows:

  • Date
  • IDKey
  • EVENT

for each Date, IDKey, there are likely a dozen or so EVENT's. Example:

 DATE       IDKEY EVENT    
10/17/2016  300328  EVENT1    
10/17/2016  300328  EVENT3    
10/17/2016  300328  EVENT4    
10/17/2016  300440  EVENT1    
10/17/2016  300440  EVENT2    
10/17/2016  300440  EVENT5 

On a given DATE, an IDKey can have multiple EVENTS, each generating a new row.

Goal: to create a single row that groups DATE and IDKEY, concatenating all unique EVENTS into a single field;

DATE  IDKEY  EVENT    
10/17/2016 300328 'EVENT1 | EVENT3 | EVENT4'    
10/17/2016 300440 'EVENT1 | EVENT2 | EVENT5'
William Robertson

Looks like a group by with listagg:

with demo (dt, id, event) as
   ( select date '2016-10-17', 300328, 'EVENT1' from dual union all
     select date '2016-10-17', 300328, 'EVENT3' from dual union all
     select date '2016-10-17', 300328, 'EVENT4' from dual union all
     select date '2016-10-17', 300440, 'EVENT1' from dual union all
     select date '2016-10-17', 300440, 'EVENT2' from dual union all
     select date '2016-10-17', 300440, 'EVENT5' from dual 
   )
select dt, id
     , listagg(event, '|') within group (order by event) as events
from   demo
group by dt, id;

DT                  ID EVENTS
----------- ---------- --------------------------------
17/10/2016      300328 EVENT1|EVENT3|EVENT4
17/10/2016      300440 EVENT1|EVENT2|EVENT5

There could he problems if there are a huge number of eventnos.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Join two tables and concatenate multiple rows of a single field

From Dev

Concatenate multiple rows into one field

From Dev

SQL insert multiple rows from single field in another table

From Dev

How can I concatenate multiple values in a single field or column? (MySQL)

From Java

Can I concatenate multiple MySQL rows into one field?

From Dev

How to concatenate multiple rows into one field in sql server

From Dev

How to concatenate multiple rows into one field in sql server

From Dev

Create single field from multiple JSON fields

From Dev

Filtering on multiple values from a single field - Django

From Dev

Field involving multiple rows from another table

From Dev

Extract and concatenate the same field from multiple records in big query

From Dev

Concatenate string values in array in a single field in MongoDB

From Dev

Concatenate field data with previous and next rows

From Dev

How to take data from rows and put it into single field?

From Dev

create a view and concatenate more than one field to single field name

From Dev

How to get multiple rows from multiple nodes in a XMLData field?

From Dev

MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

From Dev

Elasticsearch multiple analyzers for a single field

From Dev

How to list multiple field matches from a single table in oracle SQL

From Dev

Inserting multiple fields from a form into single field in database

From Dev

use single method to calculate age from multiple input field

From Dev

Selecting rows using multiple LIKE conditions from a table field

From Dev

Combine 1 field from multiple rows via AmpScript

From Dev

Remove a substring in a varchar field from multiple rows of a table

From Dev

Select one row from multiple rows based on one field

From Dev

How to update multiple mysql rows from a form field

From Dev

update field value with multiple rows

From Dev

concat multiple rows in one field

From Dev

concatenate columns horizontally, if matches in previous field. Multiple columns to concatenate

Related Related

  1. 1

    Join two tables and concatenate multiple rows of a single field

  2. 2

    Concatenate multiple rows into one field

  3. 3

    SQL insert multiple rows from single field in another table

  4. 4

    How can I concatenate multiple values in a single field or column? (MySQL)

  5. 5

    Can I concatenate multiple MySQL rows into one field?

  6. 6

    How to concatenate multiple rows into one field in sql server

  7. 7

    How to concatenate multiple rows into one field in sql server

  8. 8

    Create single field from multiple JSON fields

  9. 9

    Filtering on multiple values from a single field - Django

  10. 10

    Field involving multiple rows from another table

  11. 11

    Extract and concatenate the same field from multiple records in big query

  12. 12

    Concatenate string values in array in a single field in MongoDB

  13. 13

    Concatenate field data with previous and next rows

  14. 14

    How to take data from rows and put it into single field?

  15. 15

    create a view and concatenate more than one field to single field name

  16. 16

    How to get multiple rows from multiple nodes in a XMLData field?

  17. 17

    MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

  18. 18

    Elasticsearch multiple analyzers for a single field

  19. 19

    How to list multiple field matches from a single table in oracle SQL

  20. 20

    Inserting multiple fields from a form into single field in database

  21. 21

    use single method to calculate age from multiple input field

  22. 22

    Selecting rows using multiple LIKE conditions from a table field

  23. 23

    Combine 1 field from multiple rows via AmpScript

  24. 24

    Remove a substring in a varchar field from multiple rows of a table

  25. 25

    Select one row from multiple rows based on one field

  26. 26

    How to update multiple mysql rows from a form field

  27. 27

    update field value with multiple rows

  28. 28

    concat multiple rows in one field

  29. 29

    concatenate columns horizontally, if matches in previous field. Multiple columns to concatenate

HotTag

Archive