I form a summary report on the hours of work and downtime of construction machines at the facilities. The user enters in the form on the site for one machine name information about how much the machine worked and stood on a particular day, indicating the causes of downtime. Information from the form is entered in the jos_addRabotyAndProstoyMain , HoursRaboty (so as not to pile everything up in one table), and the causes of downtime and hours of downtime from the same form are in the jos_addRabotyAndProstoySecond table due to the fact that one machine may have several causes of downtime.
The problem is that in the jos_addRabotyAndProstoySecond table in the Prostoy and id_fk foreign keys there are duplicate values and the INNER JOIN does not work correctly (duplicating of the rows that could be eliminated in the initial SELECT by the DISTINCT operator) occurs. In the final report (link to sql fiddle below) an error in the amount of hours in the columns of Причины простоев and Причины простоев . Instead of 12 hours for KAMAZ, it displays 10, and the reasons for the downtime are also given a total of 10 (by the way, grouping cannot be done correctly for reasons due to two foreign keys, help is also needed). For some reason, a row in the jos_addRabotyAndProstoySecond table is jos_addRabotyAndProstoySecond under id = 70
Request:
SELECT avto.Marka AS Марка, SUM(DISTINCT HR.otrab) as 'Часы работы ',SUM(sec.Hours1) as Простои,CONVERT(group_concat(DISTINCT concat(prostoy.Prichina,'=',sec.Hours1) separator ';') USING 'utf8') as 'Причины простоев',CONVERT(group_concat(DISTINCT concat(HR.VipolnennayaRabota,'=',HR.v_rab) separator ';') USING 'utf8') as 'Выполненные работы' FROM jos_addRabotyAndProstoyMain main INNER JOIN avto ON main.Marka=avto.ID_Avto INNER JOIN ( SELECT VipolnennayaRabota, SUM(V_raboty) as v_rab,SUM(OtrabotanoMachine_hours) as otrab,id_main FROM HoursRaboty GROUP BY VipolnennayaRabota) HR ON HR.id_main=main.id INNER JOIN ( SELECT Prostoy,SUM(Hours)as Hours1,id_fk FROM jos_addRabotyAndProstoySecond GROUP BY id_fk,Prostoy)sec ON main.id = sec.id_fk INNER JOIN prostoy on sec.Prostoy = prostoy.ID_Prosyoy GROUP BY avto.Marka To test Sql Fiddle
I really need help already broken head, I can not make a week.
