Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] php – Calculate total hour of worker based on selected date range

Posted on November 14, 2022

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

result

Problem :

Database of OTHOUR:

enter image description here

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

READ  [ANSWERED] java - How to store the data in the list without overwritten the data?
Powered by Inline Related Posts

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)

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress