listagg in SQL to group rows in to one row

Eswar Vignesh

i have a table 1 shown below

Name  role       F1     status1 status 2 
sam   player     yes      null   null
sam   admin      yes     null    null
sam   guest       no      x       x

i want the result to be

Name  role         status1 status 2 
  sam admin,player    x       x

i have done a query to list_agg the role in to one row.but the status is null for sam to show when F1='yes'

query i used

 select name,list_agg(role,',') within group(order by name),max(status1),max(status2)
from table 1 where F1='yes'
group by name 

but i get something like this

name role          status1   status2 
sam  admin,player     null      null

i want the where to work only on role column and the max(status1) to be in status1 i.e.'x'.please help me .thank you

Tim Biegeleisen

You can try using LISTAGG() within a GROUP BY query:

SELECT Name,
    LISTAGG(Role, ',') WITHIN GROUP (ORDER BY Role) "Role"
    MAX(CASE WHEN water_access = 'Y' THEN 'Y' ELSE NULL END) "water_access",
    MAX(CASE WHEN food_access = 'Y'  THEN 'Y' ELSE NULL END) "food_access",
    MAX(CASE WHEN power_access = 'Y' THEN 'Y' ELSE NULL END) "power_access"
FROM yourTable
GROUP BY Name
ORDER BY Name DESC

Note that I chose to order the aggregation of each Name group using the Role, because you didn't provide us with any column which could give the ordering you show in your expected output.

Second note: MAX() in Oracle ignores NULL values, so it can be used in the pivot to correctly identify the Y values you want to appear.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

SQL: Group by minimum value in one field while selecting distinct rows

From Dev

Combine SQL rows into one row

From Dev

Access SQL - Create Row For Every 10 Rows And Remainder Found In Group

From Dev

Multiple rows to one row Oracle SQL

From Dev

Grouping each three rows to one group in SQL Server

From Dev

How to combine rows into one row in SQL?

From Dev

SQL - Group rows by date ranges with contiguous values in row

From Dev

How to group rows separated by a specific row in SQL

From Dev

listagg in SQL to group rows in to one row

From Dev

how do i group rows by an id row in group_concat into one row string?

From Dev

Group several rows of data into one row by column?

From Dev

combining multiple rows into one row in sql server

From Dev

SQL Show 2 rows in one row

From Dev

sum every 5 rows in one row in sql

From Dev

Combine Multiple Rows into One Row (One Column) when Using SQL

From Dev

T-SQL: Combine rows to one row

From Dev

SQL - COUNT() rows for two tables that are GROUP BY one column in one table

From Dev

SQL aggregate rows in one row with different name

From Dev

Group multiple result rows in the same row by one column in SQL Server 2008

From Dev

MySQL group rows with same ID in one row

From Dev

SQL - Group rows by date ranges with contiguous values in row

From Dev

Selecting pairs of rows in one row SQL

From Dev

SQL concatenate one of the columns in groups of 10 with LISTAGG()

From Dev

Oracle SQL aggregate rows into column listagg with condition

From Dev

Oracle SQL index rows based on group by/ parent row

From Dev

How to return a group of rows when one row meets "where" criteria in SQL Anywhere

From Dev

Oracle SQL listagg multiple column in one column

From Dev

SQL "GROUP BY" multiple rows ignoring one

From Dev

SQL group result in one row

Related Related

  1. 1

    SQL: Group by minimum value in one field while selecting distinct rows

  2. 2

    Combine SQL rows into one row

  3. 3

    Access SQL - Create Row For Every 10 Rows And Remainder Found In Group

  4. 4

    Multiple rows to one row Oracle SQL

  5. 5

    Grouping each three rows to one group in SQL Server

  6. 6

    How to combine rows into one row in SQL?

  7. 7

    SQL - Group rows by date ranges with contiguous values in row

  8. 8

    How to group rows separated by a specific row in SQL

  9. 9

    listagg in SQL to group rows in to one row

  10. 10

    how do i group rows by an id row in group_concat into one row string?

  11. 11

    Group several rows of data into one row by column?

  12. 12

    combining multiple rows into one row in sql server

  13. 13

    SQL Show 2 rows in one row

  14. 14

    sum every 5 rows in one row in sql

  15. 15

    Combine Multiple Rows into One Row (One Column) when Using SQL

  16. 16

    T-SQL: Combine rows to one row

  17. 17

    SQL - COUNT() rows for two tables that are GROUP BY one column in one table

  18. 18

    SQL aggregate rows in one row with different name

  19. 19

    Group multiple result rows in the same row by one column in SQL Server 2008

  20. 20

    MySQL group rows with same ID in one row

  21. 21

    SQL - Group rows by date ranges with contiguous values in row

  22. 22

    Selecting pairs of rows in one row SQL

  23. 23

    SQL concatenate one of the columns in groups of 10 with LISTAGG()

  24. 24

    Oracle SQL aggregate rows into column listagg with condition

  25. 25

    Oracle SQL index rows based on group by/ parent row

  26. 26

    How to return a group of rows when one row meets "where" criteria in SQL Anywhere

  27. 27

    Oracle SQL listagg multiple column in one column

  28. 28

    SQL "GROUP BY" multiple rows ignoring one

  29. 29

    SQL group result in one row

HotTag

Archive