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

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)

Related

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

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

how to group dates from ms access database as week of month using excel vba

I am using MS access 2010 database and working with Excel VBA to connect to the database and make queries. Suppose I have a table named "MyTable" like this below:
----------------------
| Date | Count |
----------------------
|7/7/16 | 12 |
----------------------
|7/8/16 | 15 |
----------------------
|7/15/16 | 18 |
----------------------
|7/18/16 | 16 |
----------------------
|8/7/16 | 15 |
----------------------
|8/8/16 | 10 |
----------------------
|8/15/16 | 9 |
----------------------
|8/16/16 | 18 |
----------------------
Now I want to use query to get a table like this:
----------------------
|Week by Month | Sum |
----------------------
|July Week 2 | 27 |
----------------------
|July Week 3 | 18 |
----------------------
|July Week 4 | 16 |
----------------------
|Aug Week 2 | 25 |
----------------------
|Aug Week 3 | 27 |
----------------------
Use DatePart to get the week of the year, then subtract the week of the first day of the month (zero based week of the month) and then add 1 (to get to a one based week of the month:
Public Function WeekOfMonth(x As Date) As Integer
WeekOfMonth = DatePart("ww", x) - _
DatePart("ww", DateSerial(Year(x), Month(x), 1)) _
+ 1
End Function
Note that the Access SQL version should be idential to what's after the = sign.
I have solved this as below:
select weeknum, sum(count1) from (
select format(date1,'MMM') & " Week - " & int((datepart('d',date1,1,1) -1 ) / 7 + 1) as weeknum, count1 from MyTable)
group by weeknum
Show Week of Month where Week 1 is always the 1st Full Week of the Month starting in that month (First Sunday is 1 or 2 or 3 or 4 or 5 or 6 or 7), days of the month prior to the first Sunday are counted as week 4/5 of previous month.
After searching and failing to find EXACTLY the right answer for my situation - I modified ComIntern's solution as follows. This is used a CONTROL on a REPORT, where [StartDate] is a criteria on the form that calls/generates the report:
=IIf((DatePart("ww",[StartDate]-7)-DatePart("ww",DateSerial(Year([StartDate]-7),Month([StartDate]-7),1))+1)="5","1",DatePart("ww",[StartDate])-DatePart("ww",DateSerial(Year([StartDate]),Month([StartDate]),1))+0)
This results in showing the Week of Month based on FULL weeks - and accounts for when the previous month's week 5 included 1 or more days from this month.
For example - Week 5 of Oct 2017 is 29 OCT - 04 NOV. If I did not include the IIF statement to adjust the formula, 05-11 NOV is returned as Week 2, but for my reporting purposes it is Week 1 of NOV. I have tested this out and appears to ALWAYS work, if you need to see Week of Month, based on FULL weeks, this should work for you!

Access Query: get difference of dates with a twist

I'm going to do my best to explain this so I apologize in advance if my explanation is a little awkward. If I am foggy somewhere, please tell me what would help you out.
I have a table filled with circuits and dates. Each circuit gets trimmed on a time cycle of about 36 months or 48 months. I have a column that gives me this info. I have one record for every time the a circuit's trim cycle has been completed. I am attempting to link a known circuit outage list, to a table with their outage data, to a table with the circuit's trim history. The twist is the following:
I only want to get back circuits that have exceeded their trim cycles by 6 months. So I would need to take all records for a circuit, look at each individual record, find the most recent previous record relative to the record currently being examined (I will need every record examined invididually), calculate the difference between the two records in months, then return only the records that exceeded 6 months of difference between any two entries for a given feeder.
Here is an example of the data:
+----+--------+----------+-------+
| ID | feeder | comp | cycle |
| 1 | 123456 | 1/1/2001 | 36 |
| 2 | 123456 | 1/1/2004 | 36 |
| 3 | 123456 | 7/1/2007 | 36 |
| 4 | 123456 | 3/1/2011 | 36 |
| 5 | 123456 | 1/1/2014 | 36 |
+----+--------+----------+-------+
Here is an example of the result set I would want (please note: cycle can vary by circuit, so the value in the cycle column needs to be in the calculation to determine if I exceeded the cycle by 6 months between trimmings):
+----+--------+----------+-------+
| ID | feeder | comp | cycle |
| 3 | 123456 | 7/1/2007 | 36 |
| 4 | 123456 | 3/1/2011 | 36 |
+----+--------+----------+-------+
This is the query I started but I'm failing really hard at determining how to make the date calculations correctly:
SELECT temp_feederList.Feeder, Temp_outagesInfo.causeType, Temp_outagesInfo.StormNameThunder, Temp_outagesInfo.deviceGroup, Temp_outagesInfo.beginTime, tbl_Trim_History.COMP, tbl_Trim_History.CYCLE
FROM (temp_feederList
LEFT JOIN Temp_outagesInfo ON temp_feederList.Feeder = Temp_outagesInfo.Feeder)
LEFT JOIN tbl_Trim_History ON Temp_outagesInfo.Feeder = tbl_Trim_History.CIRCUIT_ID;
I wasn't really able to figure out where I need to go from here to get that most recent entry and perform the mathematical comparison. I've never been asked to do SQL this complex before, so I want to thank all of you for your patience and any assistance you're willing to lend.
I'm making some assumptions, but this uses a subquery to give you rows in the feeder list where the previous completed date was greater than the number of months ago indicated by the cycle:
SELECT tbl_Trim_History.ID, tbl_Trim_History.feeder,
tbl_Trim_History.comp, tbl_Trim_History.cycle
FROM tbl_Trim_History
WHERE tbl_Trim_History.comp>
(SELECT Max(DateAdd("m", tbl_Trim_History.cycle, comp))
FROM tbl_Trim_History T2
WHERE T2.feeder = tbl_Trim_History.feeder AND
T2.comp < tbl_Trim_History.comp)
If you needed to check for longer than 36 months you could add an arbitrary value to the months calculated by the DateAdd function.
Also I don't know if the value of cycle specified the number of month from the prior cycle or the number of months to the next one. If the latter I would change tbl_Trim_History.cycle in the DateAdd function to just cycle.
SELECT tbl_trim_history.ID, tbl_trim_history.Feeder,
tbl_trim_history.Comp, tbl_trim_history.Cycle,
(select max(comp) from tbl_trim_history T
where T.feeder=tbl_trim_history.feeder and
t.comp<tbl_trim_history.comp) AS PriorComp,
IIf(DateDiff("m",[priorcomp],[comp])>36,"x") AS [Select]
FROM tbl_trim_history;
This query identifies (with an X in the last column) the records from tbl_trim_history that exceed the cycle time - but as noted in the comments I'm not entirely sure if this is what you need or not, or how to incorporate the other 2 tables. Once you see what it is doing you can modify it to only keep the records you need.

How to calculate the broadcast year and month out of the given date?

Is there a way to calculate the the broadcast year and month for a given gregorian date?
The advertising broadcast calendar differs from the regular calendar, in the way that every month needs to start on a Monday and end on a Sunday and have exactly 4 or 5 weeks. You can read about it here: http://en.wikipedia.org/wiki/Broadcast_calendar
This is a pretty common thing in TV advertising, so I guess there is a standard mathematical formula for it, that uses a combination of date functions (week(), month(), etc...).
Here is an example mapping between gregorian and broadcast dates:
| gregorian_date | broadcast_month | broadcast_year |
+----------------+-----------------+----------------+
| 2014-12-27 | 12 | 2014 |
| 2014-12-28 | 12 | 2014 |
| 2014-12-29 | 1 | 2015 |
| 2014-12-30 | 1 | 2015 |
| 2014-12-31 | 1 | 2015 |
| 2015-01-01 | 1 | 2015 |
| 2015-01-02 | 1 | 2015 |
Here is example how the broadcast calendar looks for 2015:
http://www.rab.com/public/reports/BroadcastCalendar_2015.pdf
As far as I can see, the pattern is that the first of the Gregorian month always falls within the first week of the Broadcast calendar, and any days from the previous month are pulled forward into that month to create full weeks. In Excel, you can use the following formula in cell B2 (first of your broadcast months above) to calculate the broadcast month:
=MONTH(A2+(7-WEEKDAY(A2,2)))
Similarly, in cell C2:
=IF(AND(MONTH(A2)=12,B2=1),YEAR(A2)+1,YEAR(A2))
This will return the broadcast month and year for any dates you put into your data set.
Hope that helps!
month,first,last
2018_1,2018-01-01,2018-01-28
2018_2,2018-01-29,2018-02-25
2018_3,2018-02-26,2018-03-25
2018_4,2018-03-26,2018-04-29
2018_5,2018-04-30,2018-05-27
2018_6,2018-05-28,2018-06-24
2018_7,2018-06-25,2018-07-29
2018_8,2018-07-30,2018-08-26
2018_9,2018-08-27,2018-09-30
2018_10,2018-10-01,2018-10-28
2018_11,2018-10-29,2018-11-25
2018_12,2018-11-26,2018-12-30
2019_1,2018-12-31,2019-01-27
2019_2,2019-01-28,2019-02-24
2019_3,2019-02-25,2019-03-31
2019_4,2019-04-01,2019-04-28
2019_5,2019-04-29,2019-05-26
2019_6,2019-05-27,2019-06-30
2019_7,2019-07-01,2019-07-28
2019_8,2019-07-29,2019-08-25
2019_9,2019-08-26,2019-09-29
2019_10,2019-09-30,2019-10-27
2019_11,2019-10-28,2019-11-24
2019_12,2019-11-25,2019-12-29
2020_1,2019-12-30,2020-01-26
2020_2,2020-01-27,2020-02-23
2020_3,2020-02-24,2020-03-29
2020_4,2020-03-30,2020-04-26
2020_5,2020-04-27,2020-05-31
2020_6,2020-06-01,2020-06-28
2020_7,2020-06-29,2020-07-26
2020_8,2020-07-27,2020-08-30
2020_9,2020-08-31,2020-09-27
2020_10,2020-09-28,2020-10-25
2020_11,2020-10-26,2020-11-29
2020_12,2020-11-30,2020-12-27
2021_1,2020-12-28,2021-01-31
2021_2,2021-02-01,2021-02-28
2021_3,2021-03-01,2021-03-28
2021_4,2021-03-29,2021-04-25
2021_5,2021-04-26,2021-05-30
2021_6,2021-05-31,2021-06-27
2021_7,2021-06-28,2021-07-25
2021_8,2021-07-26,2021-08-29
2021_9,2021-08-30,2021-09-26
2021_10,2021-09-27,2021-10-31
2021_11,2021-11-01,2021-11-28
2021_12,2021-11-29,2021-12-26
2022_1,2021-12-27,2022-01-30
2022_2,2022-01-31,2022-02-27
2022_3,2022-02-28,2022-03-27
2022_4,2022-03-28,2022-04-24
2022_5,2022-04-25,2022-05-29
2022_6,2022-05-30,2022-06-26
2022_7,2022-06-27,2022-07-31
2022_8,2022-08-01,2022-08-28
2022_9,2022-08-29,2022-09-25
2022_10,2022-09-26,2022-10-30
2022_11,2022-10-31,2022-11-27
2022_12,2022-11-28,2022-12-25
2023_1,2022-12-26,2023-01-29
2023_2,2023-01-30,2023-02-26
2023_3,2023-02-27,2023-03-26
2023_4,2023-03-27,2023-04-30
2023_5,2023-05-01,2023-05-28
2023_6,2023-05-29,2023-06-25
2023_7,2023-06-26,2023-07-30
2023_8,2023-07-31,2023-08-27
2023_9,2023-08-28,2023-09-24
2023_10,2023-09-25,2023-10-29
2023_11,2023-10-30,2023-11-26
2023_12,2023-11-27,2023-12-31
2024_1,2024-01-01,2024-01-28
2024_2,2024-01-29,2024-02-25
2024_3,2024-02-26,2024-03-31
2024_4,2024-04-01,2024-04-28
2024_5,2024-04-29,2024-05-26
2024_6,2024-05-27,2024-06-30
2024_7,2024-07-01,2024-07-28
2024_8,2024-07-29,2024-08-25
2024_9,2024-08-26,2024-09-29
2024_10,2024-09-30,2024-10-27
2024_11,2024-10-28,2024-11-24
2024_12,2024-11-25,2024-12-29
2025_1,2024-12-30,2025-01-26
2025_2,2025-01-27,2025-02-23
2025_3,2025-02-24,2025-03-30
2025_4,2025-03-31,2025-04-27
2025_5,2025-04-28,2025-05-25
2025_6,2025-05-26,2025-06-29
2025_7,2025-06-30,2025-07-27
2025_8,2025-07-28,2025-08-31
2025_9,2025-09-01,2025-09-28
2025_10,2025-09-29,2025-10-26
2025_11,2025-10-27,2025-11-30
2025_12,2025-12-01,2025-12-28

Design Hours of Operation SQL Table

I am designing a SQL table to store hours of operation for stores.
Some stores have very simple hours: Monday to Sunday from 9:30AM to 10:00PM
Others are little more complicated. Please consider the following scenario:
Monday: Open All Day
Tuesday: 7:30AM – 2:30PM & 4:15PM – 11:00 PM
Wednesday: 7:00PM – 12:30 AM (technically closing on Thursday morning)
Thursday: 9:00AM – 6:00PM
Friday: closed.
How would you design the table(s)?
EDIT
The hours will be used to showing if a store is open at a user selected time.
A different table can probably handle any exceptions, such as holidays.
The store hours will not change from week to week.
A table like this would be easy for both the output you posted, as well as just firing a bit back (open? yes/no):
Store | Day | Open | Closed
---------------------------
1 | 1 | 0000 | 2400
1 | 2 | 0730 | 1430
1 | 2 | 1615 | 2300
...
Features:
Using 24-hour isn't necessary, but makes math easier.
Store ID would presumably join to a lookup table where you stored Store information
Day ID would translate to day of week (1 = Sunday, 2 = Monday, etc.)
To query for your dataset, just:
SELECT Day, Open, Close... (you'd want to format Open/Close obviously)
To query IsOpen?, just:
SELECT CASE WHEN #desiredtime BETWEEN Open AND Closed THEN 1 ELSE 0 END
FROM table
WHERE store = #Store
Think of it more as defining time frames, days / weeks are more complex, because they have rules and defined start and stops.
How would you define a timeframe?
one constraint (Start[Time and Day]), one reference 'Duration' (hours, minutes,.. of the span)*. Now the shifts (timeframes) can span multiple days and you don't have to work complex logic to extract and use the data in calculations.
**Store_Hours**
Store | Day | Open | DURATION
---------------------------
1 | 1 | 0000 | 24
1 | 2 | 0730 | 7
1 | 2 | 1615 | 6.75
...
1 | 3 | 1900 | 5.5
Do you have to do more than just store and display it?
I think a design which needs to tell if a store is open at a particular time would have to be informed by all of the possibilities, otherwise, you will end up not being able to accommodate something.
What about holiday exceptions?
I would consider storing them as intervals based on a base time (minutes since time 0 on a week).
So 0 is midnight on Monday.
Interval 1 would be 0 - 1440
Interval 2 would be 1890 - 2310
etc.
You could easily convert a user selected time into a minute offset and determine if a store was open.
Your only problem remaining would be interpretation in display for friendly display (probably some extensive logic, but not impossible) and overlap at time 10080 -> 0.