I have a timecard table like below. The HourType field represents whether the Employee entered the time ("0"), or whether their supervisor overrides or enters time ("1"). If a supervisor overrides time there is both a 0 record and a 1 record (4/8/2019). If a supervisor enters time the employee did not, then there is only a 1 record (4/12/2019). I need a query to find the total time for a given week. So in the below example the total hours for this week should be 39.5 hours.
RecordId EmployeeId StartDate EndDate StartTime EndTime HourCount HourType
1 100 4/8/2019 4/8/2019 9:00:00 AM 5:00:00 PM 8 0
2 100 4/8/2019 4/8/2019 9:00:00 AM 4:30:00 PM 7.5 1
3 100 4/9/2019 4/9/2019 9:00:00 AM 5:00:00 PM 8 0
4 100 4/10/2019 4/10/2019 9:00:00 AM 5:00:00 PM 8 0
5 100 4/11/2019 4/11/2019 9:00:00 AM 5:00:00 PM 8 0
6 100 4/12/2019 4/12/2019 9:00:00 AM 5:00:00 PM 8 1
Added 2nd data set that would include multiple records per day to account for lunch hours (4/11/2019). Total Hours for this dataset should be 37.5.
RecordId EmployeeId StartDate EndDate StartTime EndTime HourCount HourType
1 100 4/8/2019 4/8/2019 9:00:00 AM 5:00:00 PM 8 0
2 100 4/8/2019 4/8/2019 9:00:00 AM 4:30:00 PM 7.5 1
3 100 4/9/2019 4/9/2019 9:00:00 AM 5:00:00 PM 8 0
4 100 4/10/2019 4/10/2019 9:00:00 AM 5:00:00 PM 8 0
5 100 4/11/2019 4/11/2019 9:00:00 AM 12:00:00 PM 3 0
6 100 4/11/2019 4/11/2019 9:30:00 AM 12:00:00 PM 2.5 1
7 100 4/11/2019 4/11/2019 1:00:00 PM 5:00:00 PM 4 0
8 100 4/12/2019 4/12/2019 9:00:00 AM 5:00:00 PM 8 1
You seem to want:
select employeeId, sum(hourcount)
from (select t.*,
row_number() over (partition by employeeid, startdate, enddate order by hourtype desc) as seqnum
from t
) t
where seqnum = 1;
That is, if multiple records match emplyeeid, startdate, and enddate, then choose the override one.
Related
I have a sql table like this
occ_val time
0 2/1/2022 3:35:08 pm
1 2/1/2022 3:59:08 pm
2 2/1/2022 4:55:08 pm
3 2/1/2022 5:32:08 pm
2 3/1/2022 4:43:08 pm
3 4/1/2022 2:15:08 pm
How to I sort by each hour to get this:
time occ_val
2:00:00 pm 3
3:00:00 pm 1
4:00:00 pm 4
5:00:00 pm 3
Your help is very much appreciated :D
Here is a MySQL solution:
select DATE_FORMAT(min(time), '%h:00'),sum(occ_val) from tbl group by hour(time)
See https://www.db-fiddle.com/f/czudbp5zug6HxX4TFV26GT/0
SELECT * from YourTableName
ORDER BY (timeCol - FLOOR(CAST(timeCol AS float)) ASC
I have a table that is currently in the following format
ID
Title
CreatedOn
1
Test 1
2021-04-26 08:00:00
2
Test 2
2021-04-26 10:00:00
3
Test 3
2021-04-27 09:00:00
4
Test 4
2021-04-28 14:00:00
5
Test 5
2021-04-28 16:00:00
6
Test 6
2021-04-28 12:00:00
7
Test 7
2021-04-29 13:00:00
8
Test 8
2021-04-30 06:00:00
9
Test 9
2021-05-17 10:00:00
10
Test 10
2021-05-18 19:00:00
11
Test 11
2021-05-18 23:00:00
12
Test 12
2021-05-19 16:00:00
13
Test 13
2021-05-20 07:00:00
14
Test 14
2021-05-21 14:00:00
15
Test 15
2021-05-21 10:00:00
16
Test 16
2021-04-30 10:00:00
What I would like to do is a query that would tell me how many requests have been Monday to Friday per hour. So aggregate all the data into just rows of Monday to Friday.
So the query should return
Day
Hour
Count
Monday
08:00
1
Monday
10:00
2
Tuesday
10:00
1
Tuesday
19:00
1
Tuesday
23:00
1
Wednesday
14:00
1
Wednesday
16:00
2
Wednesday
12:00
1
etc.. How do I achieve this?
So far I have the following
SELECT
DATENAME(WEEK, CreatedOn) AS Week,
DATEPART(Hour, CreatedOn) AS Hour,
COUNT(*) AS Requests
FROM [Enterprise32].[dbo].[nav_EmailEstimateRequests]
where CreatedOn > '2021-01-01'
GROUP BY DATENAME(WK, CreatedOn),DATEPART(Hour, CreatedOn)
ORDER BY DATENAME(WK, CreatedOn);
But the above query returns each week so Week 1 up until Week 21. Please guide me in the right direction.
Thank you!
You want weekday for the date part:
SELECT DATENAME(WEEKDAY, CreatedOn) AS Weekday,
DATEPART(Hour, CreatedOn) AS Hour,
COUNT(*) AS Requests
FROM [Enterprise32].[dbo].[nav_EmailEstimateRequests]
WHERE CreatedOn > '2021-01-01'
GROUP BY DATENAME(WEEKDAY, CreatedOn), DATEPART(Hour, CreatedOn), DATEPART(WEEKDAY, CreatedOn)
ORDER BY DATEPART(WEEKDAY, CreatedOn), Hour;
Note: I included DATEPART(weekday, ) in the GROUP BY, so you could use it in the ORDER BY.
I would like to achieve the following:
In my SQL Server database I currently have this table (4 rows to show the records).
I would like to extrapolate the M_ID column, which currently only has 4 distinct values to distinct 200 values.
The values in the other columns can be the same or be a random value/data
What is the best way to approach this?
T1
P_M L_U U_D_T_P M_ID
4/9/2020 9:00:00 PM 4/9/2020 9:00:00 PM 2 105
5/9/2020 9:00:00 PM 4/9/2020 9:00:00 PM 2 111
7/9/2020 9:00:00 PM 4/9/2020 9:00:00 PM 2 112
5/9/2020 9:00:00 PM 4/9/2020 9:00:00 PM 2 113
You can use generate a random value, using:
rand(checksum(newid()))
This can then be incorporated in your logic. In an update:
update t
set m_id = floor(rand(checksum(newid())) * 200) + 1;
How to query in oracle SQL for the AND condition from the below table.
I want to query like where Start_date =something AND End_date=something
ID Start_date End_date STATUS USER
21 4/16/2010 11:00:00 PM 4/16/2010 11:59:00 PM PENDING TOM
22 4/18/2010 11:00:00 AM 4/18/2010 11:59:00 PM COMPLETED JACK
23 4/20/2010 11:00:00 PM 4/20/2010 11:59:00 PM PENDING JEFF
24 5/16/2010 11:00:00 PM 5/16/2010 11:59:00 PM STARTED MIKE
where start_date = '4/16/2010 11:00:00 PM' and end_date = '4/16/2010 11:59:00 PM'
I have a table which looks like you can see below:
Id Date ScheduledTimeFrom ScheduledTimeTo ActualTimeFrom ActualTimeTo
1 2013-01-01 1899-12-30 07:00:00 1899-12-30 18:00:00 1899-12-30 07:23:00 1899-12-30 17:15:00
I need to calculate per half hour how many records exists, the output should be like:
Time Actual Count:
7:00 4
7:30 4
8:00 4
8:30 4
9:00 4
9:30 5
10:00 5
10:30 6
11:00 7
11:30 8
12:00 8
12:30 8
13:00 8
13:30 8
14:00 8
14:30 8
15:00 7
15:30 7
16:00 7
16:30 6
17:00 5
17:30 4
18:00 4
I already tried to make a helper table which should hold the times per halfhour. I have joined this helpertable with the table that contains the data and after that I tried to use a group by function but it was not working.
My query was like:
Create table period (timefrom datetime, timeto datetime)
insert into period
select '1899-12-30 07:00:00.000', '1899-12-30 07:30:00.000'
Union all
select '1899-12-30 07:30:00.000', '1899-12-30 08:00:00.000'
select *
from period p left join table1 t on t.ActualTimeFrom < p.timeto and t.ActualTimeTo >=p.timefrom
Grouping this give me no desired result....
Anyone an idea how to come to the result?
P.s. I am using sql server 2005.
After snooping around and testing it on my side, looks like this date function could be the answer:
DATEADD(mi,DATEDIFF(mi,0,YOUR_DATE_COLUMN)/30*30,0)