How to assign whether a specific date is during work hours or not. (SPSS) - date

Hi i'm new to this site so forgive me if i didn't search for this question thoroughly enough!
Basically i'm doing a research project where one of the variables is whether an xray was performed during normal work hours or not. Work hours include Monday-Friday, 8am-5pm.
I have input the date and time of each xray using separate variables
-[date+time (dd.mm.yyyy hh:mm)]
-[time (hh:mm)].
I was planning on inputing this information manually but i though that surely there is way to automate this and since i have next to zero experience with SPSS i thought i should ask you lovely people to help me out!
Thankyou in advance

Below I go through a quick example utilizing the xdate function to extract the day of week, and then construction an if statement to identify whether these date-times fall within workhours.
*Making example data.
data list free / xray_date (ADATE10) xray_time (TIME5).
begin data
7/6/2011 2:21
10/11/2011 15:42
07/06/2011 02:21
3/15/2011 0:21
end data.
*Here is example to find day of week name, 1 is Sunday and 7 is Saturday.
compute day_week = Xdate.Wkday(xray_date).
*to identify times of day in an if statement we need to make specific variables.
string begin_time end_time (A5).
compute begin_time = "08:00".
compute end_time = "17:00".
alter type begin_time end_time (TIME5).
*Then you can just make an if statement to identify whether a date-time meets your requirements.
compute workhours = 0.
if day_week >= 2 and day_week <= 6 and xray_time >= begin_time and xray_time <= end_time workhours = 1.
For this particular example, if you run the command list all. the resulting output will be;
xray_date xray_time day_week begin_time end_time workhours
07/06/2011 2:21 4.00000 8:00 17:00 .00000
10/11/2011 15:42 3.00000 8:00 17:00 1.00000
07/06/2011 2:21 4.00000 8:00 17:00 .00000
03/15/2011 0:21 3.00000 8:00 17:00 .00000
You can see the record with 10/11/2011 was appropriately classified as it is a Tuesday and within working hours. All of the other records are not between 8 am and 5 pm, so are initialized to the zero value for the workhours variable.

Related

Hi, I need to display data from a table (vw_tracking_resource_events), but only 14 days of data for a certain time (18:00 to 19:00)

