Tuesday 15 September 2015

sql - Pivot Table Using MySQL and Group Conact -


This is a question related to my basic question

The table in the data now Are saved like this

I have two tables: triples and tags

  The following column IDs in the triple table are PostID tag ID value 1 1 1 Murder 2 1 1 theft 3 2 2 knives 4 2 2 guns   

The tag table contains the following columns

  id tag name 1 event2 weapon   

I write SQL to create a pivot table with dynamic headers

The output should be like this

  PostID Hydration Weapons 1 Murder, Stolen 2 Nuffs, Guns   

I wrote an incompetent and partial SQL query for this, appreciating any help in writing to this SQL

  SET @sql = NULL; SELECT GROUP_CONCAT ('max' (case when tagname = '' ', tagname,' 'THEN P. value, AN' ', tag name, ``')) tag @ sql; SET @sql = CONCAT (View PostTag View as 'PPOstead, PID,', SSLL, 'P.Pidid, P.ID.' on 'Petigid = T.ID Group' from Triple P); Prepare SSML from SSMT; EXTUTE STMT ; DEALLOCATE STMT;    

You have to create two strings, internal rotation to create a column For bah event and weapon for the question, and with the second string GROUP_CONCAT :

  SET @ Sql = NULL; SET @ sql2 = null; Select GROUP_CONCAT (``, tagname, '`') as opposed to CONCAT ('group_concat (', TagName, 'separator', '')) @ sql2 tags; SELECT GROUP_CONCAT ( Separate concrete ('max' (case when tagname = '' ', tagname,' 'THEN P. value as AN' ', tag name, ``')) tags @ sql; SET @sql = CONCAT 'Postid select', 'sql2,' from (SELECT p.postid, p.id, ', @sql,' triples p to p.tagid = p.postid, p.id) Post group 'posted'); Prepare SSML with SSMT; EXTUTE STMT; DEALLOCATE STMT;   

View

This is making the SQL code:

  select postId, group_concat (event separator ',') Event , Group_concat (weapon separator ',') from weapon (SELECT p.postid, p.id, max (case when tag name = 'accident' then P. value AUD) as 'event', maximum (case when tagname = 'Weapon' then P.value END) as Triple P 'weapon' on the p.tagid = t.id group p.postid, p.id added the di group with the post tag;  < / Pre> 

View

No comments:

Post a Comment