cornerstone
cornerstone

Reputation: 659

SQL - Select Join and Group

The result of a SELECT statement by joining two tables is the following -

Col 1   |  Col 2    |  Col 3   |  Col 4
 ----------------------------------
sample11| sample12  | sample13 | sample14
sample11| sample12  | sample23 | sample24
sample11| sample12  | sample23 | sample34
sample11| sample12  | sample43 | sample44

How group and concatenate the values in col 4 for row2 and row 3 (based on the distinct values in col 3) such that the result is the following -

Col 1   |  Col 2    |  Col 3   |  Col 4
 ----------------------------------
sample11| sample12  | sample13 | sample14
sample11| sample12  | sample23 | sample24, sample34
sample11| sample12  | sample43 | sample44

Upvotes: 0

Views: 62

Answers (1)

John Woo
John Woo

Reputation: 263703

If you are using oracle 11g

SELECT  "Col 1", "Col 2", "Col 3", 
        LISTAGG("Col 4", ',') WITHIN GROUP (ORDER BY "Col 1") AS "Col 4"
FROM    TableName
GROUP   BY "Col 1", "Col 2", "Col 3"

Upvotes: 2

Related Questions