I use PostgreSQL
I can run a single query per day, but it will take a long time to go through every day.
The "zone" and "reader" also changes, so to run single queries every time will keep me up until late.
If at best I can only change the "reader" and "zone" every time it would help. The main "PAIN" I have, is to change the dates every time. It will be from 2022 11 18 18:00 to 2022 12 01 19:00.
P.S - I'm new to SQL, please be gentle :)
My current query:
select * from vw_tracking_resource_events
where "when_enter_dt_timezone" between '2022 11 18 18:00:00' and '2022 11 18 19:00:00'
and "zone" = '085 Level'
and "site" = 'MK'
and "reader" = 'RV Shaft'
and "group" = 'Lamp'
If you cast your field to separate and compare the date part and the time part to desired ranges, it becomes super easy:
WHERE when_enter_dt_timezone BETWEEN '2022-11-18' AND '2022-12-01T23:59:59.999'
AND when_enter_dt_timezone::time BETWEEN '18:00' AND '19:00'
Edit:
#Stefanov.sm makes a very good point regarding the casting of the timestamp to type date (1st criterion above) if an index can be used to retrieve data.
I corrected the query to take his remark.
Disclaimer: With when_enter_dt_timezone::date BETWEEN ... AND 2022-12-01, you include e.g. 2021-12-01T18:30.
Without the cast, the upper bound 2022-12-01 is implicitly set to midnight (morning); you will either have to change the upper bound to 2022-12-02 (which #Stefanov.sm suggested and works very well since you have a condition on the time anyway) or set your upper bound to 2022-12-01T23:59:59.999 (which is what I did above, although only to draw your attention to this specific issue).
You can try something like this to get records for the last 14 days between 6:00 p.m. and 7:00 p.m.
select * from vw_tracking_resource_events
where when_enter_dt_timezone > current_date - interval '14' day and
when_enter_dt_timezone::time between time '18:00' AND time '19:00'
Demo in sqldaddy.io
Modified using #Atmo notes
and #a_horse_with_no_name

How to calculate the weekend when counting a date range in Google Sheets?

I have the below columns
StartDate EndDate CountDay
01 May 20 05 May 20 ?
As you see, 01 May is Friday, so from 01-05 May if we count all days including weekend it will be 4 days.
What I want is on column "CountDay" it only counts the Workdays, not the weekend.
SO the expected result would be 2.
Anyone know how to do it using a formula in Google Sheets?
Do you consider Fridays as part of the weekend?
If yes, then you could also try the following formula:
=NETWORKDAYS.INTL(A10, B10,"0000111")
If not, please use this formula:
=NETWORKDAYS.INTL(A10, B10)
How the formulas work.
By using the function NETWORKDAYS.INTL we can "adjust" the weekend (non-working weekdays) to our liking.
In this case we account Fridays as our non-working weekdays by using as the 3rd parameter 0000111 instead of the default 0000011 where every 0 represents a working weekday and every 1 a non-working weekday.
(Very useful for people working part-time)
Someone who has part-time work on only Mondays, Wednesdays and Fridays and wants to calculate the working days Friday, 1 May 2020 - Tuesday, 30 June 2020 could adjust the formula to:
=NETWORKDAYS.INTL(A10, B10,"0101011")
As explained on the official Google help page for NETWORKDAYS.INTL
weekend – [ OPTIONAL – 1 by default ] – A number or string representing which days of the week are considered weekends.
String method: Weekends can be specified using seven 0s and 1s, where the first number in the set represents Monday and the last number is for Sunday. A zero means that the day is a work day, a 1 means that the day is a weekend. For example, “0000011” would mean Saturday and Sunday are weekends.
Number method: Instead of using the string method above, a single number can be used. 1 = Saturday/Sunday are weekends, 2 = Sunday/Monday and this pattern repeats until 7 = Friday/Saturday. 11 = Sunday is the only weekend day, 12 = Monday is the only weekend day and this pattern repeats until 17 = Saturday is the only weekend day.
I just found how to do it:
=if(weeknum(A10)<weeknum(B10),B10-A10-2*(weeknum(B10)-weeknum(A10)),B10-A10)
something like that

how to find number of days since 28th of last month till 27th of current month in db2

I need to generate a report on 28th of every month .
So for that I need to run an autosys job.
In that I have a query with the condition
validation_date >= (number of days since last run)
Could you please help me on this .How can I achieve this condition in DB2 ?
This is a monthly job.So I don't want to hard code my previous run date in the query .At the same time I need to get a condition which satisfies for all the months .
Note :
If the query is running on feb 28th ,then feb 28th is not included. I need to get data from january 28th(included) till feb 27th(included)
similarly for march 28th run ,I need to get data from feb 28th(included) till march 27th(included)...Thanks in advance.Please help
Consider putting your report generation in a procedure, and parameterizing the start and end dates. In other words, have something like this:
create procedure monthly_report(
start_date date,
end_date date
)
language sql
begin
... report queries here ...
end
Now you potentially have something much more flexible (depending on the report requirements). If, in the future, you want to run a report on a different day, or for a different length of time, you will be able to do that.
Once you design it this way, it also may be easier to set the dates in your job scheduling script, rather than in SQL. If you did it in SQL, you could do something like this:
call monthly_report(
(select
year(current timestamp - 2 months) ||'-'||
month(current timestamp - 2 months) ||'-'||
'28' from sysibm.sysdummy1
),
(select
year(current timestamp - 1 month) ||'-'||
month(current timestamp - 1 month) ||'-'||
'27' from sysibm.sysdummy1
)
)
You may need to tweak it to handle some edge cases (I'm not exactly sure if you care what happens if it runs on the 29th of the month, and if so, how to handle it). But you get the basic approach.
You can use DAY() function that extracts day of month from date and you can use it for triggering job. for example where day(param)=28.
other two parameters can be calculated with date calculation , here is example for trigger , date_to value and date_from value
select day(timestamp_format(20170228,'yyyyMMdd') ),timestamp_format(20170228,'yyyyMMdd')- 1 DAY,timestamp_format(20170228,'yyyyMMdd') -1 month from sysibm.sysdummy1;
if your parameter/column is date/timestamp you can remove timestamp_format(20170228,'yyyyMMdd') function and just put your column/parameter

Matlab- Changing uniques date code values to more manageable data

I have a variable called sentDate which stores the month and day from Nov 27th - Dec 6th.Each day has a number of sentiment ratings that it represents therefore I need to assign unique day codes to each day so I can perform...
allSents(dayCodes==1)
So far I have managed to assign day codes using...
[a,b,dayCodes]=unique(sentDate);
[d,e,allSents]=unique(sentiment);
However the day codes take the last digit on the date e.g 27th becomes 7, 28th becomes 8, etc. I need it so the day codes start from 1 and increase for each day until the 6th of December, therefore 1-11.
Any idea on how I may do this ?
have you tried the datenum function? then subtract off whatever offset to give the appropriate start day number.
For those who may have a similar problem, by specifying stable in as a parameter e.g
[a,b,dayCodes]=unique(sentDate,'stable');
Will specify the daycodes in the same order as in sentDate.

Need to sort by Date then Hour, then output Date, text Day of week , range of hours SQL Server 2008 R2

NEWBIE at work! I am trying to create a simple summary that counts the number of customer visits and groups by 1) date and 2) hour, BUT outputs this:
Date Day of Wk Hour #visits
8/12/2013 Monday 0 5
8/12/2013 Monday 1 7
8/12/2013 Monday 6 10
8/13/2013 Tuesday 14 25
8/13/2013 Tuesday 16 4
We are on military time, so 14 = 2:00 pm
Select
TPM300_PAT_VISIT.adm_ts as [Date]
,TPM300_PAT_VISIT.adm_ts as [Day of Week]
,TPM300_PAT_VISIT.adm_ts as [Hour]
,count(TPM300_PAT_VISIT.vst_ext_id) as [Total Visits]
From
TPM300_PAT_VISIT
Where
TPM300_PAT_VISIT.adm_srv_cd='22126'
and TPM300_PAT_VISIT.adm_ts between '07-01-2013' and '08-01-2013'
Group by
cast(TPM300_PAT_VISIT.adm_ts as DATE)
,datepart(weekday,TPM300_PAT_VISIT.adm_ts)
,datepart(hour,TPM300_PAT_VISIT.adm_ts)
Order by
CAST(TPM300_PAT_VISIT.adm_ts as DATE)
,DATEPART(hour,TPM300_PAT_VISIT.adm_ts)
This should solve the problem:
; With Streamlined as (
SELECT
DATEADD(hour,DATEDIFF(hour,'20010101',adm_ts),'20010101') as RoundedTime,
vst_ext_id
from
TPM300_PAT_VISIT
where
adm_srv_cd='22126' and
adm_ts >= '20130701' and
adm_ts < '20130801'
)
Select
CONVERT(date,RoundedTime) as [Date],
DATEPART(weekday,RoundedTime) as [Day of Week],
DATEPART(hour,RoundedTime) as [Hour],
count(vst_ext_id) as [Total Visits]
From
Streamlined
Group by
RoundedTime
Order by
CONVERT(date,RoundedTime),
DATEPART(hour,RoundedTime)
In the CTE (Streamlined)'s select list, we floor each adm_ts value down to the nearest hour using DATEADD/DATEDIFF. This makes the subsequent grouping easier to specify.
We also specify a semi-open interval for the datetime comparisons, which makes sure we include everything in July (including stuff that happened at 23:59:59.997) whilst excluding events that happened at midnight on 1st August. This is frequently the correct type of comparison to use when working with continuous data (floats, datetimes, etc), but means you have to abandon BETWEEN.
I'm also specifying the dates as YYYYMMDD which is a safe, unambiguous format. Your original query could have been interpreted as either January 7th - January 8th or 1st July - 1st August, depending on the settings of whatever account you use to connect to SQL Server. Better yet, if these dates are being supplied by some other (non-SQL) code, would be for them to be passed as datetimes in the first place, to avoid any formatting issues.