Mysql Join / Union issue

I am trying to create a report that pulls data from 2 tables: a & b. The report is grouped by a.clock. Most of the data for the report comes from a - that part is working fine. a.clock links with b.userID.

The part i am struggling with is for one of the columns the data comes from b. I need to total up the following for each a.clock grouping in the main report (this query works standalone)

 SELECT (
   SUM(
     TIME_TO_SEC(
       TIMEDIFF(
         CONCAT(b.endDate, ' ', b.outTime),
         CONCAT(b.startDate, ' ', b.inTime)
       )
     ) / 3600
   )
 ) AS 'Misc Hours' FROM b

In other words, i need to total the Misc Hours (in b) for each a.clock. I thought maybe joining the b table was necessary but that didn't seem to work. Any suggestions?

Here are the table definitions (sorry, verbose)

CREATE TABLE `a` (
  `laborID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '0=sched; 1=accepted; 2=complete; 3=authorize',
  `laborType` varchar(15) NOT NULL DEFAULT '0' COMMENT 'Lookup',
  `hours` varchar(15) NOT NULL DEFAULT '',
  `wage` varchar(15) NOT NULL DEFAULT '',
  `id` int(10) unsigned NOT NULL DEFAULT '0',
  `laborDate` date NOT NULL,
  `ot` varchar(15) NOT NULL,
  `clock` varchar(15) NOT NULL,
  `setup` varchar(15) NOT NULL DEFAULT '',
  `ot2` varchar(15) NOT NULL,
  `wageOT1` varchar(15) NOT NULL,
  `wageOT2` varchar(15) NOT NULL,
  `inTime` varchar(15) NOT NULL,
  `outTime` varchar(15) NOT NULL,
  `startDateString` varchar(125) NOT NULL,
  `endDateString` varchar(125) NOT NULL,
  `authRequestDate` datetime NOT NULL,
  `authRequestUser` int(10) unsigned NOT NULL,
  `authRequestReason` text NOT NULL,
  `authDate` datetime NOT NULL,
  `authUser` int(10) unsigned NOT NULL,
  `authRequired` varchar(45) NOT NULL,
  `km` varchar(45) NOT NULL,
  `travelTime` varchar(45) NOT NULL,
  `actualInTime` varchar(45) NOT NULL,
  `actualOutTime` varchar(45) NOT NULL,
  `actualKm` varchar(45) NOT NULL,
  `actualTravelTime` varchar(45) NOT NULL,
  `intNotes` text,
  `extNotes` text,
  `billableReason` text,
  `billableHours` varchar(45) DEFAULT NULL,
  `actualHours` varchar(45) DEFAULT NULL,
  `overtime` varchar(45) DEFAULT NULL,
  `followUpReason` text NOT NULL,
  `responseType` varchar(45) DEFAULT NULL,
  `followUpType` int(10) unsigned DEFAULT NULL,
  `billableDrop` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`laborID`),
  KEY `id` (`id`),
  KEY `type` (`type`),
  KEY `laborDate` (`laborDate`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;


CREATE TABLE `b` (
  `timecodeID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `userID` int(10) unsigned NOT NULL,
  `inTime` varchar(45) NOT NULL,
  `outTime` varchar(45) NOT NULL,
  `startDateString` varchar(145) NOT NULL,
  `endDateString` varchar(145) NOT NULL,
  `startDate` varchar(45) NOT NULL,
  `endDate` varchar(45) NOT NULL,
  `type` varchar(45) NOT NULL,
  `comments` text NOT NULL,
  `multiDay` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`timecodeID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Answers


Well if you have multiple rows in each of the table then you will get larger sum as each row in labor will join with each row in timecodes. So the idea is to have nested query group by clock and userid to get one row each as per the groupping

SELECT A.Clock, A.hours, B.'Misc Hours'
  FROM
     (
        SELECT labor.clock,
               SUM(hours) Hours
          FROM labor
         GROUP BY labor.clock
     ) A
 INNER JOIN 
     (
        SELECT  timecodes.userID,
                (SUM(
                     TIME_TO_SEC(
                          TIMEDIFF(
                               CONCAT(timecodes.endDate,' ',timecodes.outTime),
                               CONCAT(timecodes.startDate,' ',timecodes.inTime)
                          )
                      )/3600
                  )
                 ) AS 'Misc Hours'
         FROM timecodes
        GROUP BY timecodes.userID

) AS B ON A.Clock = B.UserId

SELECT timecodes.userID,
       (SUM(
           TIME_TO_SEC(
                      TIMEDIFF(
                              CONCAT(timecodes.endDate,' ',timecodes.outTime),
                              CONCAT(timecodes.startDate,' ',timecodes.inTime)
                              )
                      )/3600
           )
       )
      AS 'Misc Hours'
FROM timecodes WHERE timecodes.userID=labor.clock GROUP BY labor.clock

Need Your Help

Apparent memory leak with numpy tolist() in long running process

python python-2.7 numpy

I run a server which acts as a data processing node for clients within the team. Recently we've been refactoring legacy code within the server to leverage numpy for some of the filtering/transform...

Segmentation fault (core dumped) in c linux

c segmentation-fault coredump

My code returns a Segmentation fault and I do not know why.