Comparing multiple rows of data within a date range to create a new table in MS Access - sql

I'm a novice to SQL & MS-Access, however I have a table of data in MS-Access that looks like:
ID | Start_Time | End_Time
1 | 1:00:00 PM | 1:00:30 PM
2 | 2:15:10 PM | 2:15:50 PM
3 | 2:15:30 PM | 2:18:40 PM
4 | 2:17:00 PM | 2:17:30 PM
5 | 2:45:10 PM | 3:03:10 PM
Each row is sequentially recorded into the database. I want to compare the start and end times of each and combine together rows that overlap. For instance, ID 1's Start_Time and End_Time do not overlap any other times in the table, therefore, it would get posted into the new table. However, ID 2 through 4 have Start_Times and End_Times that overlap with ID 2's Start_Time as Start_Time of the group and ID 3's End_Time as the End_Time of the group ID 2 through 4.
The end result would be a new table that should look like:
ID | Start_Time | End_Time | Duration_seconds
1 | 1:00:00 PM | 1:00:30 PM | 30
2 | 2:15:10 PM | 2:18:40 PM | 210
3 | 2:45:10 PM | 3:03:10 PM | 1080
How can I do this in SQL/MS-Access?
Thank you!!

This might need a few passes through the recordsets.
Define 2 new variables. NewStart and NewEnd. As you grab each ID, assign the existing start/end times.
Using a nested loop compare each record to every other record (new times). If the start time is between the time range then replace that IDs NewStart as the other IDs start time. The NewEnd will be assigned the greater of the current ID or comparitive IDs end time.
As you cycle through, items 3 and 4 will have the same "new" times as ID 2. All will have transformed "new" times.
After this, you query distinct times for each ID

Related

Splunk: Split a time period into hourly intervals

index="dummy" url="https://www.dummy.com" status="200 OK"
| stats count by id
| where count > 10
If I apply this above query for 1 day, I would get, for example
id count
ABC 50
XYZ 60
..
This would mean ABC hit https://www.dummy.com 50 times in 1 day, and XYZ called that 60 times.
Now I want to check this for 1 day but with every two hours interval
Suppose, ABC called that request 25 times at 12:00 AM, then 25 times at 3:AM,
and XYZ called all the 60 requests between 12 AM and 2 AM
I want the output to look like this (time format doesn't matter)
id count time
XYZ 60 12:00 AM
ABC 25 12:00 AM
ABC 25 2:00 AM
..
You can use bin to group events into time buckets. You can use any span value, but for the 2 hours you mentioned, the updated query would be:
index="dummy" url="https://www.dummy.com" status="200 OK"
| bin _time span=2h
| stats count by id, _time
| where count > 10

Given 3 rows, If the difference between the endtime and startime of 2 different rows is less than 5, merge them

Given 3 rows, If the difference between the endtime and startime of 2 different rows is less than 5, I want to merge them.
So Given:
I want to get:
a | b | StartTime | EndTime
161 | 25 | 2020-12-07 00:00:00.000 | 2021-09-03 00:00:00.000
Because the difference between 1st row endtime and 2nd row starttime is < 5 so they merged and then it finally merged with the 3rd row.
How can we achieve this in SQL? Thanks :)

Review scripts which are older than 3 months and in the last 30 days

