Automation Script in maximo, how to count days between 2 dates without weekends and holidays? - jython

Automation Script in maximo, how to count days between 2 dates without weekends and holidays?
I could count days, and remove Sundays and Saturdays but what about holidays?
In Maximo present calendar application where all holidays is present. How I could use it with jython scripts?

Ok. I made it. Maximo has table "nonworktime" it contain all holidays from calendar application.
So, i write this jython script for automation Script in maximo. It return number of working days between 2 dates.
#*********************
def getDateDiff(start, finish):
calStart=0
calStart=Calendar.getInstance()
calStart.setTime(start)
calFinish=0
calFinish=Calendar.getInstance()
calFinish.setTime(finish)
daysDiff=0
str1=str(0)
cS=None
if calStart.getTime()<calFinish.getTime():
while calStart.getTime()<=calFinish.getTime():
if calStart.get(Calendar.DAY_OF_WEEK) !=1 and calStart.get(Calendar.DAY_OF_WEEK) !=7:
cS = SimpleDateFormat("dd.MM.yyyy").format(calStart.getTime())
myMXServer = MXServer.getMXServer()
userInfo = mbo.getThisMboSet().getUserInfo()
nwSet= myMXServer.getMboSet("NONWORKTIME", userInfo)
nwSetWhere = 'to_date( \''+cS+'\' , \'dd.mm.yyyy\' ) >= startdate and to_date(\''+cS+'\' , \'dd.mm.yyyy\' ) <=enddate'
nwSet.setWhere(nwSetWhere)
nwSet.reset()
if nwSet.count() ==0:
daysDiff = daysDiff+1
calStart.add(Calendar.DATE, 1)
else:
while calFinish.getTime()<=calStart.getTime():
if calFinish.get(Calendar.DAY_OF_WEEK) !=1 and calFinish.get(Calendar.DAY_OF_WEEK) !=7:
cS = SimpleDateFormat("dd.MM.yyyy").format(calFinish.getTime())
myMXServer = MXServer.getMXServer()
userInfo = mbo.getThisMboSet().getUserInfo()
nwSet= myMXServer.getMboSet("NONWORKTIME", userInfo)
nwSetWhere = 'to_date( \''+cS+'\' , \'dd.mm.yyyy\' ) >= startdate and to_date(\''+cS+'\' , \'dd.mm.yyyy\' ) <=enddate'
nwSet.setWhere(nwSetWhere)
nwSet.reset()
if nwSet.count() ==0:
daysDiff = daysDiff-1
calFinish.add(Calendar.DATE, 1)
return str(daysDiff)
#**********************
daysDiffernce = getDateDiff(plandate, factdate)

Related

<= less than equal to does't work as expected for sequilize query in nodejs

I have the following query in sequilize.js
const [results] = await sequelize.query(`select DISTINCT ON (variant_id) variant_id, Count(wl) as count, pds.name AS name,
CASE
WHEN pvs.name is NULL THEN pds.name
ELSE pvs.name
END
as variant_name,
im.url as img_url
from wish_lists as wl
LEFT JOIN products AS pds ON pds.id = wl.product_id
LEFT JOIN images as im on im.product_id = wl.product_id
LEFT JOIN product_variants as pvs ON pvs.id = wl.variant_id
where wl.created_at >= '${startDate}' AND wl.created_at <= '${endDate}' AND wl.tenant_id=${merchant_id}
group by ( variant_id, pds.name, im.url, pvs.name) LIMIT 100 OFFSET 0`)
The problem is if today is the 20th and i have products in the db that are selected on the 20th the query does't include those even though i have <= for the endDate, why is that ?
So why is the same day entries not being selected ?
Firstly, take a look at timezone of datetime data in database and in query param.
Secondly, created_at is the timestamp, which contain Time data (for example 2019-12-20T01:20:30.000Z). Then, your query param ${endDate} is just Date data only, it will append time = 0 (which is 00:00:00.000) before the comparison.
So the comparison would look like:
2019-12-20T01:20:30.000Z <= 2019-12-20T00:00:00.000Z
And the result is false, then it cannot be selected.

