This is a Question related to my original question
Data in the tables are saved like this now
I have two tables Triples and Tags
Triples Table has the following Columns
id PostID TagID Value
1 1 1 Murder
2 1 1 Theft
3 2 2 Knife
4 2 2 Gun
Tags Table has the following Columns
id TagName
1 Incident
2 Weapon
I am trying to write sql to create a Pivot Table with Dynamic Headers
Output should be like this
PostID Incident Weapon
1 Murder,Theft
2 Knifie,Gun
I written a inefficient and partial sql query for this, Any help in writing this sql appreciated
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN TagName = ''',
TagName,
''' THEN p.value END) AS `',
TagName, '`'
)
) INTO @sql
FROM tags;
SET @sql
= CONCAT('CREATE VIEW PostTags as SELECT p.postid, p.id, ', @sql, '
from triples p
left join tags t
on p.tagid = t.id
group by p.postid, p.id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;