Solution 1 :
with tempTable as (
select
t.workerid,
sum(DATEDIFF(minute,0,t.hour)) hour,
sum(DATEDIFF(minute,0,t.type5)) type5,
sum(DATEDIFF(minute,0,t.type1)) type1,
sum(DATEDIFF(minute,0,t.type2)) type2,
sum(DATEDIFF(minute,0,t.type3)) type3
from YourTable t
/*Where Conditions*/
group by t.workerid
)
select
t.workerid,
format(DATEADD(minute,t.hour,0),'HH:mm:ss') as hour,
format(DATEADD(minute,t.type5,0),'HH:mm:ss') as type5,
format(DATEADD(minute,t.type1,0),'HH:mm:ss') as type1,
format(DATEADD(minute,t.type2,0),'HH:mm:ss') as type2,
format(DATEADD(minute,t.type3,0),'HH:mm:ss') as type3
from tempTable t
Problem :
Database of OTHOUR:
Hello, i would want to ask how to calculate total sum of hour, type5, type1, type2, type3, as show in picture for each worker id based on selected date range?
For example, i want to calculate total hour of worker from 2020-06-08 to 2020-06-21 which in the result,
workerid hour type5 type1 type2 type3
04405 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00
04408 12:00:00 02:00:00 00:00:00 08:00:00 02:00:00
04458 12:30:00 00:00:00 08:00:00 04:30:00 00:00:00
which 04408 have 2 day in date range, so it will sum the hour while 04405 didnt have any ot in date range, so it return 00:00:00
how would i do in sql or php, now i creating an android app, thank you
Updated—
<?php
include_once("dbconnect.php");
$sql ="with tempTable as (
select
t.workerid,
sum(DATEDIFF(minute,0,t.hour)) hour,
sum(DATEDIFF(minute,0,t.Type5)) Type5,
sum(DATEDIFF(minute,0,t.Type1)) Type1,
sum(DATEDIFF(minute,0,t.Type2)) Type2,
sum(DATEDIFF(minute,0,t.Type3)) Type3
from othour t
group by t.workerid
)
select
t.workerid,
format(DATEADD(minute,t.hour,0),'HH:mm:ss') as hour,
format(DATEADD(minute,t.Type5,0),'HH:mm:ss') as Type5,
format(DATEADD(minute,t.Type1,0),'HH:mm:ss') as Type1,
format(DATEADD(minute,t.Type2,0),'HH:mm:ss') as Type2,
format(DATEADD(minute,t.Type3,0),'HH:mm:ss') as Type3
from tempTable t";
$result= $conn -> query($sql);
if($result -> num_rows > 0) {
$response["ot"] = array();
while ($row = $result -> fetch_assoc()) {
$ot = array();
$ot[workerid] = $row["workerid"];
$ot[othour] = $row["hour"];
$ot[type5] = $row["Type5"];
$ot[type1] = $row["Type1"];
$ot[type2] = $row["Type2"];
$ot[type3] = $row["Type3"];
array_push($response["ot"], $ot);
}
echo json_encode($response);
} else {
echo json_encode("error");
}
$conn ->close();
?>
Comments
Comment posted by Han Yun Xi
Dataabse were screenshoted on the above picture which tag database of OTHOUR
Comment posted by Han Yun Xi
sorry for may mislead u, but i want to know calculate total hour based on selected date range, not add all of them, the result still need to match workerid, only hour,type5,type1,type2 and type3 that match to same workerid in selected range will sum together
Comment posted by Han Yun Xi
but i cannot show in json,maybe my php problem, could u help?
Comment posted by Han Yun Xi
thanks, but i can’t add to json, it show error, maybe my php problem, could u help?
Comment posted by Hesam Akbari
Hi, I honestly don’t know about PHP, but that’s for sure the query is correct So, What is the problem or error?
Comment posted by Han Yun Xi
i cannot add the data into json through php, but during key in the code in phpmyadmin, the have error on near with (error with unrecognized statement type near with)