SQL Most recently updated row

I am pulling information from an SQL database but it displays all lines various times because of multiple updates.
i have managed to eliminate duplicate using the distinct function however still shows multiple lines for the changes.
i.e.
original Qty = 4
update 1 Qty = 5
update 2 Qty = 6
All i want is the most recent update each line which is column OLLNID
SELECT DISTINCT
OLMCU,
OLKCOO,
OLDOCO,
OLDCTO,
Date( OLTRDJ, CYYDDD ) AS OLTRDJ,
OLLNID AS OLLNID_1,
OLDSC1,
OLDSC2 AS OLDSC2_1,
OLUOM,
OLUORG,
OLPRRC,
OLAEXP,
OLANBY
FROM
E1PDES01.PRODDTA.F43199 F43199
WHERE
OLMCU = '13248'
AND Date( OLTRDJ, CYYDDD ) >= '01/01/2017'
AND OLDCTO = 'OP'
AND OLDOCO = 13484379
ORDER BY
6
Like subkonstrukt I am guessing what you really want.
In case that you want the latest results by OLLNID (which is then e.g. a date), then you could group by all other values (when they differ) and choose the max OLLNID.
SELECT
OLMCU,
OLKCOO,
OLDOCO,
OLDCTO,
Date(OLTRDJ, CYYDDD) AS OLTRDJ,
MAX(OLLNID) AS OLLNID_1,
OLDSC1,
OLDSC2 AS OLDSC2_1,
OLUOM,
OLUORG,
OLPRRC,
OLAEXP,
OLANBY
FROM E1PDES01.PRODDTA.F43199 F43199
WHERE OLMCU = '13248'
AND Date(OLTRDJ, CYYDDD) >= '01/01/2017'
AND OLDCTO = 'OP'
AND OLDOCO = 13484379
GROUP BY
OLMCU,
OLKCOO,
OLDOCO,
OLDCTO,
OLTRDJ,
OLDSC1,
OLDSC2,
OLUOM,
OLUORG,
OLPRRC,
OLAEXP,
OLANBY;
depending on what SQL Flavor you´re using this might not work, also I am made some wild assumptions on your data so if you could supply more information (maybe schema / types, SQL Flavor, maybe two or three dummy rows) I will edit my answer
SELECT
org.OLMCU,
org.OLKCOO,
org.OLDOCO,
org.OLDCTO,
Date(org.OLTRDJ, CYYDDD ) AS OLTRDJ,
org.OLLNID AS OLLNID_1,
org.OLDSC1,
org.OLDSC2 AS OLDSC2_1,
org.OLUOM,
org.OLUORG,
org.OLPRRC,
org.OLAEXP,
org.OLANBY
FROM
E1PDES01.PRODDTA.F43199 F43199 as org
LEFT OUTER JOIN E1PDES01.PRODDTA.F43199 F43199 aux ON org.OLLNID = aux.OLLNID AND org.OLLNID < aux.OLLNID
WHERE
org.OLMCU = '13248'
AND Date( org.OLTRDJ, CYYDDD ) >= '01/01/2017'
AND org.OLDCTO = 'OP'
AND org.OLDOCO = 13484379
AND aux.OLLNID IS NULL
GROUP BY
org.OLMCU,
org.OLKCOO,
org.OLDOCO,
org.OLDCTO,
Date(org.OLTRDJ, CYYDDD ) AS OLTRDJ,
org.OLLNID AS OLLNID_1,
org.OLDSC1,
org.OLDSC2 AS OLDSC2_1,
org.OLUOM,
org.OLUORG,
org.OLPRRC,
org.OLAEXP,
org.OLANBY
ORDER BY org.OLLNID

Mean time to Failure calculation in DAX

