我正在尝试创建一个从2个表中提取数据的报告:a&湾该报告按时间分组。该报告的大多数数据来自 - 该部分工作正常。 a.clock与b.userID链接。
我正在努力的部分是数据来自b的其中一列。我需要在主报告中为每个a.clock分组总计以下内容(此查询独立运行)
SELECT (
SUM(
TIME_TO_SEC(
TIMEDIFF(
CONCAT(b.endDate, ' ', b.outTime),
CONCAT(b.startDate, ' ', b.inTime)
)
) / 3600
)
) AS 'Misc Hours' FROM b
换句话说,我需要为每个时间点总计杂项时数(b)。我想可能加入b表是必要的,但似乎没有用。有什么建议吗?
以下是表格定义(抱歉,详细)
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;
答案 0 :(得分:0)
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
答案 1 :(得分:0)
如果你在每个表中有多行,那么你将获得更大的总和,因为人工中的每一行都将与时间码中的每一行连接。因此,我们的想法是按时间和嵌入式查询分组,按照灌浆
分别获取一行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