AI52487963
AI52487963

Reputation: 1273

MySQL pivoting column names based on column values?

I have a table of data like:

ID1   F1.   F2.   F3
X1. Enabled. Disabled. Disabled
X2. Disabled. Enabled. Enabled. 

I'd like to get it into the form of:

ID1  Fields
X1.  F1
X2.  F2,F3

But I'm having writers block on how to approach the problem. Is this something a pivot would help with? Self-joining the table? I've tried some group_concat() aggregations on if() statements to no avail, similarly with grouping on case whens.

Upvotes: 2

Views: 56

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562651

SELECT ID1, CONCAT_WS(',', NULLIF(F1,'Disabled'), NULLIF(F2,'Disabled'), NULLIF(F3,'Disabled')) AS Fields
FROM ...

See:

Upvotes: 2

Related Questions