Oracle SQL AND condition for DATE - sql

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'

Related

SQL sort by each hour with different dates

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

Extrapolate table with data

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;

SQL Server Timecard Query

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.

Total time calculation in a sql query for a day where time in 24 hour format as hhmm

I have a table with date(date), left time(varchar2(4)) and arrival time(varchar2(4)). Time taken is in 24 hour format as hhmm. If a person travel 3 times a day, what will be the query to calculate total travel time in a day?
I am using oracle 11g. Kindly help. Thank you.
Convert the value to a number and report in minutes:
select to_number(substring(time, 1, 2))*60 + to_number(substring(time, 3, 2)) as minutes
Your query would look something like:
select person, sum(to_number(substring(time, 1, 2))*60 + to_number(substring(time, 3, 2))) as minutes
from t
group by person;
I see no reason to convert this back to a string -- or to even store the value as a string instead of as a number. But if you need to, you can reverse the process to get a string.
There are 2 answers, If you want to sum time only on date then it can be done as:-
select curr_date,
sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference
from sql_prac group by curr_date,arrival_time,left_time;
The sample output is as follows:-
select curr_date,left_time,arrival_time from sql_prac;
CURR_DATE LEFT_TIME ARRIVAL_TIME
--------- -------------------- --------------------
30-JUN-17 00:00:00 15:00:00
30-JUL-17 03:30:00 11:30:00
30-AUG-17 03:00:00 12:30:00
30-SEP-17 04:00:00 17:00:00
30-JUN-17 00:00:00 15:00:00
30-JUL-17 03:30:00 11:30:00
30-AUG-17 03:00:00 12:30:00
30-SEP-17 04:00:00 17:00:00
30-SEP-17 04:00:00 17:00:00
9 rows selected
select curr_date,sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference
from sql_prac group by curr_date,arrival_time,left_time;
CURR_DATE DIFFERENCE
--------- ----------
30-JUN-17 30
30-JUL-17 16
30-SEP-17 39
30-AUG-17 19
If you want to sum it by person and date then it can be done as:-
select dept,curr_date,sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference
from sql_prac group by dept,curr_date,arrival_time,left_time order by Dept;
The sample output is as follows:-
Data in table is:-
select dept,curr_date,left_time,arrival_time from sql_prac;
DEPT CURR_DATE LEFT_TIME ARRIVAL_TIME
-------------------- --------- -------------------- --------------------
A 30-SEP-17 04:00:00 17:00:00
B 30-SEP-17 04:00:00 17:00:00
C 30-AUG-17 03:00:00 12:30:00
D 30-DEC-17 04:00:00 17:00:00
A 30-SEP-17 04:00:00 17:00:00
B 30-JUL-17 03:30:00 11:30:00
C 30-AUG-17 03:00:00 12:30:00
D 30-SEP-17 04:00:00 17:00:00
R 30-SEP-17 04:00:00 17:00:00
Data fetched using the query
select dept,curr_date,sum(24 * (to_date(arrival_time, 'HH24:mi:ss')- to_date(left_time, 'HH24:mi:ss'))) as difference
from sql_prac group by dept,curr_date,arrival_time,left_time order by Dept;
DEPT CURR_DATE DIFFERENCE
-------------------- --------- ----------
A 30-SEP-17 26
B 30-JUL-17 8
B 30-SEP-17 13
C 30-AUG-17 19
D 30-SEP-17 13
D 30-DEC-17 13
R 30-SEP-17 13

how to get no of transactions on an hourly range

Table has columns Receiptno: and [TransDate] and Transtime. eg: Data below
0080052594 2012-10-28 1899-12-30 19:01:38.000
0080052595 2012-10-28 1899-12-30 19:05:09.000
0080052596 2012-10-28 1899-12-30 19:05:15.000
I need query to get hourly interval and the no: of transactions in the below format
Hour Inetrval No: Trans
09:01-10:00 10
10:01-11:00 16
Which DBMS are you using? What specific data type are your fields?
In Access, you'd use a Totals query (GROUP BY in SQL), you could do it using an additional field (or layered queries). You could also use inline code string comparisons. This would work with string or DateTime fields for transaction date and time.
For example, TABLE1:
RECEIPTNO TRANSDATE TRANSTIME
1 1/12/2015 4:32:00 PM
2 1/12/2015 4:45:00 PM
3 1/12/2015 4:52:00 PM
4 1/12/2015 3:57:00 PM
5 1/12/2015 4:07:00 PM
6 1/12/2015 4:09:00 PM
7 1/12/2015 6:15:00 PM
8 1/12/2015 12:34:00 PM
9 1/12/2015 2:45:00 PM
10 1/12/2015 3:15:00 PM
11 1/12/2015 3:17:00 PM
12 1/12/2015 3:49:00 PM
13 1/12/2015 3:47:00 PM
14 1/12/2015 2:52:00 PM
15 1/12/2015 2:36:00 PM
16 1/12/2015 2:17:00 PM
17 1/12/2015 2:25:00 PM
18 1/12/2015 4:12:00 PM
QUERY1 to count by hour as string:
SELECT Count(TABLE1.RECEIPTNO) AS CountOfRECEIPTNO, TABLE1.TRANSDATE, Left([TRANSTIME],InStr([TRANSTIME],":")-1) AS [HOUR]
FROM TABLE1
GROUP BY TABLE1.TRANSDATE, Left([TRANSTIME],InStr([TRANSTIME],":")-1)
ORDER BY Left([TRANSTIME],InStr([TRANSTIME],":")-1);
Results:
CountOfRECEIPTNO TRANSDATE HOUR
1 1/12/2015 12
5 1/12/2015 2
5 1/12/2015 3
6 1/12/2015 4
1 1/12/2015 6