Reputation: 21
I need a query to retrieve machinery stop codes and failure descriptions that looks like a duplicate search query but it is quite different.
Here's my source table
I'm using this query
SELECT `TestDups`.`MachineID`
, `TestDups`.`Code`
, `TestDups`.`StopTime`
FROM `TestDups`
WHERE `TestDups`.`Code` IN ( SELECT `TestDups`.`Code`
FROM `TestDups`
GROUP BY `TestDups`.`Code`
HAVING COUNT(*) > 0 )
ORDER BY `TestDups`.`MachineID`
which returns this:
but I would like it to return this:
How can I reach my goal?
Upvotes: 2
Views: 142
Reputation: 8419
Hope this query would produce your desired result. I have given link for the demo as well
select MachineID,Code,sum(stoptime) as StopTime, count(*) as Num from
TestDups group by MachineID,code order by machineid
Upvotes: 1
Reputation: 711
Something like following will work;
SELECT ... SUM(StopTime), COUNT(*) as Num FROM ... GROUP BY MACHINEID, CODE
Upvotes: 2