我希望Client(URL, location='https://sws-crt.cert.sabre.com/')
endtime
和starttime
被group
提取并提升后加班加总。
这是我的代码:
$sql = "SELECT o.*, u.* ,p.* ,
SEC_TO_TIME(SUM(TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(dateto, '%m/%d/%y %H:%i'), '%Y-%m-%d %H:%i%s')) - TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i'), '%Y-%m-%d %H:%i%s')))) AS totalHrs,
SEC_TO_TIME(SUM(TIME_TO_SEC(totalHrs))) AS grandTotal // Can't make this run
FROM overtime AS o, users AS u, position AS p
WHERE o.user_id=u.user_id and u.p_id=p.p_id
GROUP BY p.p_id
ORDER BY p.position_name ASC";
这是我的数据库:
|-----------|-------------|-------------|-----------------------|---------------------|
|-- ot_id --|-- user_id --|--- p_id ----|------ datefrom -------|------- dateto ------|
|-----------|-------------|-------------|-----------------------|---------------------|
| 1 | 3 | 17 | 12/17/15 17:00:00 | 12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
| 2 | 3 | 17 | 12/17/15 17:00:00 | 12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
| 3 | 10 | 27 | 12/17/15 17:00:00 | 12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
| 4 | 45 | 27 | 12/17/15 17:00:00 | 12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
| 5 | 44 | 5 | 12/17/15 17:00:00 | 12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
这是输出:
p_id #17 ----> Junior Programmer
#27 ----> SAP Programmer
#5 -----> Technician
|----------------------|--------------|
|------ position ------|-- Total Hrs--|
|----------------------|--------------|
| Junior Programmer | 4:00:00 |
|----------------------|--------------|
| SAP Programmer | 4:00:00 |
|----------------------|--------------|
| Technician | 2:00:00 |
|----------------------|--------------|
| GRAND TOTAL | 10:00:00 | <-------- CAN'T GET THIS
|----------------------|--------------|
任何帮助都可以。
提前致谢:)
答案 0 :(得分:1)
使用WITH ROLLUP函数获取任何列的累积总和。
试试这个:
SELECT IFNULL(p.position_name, 'Grand Total') AS `position`
SEC_TO_TIME(SUM(TIME_TO_SEC(STR_TO_DATE(dateto, '%m/%d/%y %H:%i')) - TIME_TO_SEC(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i')))) AS totalHrs
FROM overtime AS o, users AS u, POSITION AS p
WHERE o.user_id=u.user_id AND u.p_id=p.p_id
GROUP BY p.p_id WITH ROLLUP
ORDER BY p.position_name ASC;
或强>
您还可以使用TIMESTAMPDIFF()函数生成两个日期的差异。
SELECT IFNULL(p.position, 'Grand Total') AS `position`
SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND,STR_TO_DATE(datefrom, '%m/%d/%y %H:%i'),STR_TO_DATE(dateto, '%m/%d/%y %H:%i')))) AS totalHrs
FROM overtime AS o, users AS u, POSITION AS p
WHERE o.user_id=u.user_id AND u.p_id=p.p_id
GROUP BY p.p_id WITH ROLLUP
ORDER BY p.position_name ASC
或强>
SELECT o.*, u.* ,p.* ,
SEC_TO_TIME(SUM(TIME_TO_SEC(STR_TO_DATE(dateto, '%m/%d/%y %H:%i')) - TIME_TO_SEC(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i')))) AS totalHrs
FROM overtime AS o, users AS u, POSITION AS p
WHERE o.user_id=u.user_id AND u.p_id=p.p_id
GROUP BY p.p_id WITH ROLLUP
ORDER BY p.position_name ASC
答案 1 :(得分:0)
哦!我的缺点是,我在代码中发现了错误。
%H:%i:%s
是获得差异和时间总和所必需的。
另外,我忘记在:
中加入%H:%i%s
。
添加此行将获得总计:
(select SEC_TO_TIME(SUM(TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(dateto, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')) - TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')))) FROM overtime WHERE approve_by=0) AS grandTotal
最终守则将是:
SELECT o.*, u.* ,p.* ,
SEC_TO_TIME(SUM(TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(dateto, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')) - TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')))) AS totalHrs,
(select SEC_TO_TIME(SUM(TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(dateto, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')) - TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')))) FROM overtime) AS grandTotal
FROM overtime AS o, users AS u, position AS p
WHERE o.user_id=u.user_id and u.p_id=p.p_id and o.approve_by=0
GROUP BY p.p_id
ORDER BY p.position_name ASC