I'm try to run a query that will allow me to see where we have scripts running that are older than 3 months old over the last 30 days delivery, so we know they need to be updated.
I have been able to build the query to show me all the scripts and their last regen dates (with specific dates put in) but can't work out;
How to look at only the last 30 days data.
How to see only the scripts where the date_regen column is older than 3 months from today's date - From the last 30 days data that I'm reviewing.
EXAMPLE TABLE
visit_datetime | client | script | date_regen |
2019/10/04 03:32:51 | 1 | script1 | 2019-09-17 13:12:01 |
2019/09/27 03:32:52 | 2 | script2 | 2019-07-18 09:44:02 |
2019/10/06 03:32:50 | 3 | script3 | 2019-03-18 14:08:02 |
2019/10/02 06:28:24 | 4 | script6 | 2019-09-11 10:02:01 |
2019/03/01 06:28:24 | 5 | script7 | 2019-02-11 10:02:01 |
The below examples haven't been able to get me what I need. My idea was that I would get the current date (using now()) and then knowing that, look at all data in the last 30 days.
After that I would then WHERE month,-3 (so date_regen 3 months+ old from the current date.
However I can't get it to work. I also looked at trying to do -days but that also had no success.
-- WHERE MONTH = MONTH(now()) AND YEAR = YEAR(now())
-- WHERE date_regen <= DATEADD(MONTH,-3,GETDATE())
-- WHERE DATEDIFF(MONTH, date_regen, GetDate()) >= 3
Code I am currently using to get the table
SELECT split_part(js,'#',1) AS script,
date_regen,
client
FROM table
WHERE YEAR=2019 AND MONTH=10 AND DAY = 01 (This where is irrelevant as I would need to use now() but I don't know what replaces "YEAR/MONTH/DAY ="
GROUP BY script,date_regen,client
ORDER BY client DESC;
END GOAL
I should only see client 3 as clients 1+2+4 have tags where the date_regen is in the last 3 months, and client 5 has a visit_datetime out of the 30 limit.
visit_datetime | client | script | date_regen |
2019/10/06 03:32:50 | 3 | script3 | 2019-03-18 14:08:02 |
I think you want simple filtering:
select t.*
from t
where visit_datetime >= current_timestamp - interval 30 day and
date_regen < add_months(current_timestamp, -3)

How to go between a set of dates and times

I have a set of data where one column is date and time. I have been asked for all the data in the table, between two date ranges and within those dates, only certain time scale. For example, I was data between 01/02/2019 - 10/02/2019 and within the times 12:00 AM to 07:00 AM. (My real date ranges are over a number of months, just using these dates as an example)
I can cast the date and time into two different columns to separate them out as shown below:
select
name
,dateandtimetest
,cast(dateandtimetest as date) as JustDate
,cast(dateandtimetest as time) as JustTime
INTO #Test01
from [dbo].[TestTable]
I put this into a test table so that I could see if I could use a between function on the JustTime column, because I know I can do the between on the dates no problem. My idea was to get them done in two separate tables and perform an inner join to get the results I need
from #Test01
WHERE justtime between '00:00' and '05:00'
The above code will not give me the data I need. I have been racking my brain for this so any help would be much appreciated!
The test table I am using to try and get the correct code is shown below:
|Name | DateAndTimeTest
-----------------------------------------|
|Lauren | 2019-02-01 04:14:00 |
|Paul | 2019-02-02 08:20:00 |
|Bill | 2019-02-03 12:00:00 |
|Graham | 2019-02-05 16:15:00 |
|Amy | 2019-02-06 02:43:00 |
|Jordan | 2019-02-06 03:00:00 |
|Sid | 2019-02-07 15:45:00 |
|Wes | 2019-02-18 01:11:00 |
|Adam | 2019-02-11 11:11:00 |
|Rhodesy | 2019-02-11 15:16:00 |
I have now tried and got the data to show me information between the times on one date using the below code, but now I would need to make this piece of code run for every date over a 3 month period
select *
from dbo.TestTable
where DateAndTimeTest between '2019-02-11 00:00:00' and '2019-02-11 08:30:00'
You can use SQL similar to following:
select *
from dbo.TestTable
where (CAST(DateAndTimeTest as date) between '2019-02-11' AND '2019-02-11') AND
(CAST(DateAndTimeTest as time) between '00:00:00' and '08:30:00')
Above query will return all records where DateAndTimeTest value in date range 2019-02-11 to 2019-02-11 and with time between 12AM to 8:30AM.

Get records after a certain time in PostgreSQL

I have a table that looks like this:
id | flight_number | departure_time | arrival_time
---+---------------+----------------+-------------
1 | UAL123 | 07:00:00 | 08:30:00
---+---------------+----------------+-------------
2 | AAL456 | 07:30:00 | 08:40:00
---+---------------+----------------+-------------
3 | SWA789 | 07:45:00 | 09:10:00
I'm trying to figure out an SQL query that can get upcoming flights based on departure time given the current time. For instance, at 07:20, I would like to return AAL456, SWA789 since those flights have not departed yet. At 07:40, I would like to just return SWA789. What is a good way to do this?
Well, you can use LOCALTIME to get the current time. So, if the departure_time is stored as a time, then:
select t.*
from t
where t.departure_time > localtime;
This assumes no time zone information is part of the time value. Also, it will return no flights after the last flight has departed for a day (which is consistent with the phrasing of your question).