Query:
SELECT avto.Marka,SUM(OtrabotanoMachine_hours) as 'work hours',SUM(sec.Hours) as Downtime,CONVERT(group_concat(concat(prostoy.Prichina,'=',sec.Hours) separator ';') USING 'utf8') as 'downtime reasons',CONVERT(group_concat(concat(VipolnennayaRabota,'=',V_raboty) separator ';') USING 'utf8') as 'performed works'
FROM
jos_addRabotyAndProstoyMain main
INNER JOIN avto ON main.Marka=avto.ID_Avto
INNER JOIN (
SELECT id_fk, SUM(Hours) AS Hours, Prostoy
FROM jos_addRabotyAndProstoySecond
GROUP BY id_fk,Prostoy) sec ON sec.id_fk = main.id
INNER JOIN prostoy ON sec.Prostoy = prostoy.ID_Prosyoy
GROUP BY avto.Marka
Wrong result:
| Marka | work hours |downtime| downtime reasons | performed works|
|JCB-90QX | 44 | 7 |lack of materials=2; | work=51; |
| | | |machine malfunction=1; | work=51; |
| | | |Others=3; | work=51; |
| | | |unscheduled repairs=1 | work=51; |
The columns "work hours" and "performed works" contain duplicates. How eliminate duplicates?
I need result like this:
| Marka | work hours |downtime| downtime reasons | performed works|
|JCB-90QX | 11 | 7 |lack of materials=2; | work=51; |
| | | |machine malfunction=1; | |
| | | |Others=3; | |
| | | |unscheduled repairs=1 | |
Update SQL Fiddle
I think the solution is to add a DISTINCT
clause to the columns where you want to use unique data.
Try this (taken from your SQL Fiddle):
SELECT avto.Marka,SUM(distinct OtrabotanoMachine_hours) as 'work hours',SUM(sec.Hours) as Downtime,CONVERT(group_concat(concat(prostoy.Prichina,'=',sec.Hours) separator ';') USING 'utf8') as 'downtime reasons',CONVERT(group_concat(distinct concat(VipolnennayaRabota,'=',V_raboty) separator ';') USING 'utf8') as 'performed works'
FROM
jos_addRabotyAndProstoyMain main
INNER JOIN avto ON main.Marka=avto.ID_Avto
INNER JOIN (
SELECT id_fk, SUM(Hours) AS Hours, Prostoy
FROM jos_addRabotyAndProstoySecond
GROUP BY id_fk,Prostoy) sec ON sec.id_fk = main.id
INNER JOIN prostoy ON sec.Prostoy = prostoy.ID_Prosyoy
GROUP BY avto.Marka
However, it is important to note that I am not 100% sure how the OtrabotanoMachine_hours
is set up. If it is possible for there to be two legitimate separate entries with the same values, it will de-dupe them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.