Janthine Posted September 6, 2013 Share Posted September 6, 2013 Hello, hope someone can help me out here I have a table, Table Scores First I want to join the rows on ID when the score is the same, so that in the example below ID 2 has one row. But is it also possible to remove the score column and put the score in the different year-columns as below? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/ Share on other sites More sharing options...
vinny42 Posted September 6, 2013 Share Posted September 6, 2013 The year1, year2, year3 columns smell like an error in the design, repeating columns are usually not as flexible as you might want. Your first question could be a simple as running an aggregate:SELECT id, SUM(year1), SUM(year2), ... FROM table GROUP BY id; The second question requires more rules, you have put A and C in the same row, and all the B's in the second, why can A and C be merged, but the B in year-2 cannnot? Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448531 Share on other sites More sharing options...
Janthine Posted September 8, 2013 Author Share Posted September 8, 2013 Hello, thanks Vinny, yes by SUM I get the score counted up. The second one, I thought the Bs were merged into one row. Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448669 Share on other sites More sharing options...
Janthine Posted September 8, 2013 Author Share Posted September 8, 2013 Oh I want one row per ID Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448670 Share on other sites More sharing options...
Janthine Posted September 8, 2013 Author Share Posted September 8, 2013 I think it works by command group_concat ? Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448673 Share on other sites More sharing options...
vinny42 Posted September 8, 2013 Share Posted September 8, 2013 Your second question is not group_concat, because year1 has A and B, but your example shows separate rows for year1, and the first row contains A and C, so there is something special about B, or you haven't thought enough about what you acrually want. Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448674 Share on other sites More sharing options...
Janthine Posted September 8, 2013 Author Share Posted September 8, 2013 No it is random, A B C can be placed anywhere Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448678 Share on other sites More sharing options...
Janthine Posted September 8, 2013 Author Share Posted September 8, 2013 if ID 1 has in year_1 a score of B and C I would like to see in column year_1: B,C Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448679 Share on other sites More sharing options...
Janthine Posted September 8, 2013 Author Share Posted September 8, 2013 The first table is an example how I worked it out..so per score there is a row now ...now I want to bring it back per ID and that worked with what you explained SELECT id, SUM(year1), SUM(year2), ... FROM table GROUP BY id and when I write SUM(year1)>0 year1 it gives the scores per ID in each column. Now I like to see the different scores in one row but I guess it's won't work out Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448681 Share on other sites More sharing options...
Solution fenway Posted September 8, 2013 Solution Share Posted September 8, 2013 (edited) This is screaming for normalization, but this will work (link to http://sqlfiddle.com/#!2/30c2b/1/0): select id , IFNULL(group_concat(year1),0) as `Year 1` , IFNULL(group_concat(year2),0) as `Year 2` , IFNULL(group_concat(year3),0) as `Year 3` from ( select id , IF(year1,score,null) as year1 , IF(year2,score,null) as year2 , IF(year3,score,null) as year3 from scores ) t1 group by id; Obviously, depending on how you want duplicates and sorting, you'll need to tweak the group_concat() call slightly. Edited September 8, 2013 by fenway sqlfiddle Quote Link to comment https://forums.phpfreaks.com/topic/281937-join-rows/#findComment-1448727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.