prestodbeaver

Grouping column and the result will be displayed horizontally


I'm trying to work out how to get the result horizontally using group by and family column values should be displayed horizontally.

I want to find the itemids that used familys.

CREATE TABLE Table1
([ITEMID] int, Family (15))

;

INSERT INTO Table1
([ITEMID], [Family])
VALUES
('1234', 'F1'),
('1234', 'F2'),
('1234', 'F3')
('12345', 'F1'),
('12345', 'F2'),
('12345', 'F4')
('123457', 'F1'),
('123457', 'F2')
('123457', 'F3')
('223457', 'F1')
('223457', 'F2')
('423458', 'F8')

;
Expected result
ITEMID--FAMILY
1234----F1,F2,F3
12345---F1,F2,F4
123457--F1,F2,F3
223457--F1,F2,
423458--F8

Solution

  • Hi you could use the GROUP_CONCAT.

    SELECT ITEMID, GROUP_CONCAT(Family ORDER BY Family) AS FAMILY
    FROM Table1
    GROUP BY ITEMID;