I am trying to calculate the mean time to failure for each asset in a job table. At the moment I calculate as follows;
Previous ID = CALCULATE(MAX('JobTrackDB Job'[JobId]),FILTER('JobTrackDB Job','JobTrackDB Job'[AssetDescriptionID]=EARLIER('JobTrackDB Job'[AssetDescriptionID]) && 'JobTrackDB Job'[JobId]<EARLIER('JobTrackDB Job'[JobId])))
Then I bring back the last finish time for the current job when the JobStatus is 7 (closed);
Finish Time = CALCULATE(MAX('JobTrackDB JobDetail'[FinishTime]),'JobTrackDB JobDetail'[JobId],'JobTrackDB JobDetail'[JobStatus]=7)
Then I bring back the previous jobs finish time where the JobType is 1 (Response rather than comparing it to maintenance calls);
Previous Finish = CALCULATE(MAX('JobTrackDB Job'[Finish Time]),FILTER('JobTrackDB Job','JobTrackDB Job'[AssetDescriptionID]=EARLIER('JobTrackDB Job'[AssetDescriptionID]) && 'JobTrackDB Job'[Finish Time]<EARLIER('JobTrackDB Job'[Finish Time]) && EARLIER('JobTrackDB Job'[JobTypeID])=1))
Then I calculate the Time between failure where I also disregard erroneous values;
Time between failure = IF([Previous Finish]=BLANK(),BLANK(),IF('JobTrackDB Job'[Date Logged]-[Previous Finish]<0,BLANK(),'JobTrackDB Job'[Date Logged]-[Previous Finish]))
Issue is that sometimes the calculation uses previous maintenance jobs even though I specified JobTypeID = 1 in the filter. Also, the current calculation does not take into account the time from the start of records to the first job for that asset and also from the last job till today. I am scratching my head trying to figure it out.
Any ideas???
Thanks,
Brent
Some base measures:
MaxJobID := MAX( Job[JobID] )
MaxLogDate := MAX ( Job[Date Logged] )
MaxFinishTime := MAX (JobDetail[Finish Time])
Intermediate calculations:
ClosedFinishTime := CALCULATE ( [MaxFinishTime], Job[Status] = 7 )
AssetPreviousJobID := CALCULATE (
[MaxJobID],
FILTER(
ALLEXCEPT(Job, Table1[AssetDescriptionID]),
Job[JobId] < MAX(Table1[JobID])
)
)
PreviousFinishTime: = CALCULATE ( [ClosedFinishTime],
FILTER(
ALLEXCEPT(Job, Job[AssetDescriptionID]),
Job[JobId] < MAX(Job[JobID])
&& Job[JobType] = 1
)
)
FailureTime := IF (
ISBLANK([PreviousFinishTime]),
0,
( [MaxLogDate]-[PreviousFinishTime] )
)
This should at least get you started. If you want to set some sort of "first day", you can replace the 0 in the FailureTime with a calc like MaxLogDate - [OverallFirstDate], which could be a calculated measure or a constant.
For something that hasn't failed, you'd want to use an entirely different measure since this one is based on lookback only. Something like [Days Since Last Failure] which would just be (basically) TODAY() - [ClosedFinishTime]

SQL Having statement with Date Range - The grouping is inconsistent

I'm struggling with this script. I want to write it where it'll calculate the total of instances then add the sum of those instances, however, when I run the script with the "HAVING....AND CSH.SLS_DT BETWEEN '2015-01-01' and '2015-06-16'" I get an error stating that "the grouping is inconsistent".
The script runs fine when I comment out the date range, but the date range is critical to this script. Thank you in advance for your help. Script is below:
SELECT OS.STR_NBR STORE
, CSH.CSHR_USER_ID LDAP
, COUNT(CSH.USER_ID) AS COUNT
, SUM(OS.OVR_SHRT_AMT) AS TOTAL
FROM PRHDW.VLT_CSHR_AUD CSH
LEFT OUTER
JOIN PRHDW.VLT_AUD_OVR_SHRT OS
ON OS.STR_NBR = CSH.STR_NBR
AND OS.SLS_DT = CSH.SLS_DT
AND OS.CSHR_SYSUSR_ID = CSH.CSHR_SYSUSR_ID
GROUP BY OS.STR_NBR, CSH.CSHR_USER_ID
HAVING OS.STR_NBR = '0121'
--AND SUM (OS.OVR_SHRT_AMT) > 0
AND CSH.SLS_DT BETWEEN '2015-01-01' AND '2015-06-16'
ORDER BY TOTAL DESC
WITH UR;
Based on your constraints it should work fine with a where clause. The intention of the having clause is to compare to some aggregate. Obviously you aren't doing that so
should read as follows:
AND OS.CSHR_SYSUSR_ID = CSH.CSHR_SYSUSR_ID
WHERE OS.STR_NBR = '0121'
AND CSH.SLS_DT BETWEEN '2015-01-01' AND '2015-06-16'
GROUP BY OS.STR_NBR, CSH.CSHR_USER_ID

