There is a MYSQL table, which has the following structure:
CREATE TABLE `data` ( `ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `objectID` INT(10) UNSIGNED NOT NULL, `actionID` INT(10) UNSIGNED NOT NULL, `year` DATE NOT NULL, PRIMARY KEY (`ID`), UNIQUE INDEX `ID` (`ID`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB; With the following content:
ID objectID actionID year 1 1 1 2013 2 2 3 2013 3 1 7 2014 4 2 3 2014 5 1 4 2015 6 2 2 2015 I make the following request:
SELECT objectID, CASE year WHEN "2010" THEN actionID ELSE 0 END as "2013", CASE year WHEN "2011" THEN actionID ELSE 0 END as "2014", CASE year WHEN "2012" THEN actionID ELSE 0 END as "2015" FROM data And I get the following response (several different actionID for the objectID in one year):
objectID 2013 2014 2015 1 1 1 7 1 8 1 2 2 3 2 3 2 2 The question is whether I can and how, with some modification of the request, get an answer of the form:
objectID 2013 2014 2015 1 1 7,8 2 2 3 3 2 Or:
objectID 2013 2014 2014 2015 1 1 7 8 2 2 3 3 2 When using GROUP BY objectID displays only one of the rows per row:
objectID 2013 2014 2015 1 1 2 3 The solution that suited me, based on Mike's advice:
SELECT objectID, group_concat( DISTINCT CASE years WHEN "2013" THEN km_typeID END SEPARATOR '; ' ) as "2013", group_concat( DISTINCT CASE years WHEN "2014" THEN km_typeID END SEPARATOR '; ' ) as "2014", group_concat( DISTINCT CASE years WHEN "2015" THEN km_typeID END SEPARATOR '; ' ) as "2015", FROM data GROUP BY objectID;
SUM(CASE year WHEN "2013" THEN actionID ELSE 0 END) as 2013group by which you are writing of course you need the same - MikeSUM()does not work as it should, if there are several actions for one object in one year. They, logically, are simply summarized. Although, for the sake of justice, I did not mention this, now I will correct it - nsmindsum()usegroup_concat()- Mike