SP Previous year sales data from given date

How to show current year data and previous Data in two column SQL Server 2000
Below Procedure shows my Given date Data I want to set Previous year data from given date in other column
SELECT TOP 100 PERCENT
dbo.SI_Item.ig2_Code, dbo.SI_ItemGroup2.ig2_Desc,
SUM(SI_InvoiceDetail.invcd_Rate * dbo.SI_InvoiceDetail.invcd_Qty -
dbo.SI_InvoiceDetail.invcd_DiscountAmt) AS Total
FROM
dbo.SI_InvoiceDetail
INNER JOIN
dbo.SI_Item ON dbo.SI_InvoiceDetail.itm_ItemCode = dbo.SI_Item.itm_ItemCode
INNER JOIN
dbo.SI_InvoiceMaster ON dbo.SI_InvoiceDetail.invcm_CoCode = dbo.SI_InvoiceMaster.invcm_CoCode AND
dbo.SI_InvoiceDetail.invcm_BrCode = dbo.SI_InvoiceMaster.invcm_BrCode AND
dbo.SI_InvoiceDetail.invcm_SiteCode = dbo.SI_InvoiceMaster.invcm_SiteCode AND
dbo.SI_InvoiceDetail.invcm_Year = dbo.SI_InvoiceMaster.invcm_Year AND
dbo.SI_InvoiceDetail.invcm_Period = dbo.SI_InvoiceMaster.invcm_Period AND
dbo.SI_InvoiceDetail.docs_DocCode = dbo.SI_InvoiceMaster.docs_DocCode AND
dbo.SI_InvoiceDetail.doctyp_Code = dbo.SI_InvoiceMaster.doctyp_Code AND
dbo.SI_InvoiceDetail.invcm_DocNo = dbo.SI_InvoiceMaster.invcm_DocNo
INNER JOIN
dbo.SI_ItemGroup2 ON dbo.SI_Item.ig2_Code = dbo.SI_ItemGroup2.ig2_Code
WHERE (dbo.SI_InvoiceDetail.docs_DocCode = 'inv') AND
(dbo.SI_InvoiceDetail.itm_ItemCode BETWEEN '0101010000001' AND '0301020004001') AND
(dbo.SI_InvoiceMaster.invcm_Date BETWEEN #Invcm_date_from AND #Invcm_date_to)
GROUP BY dbo.SI_Item.ig2_Code ,SI_ItemGroup2.ig2_Desc
Resolved it 70% by changing in Query as below
SELECT TOP 100 PERCENT dbo.SI_Item.ig2_Code, dbo.SI_ItemGroup2.ig2_Desc,year(dbo.SI_InvoiceMaster.invcm_Date) AS SalesYear,
&
Group By Year(dbo.SI_InvoiceMaster.invcm_Date),dbo.SI_Item.ig2_Code ,SI_ItemGroup2.ig2_Desc
ORDER BY Year(dbo.SI_InvoiceMaster.invcm_Date) ,dbo.SI_Item.ig2_Code
but its works but not as i want because its showing year in Row and User mut inpuer date range of aboce 2 years then will show.
if anyone Got Proper solution do share please