SQL Where Clause Maximo IBM - sql

I want to create a where clause in Maximo.
We have a clause today where we can see the work order about to breach responded SLA.
But i want the clause to calculate to show work order about to breach responded SLA within 1 hour.
Is that possible?
Clause is as follows:
(woclass = 'WORKORDER' or woclass = 'ACTIVITY')
and historyflag = 0
and istask = 0
and workorderid in (select sla.ownerid from slarecords sla where sla.ownertable = 'WORKORDER' and sla.ownerid = workorderid)
and wonum not in (select wos.wonum from wostatus wos where wos.status = 'RESPONDED' and wos.siteid = siteid and wos.wonum = wonum)
and sysdate < targstartdate
and (((targstartdate - reportdate) - (targstartdate - sysdate))/(targstartdate - reportdate)*100)>90
and (siteid in (select defsite from maxuser mu where mu.sdx_siteteam is null and mu.userid = 'ALEXANDER.JEPPSON#SODEXO.COM' )
or (siteid in (select sis.siteid from sdx_integratedsites sis where sis.sdx_property = 'COMMANDCENTER' and sis.sdx_type = (select sdx_siteteam from maxuser mu where mu.userid = 'ALEXANDER.JEPPSON#SODEXO.COM' ))))

It sounds like you should change
and sysdate < targstartdate
to subtract an hour from the target
and sysdate < targstartdate - 1/24
or add an hour to the current time
and sysdate + 1/24 < targstartdate

Related

how to use double group by and sum statements together in oracle

I am trying to use query below but it is giving an error
SELECT s.LOCAL_CODE,substr(p.ACCOUNT_CREDIT,-3),(p.SUMMA/100) as profit
FROM OPERATIONS s INNER JOIN LEADS p ON s.PAY_ID = p.PAY_ID
WHERE s.date_paid >= TO_DATE('03.12.2019', 'DD.MM.YYYY')
AND s.date_paid < TO_DATE('03.12.2019', 'DD.MM.YYYY') + INTERVAL '1' DAY
AND state = 'T'
AND s.filial_code = '006789'
AND SUBSTR(p.ACCOUNT_CREDIT, 1, 5) = '765294'
GROUP BY s.LOCAL_CODE,substr(p.ACCOUNT_CREDIT,-3);
If LEADS.SUMMA has expected value then you don't need Group By clause, else if you use Group By then all not grouped fields can be used only as arguments of aggregate functions:
SELECT s.LOCAL_CODE
, Substr(p.ACCOUNT_CREDIT, -3)
, Sum(p.SUMMA)/100 as profit
FROM OPERATIONS s
INNER JOIN LEADS p ON s.PAY_ID = p.PAY_ID
WHERE s.date_paid >= TO_DATE('03.12.2019', 'DD.MM.YYYY')
AND s.date_paid < TO_DATE('03.12.2019', 'DD.MM.YYYY') + INTERVAL '1' DAY
AND state = 'T'
AND s.filial_code = '006789'
AND SUBSTR(p.ACCOUNT_CREDIT, 1, 5) = '765294'
GROUP BY s.LOCAL_CODE
, substr(p.ACCOUNT_CREDIT, -3);

Data Wont Return

I have added a timestamp statement to the below SQL but it is not adding it to the table.
with cx(seq, user_id, mod_date_time, menu_optn_name) as (
select 1, 'U1', timestamp '2007-05-21 16:00:00', 'O1' from dual),
cc(seq, user_id, mod_date_time, menu_optn_name) as (
select seq, user_id, cast(mod_date_time as date), menu_optn_name from cx)
select
user_id, MENU_OPTN_NAME, MOD_DATE_TIME,
case
when MOD_DATE_TIME > prior MOD_DATE_TIME+(1/96) and user_id = prior user_id then round((MOD_DATE_TIME - prior MOD_DATE_TIME)*1440,2)
else null
end as TIME_GAP
from
(select
ptt.user_id, MENU_OPTN_NAME, ptt.MOD_DATE_TIME,
row_number() over (partition by ptt.user_id order by ptt.MOD_DATE_TIME) seq
from PROD_TRKG_TRAN ptt
join cd_master cm on
ptt.cd_master_id = cm.cd_master_id
Where
MENU_OPTN_NAME = 'Cycle Cnt {Reserve}' --CHANGE BASED ON WHAT YOUR TRACKING... MENU NAMES AT THE BOTTOM
and ptt.user_id = 'SCOUNCIL' --CHANGE BASED ON WHO YOU WANT TO TRACK FOR A GAP...
and cm.cd_master_id =
(select cd_master_id
from cd_master
where
co = '&CO'
and div = '&DIV')
and ptt.create_date_time >=
/*Today*/ trunc(sysdate)
--/*This Week*/ trunc(sysdate-(to_char(sysdate,'D')-1))
--/*This Month*/ trunc(sysdate)-(to_char(sysdate,'DD')-1)
--/*Date Range*/ '&FromDate' and ptt.create_date_time-1 < '&ToDate'
--group by ptt.user_id
)cc
CONNECT BY
user_id = prior user_id
and seq = prior seq+1
start with
seq = 1
I have the query to show me anything with a time gap greater then 15 will tell me how long the gap is but I want it to account for the start time of the shift being 4PM so if they start work at 4pm and don't do anything in the system until 4:41PM then there was a 41 min gap.
not sure what I did wrong... I built the timestamp part separate and then put it into the query possibly its not meant to go with a query and should be on its own.
Thanks for any insight with this issue.

CASE WHEN expression in oracle

I have built a CASE WHEN expression to show me when someone has no system transaction in the system under their user id for anything greater then 15 minutes.
case
when MOD_DATE_TIME > prior MOD_DATE_TIME+(1/96)
and user_id = prior user_id
then round((MOD_DATE_TIME - prior MOD_DATE_TIME)*1440,2)
else null
end as TIME_GAP
That above is the case when statement only. The full query is below:
select
user_id, MENU_OPTN_NAME, MOD_DATE_TIME,
case
when MOD_DATE_TIME > prior MOD_DATE_TIME+(1/96) and user_id = prior user_id then round((MOD_DATE_TIME - prior MOD_DATE_TIME)*1440,2)
else null
end as TIME_GAP
from
(select
ptt.user_id, MENU_OPTN_NAME, ptt.MOD_DATE_TIME,
row_number() over (partition by ptt.user_id order by ptt.MOD_DATE_TIME) seq
from PROD_TRKG_TRAN ptt
join cd_master cm on
ptt.cd_master_id = cm.cd_master_id
Where
MENU_OPTN_NAME = 'Cycle Cnt {Reserve}' --CHANGE BASED ON WHAT YOUR TRACKING... MENU NAMES AT THE BOTTOM
and ptt.user_id = 'LLEE1' --CHANGE BASED ON WHO YOU WANT TO TRACK FOR A GAP...
and cm.cd_master_id =
(select cd_master_id
from cd_master
where
co = '&CO'
and div = '&DIV')
and ptt.create_date_time >=
/*Today*/ trunc(sysdate)
--/*This Week*/ trunc(sysdate-(to_char(sysdate,'D')-1))
--/*This Month*/ trunc(sysdate)-(to_char(sysdate,'DD')-1)
--/*Date Range*/ '&FromDate' and ptt.create_date_time-1 < '&ToDate'
--group by ptt.user_id
)cc
CONNECT BY
user_id = prior user_id
and seq = prior seq+1
start with
seq = 1
What I want the query to do is in the CASE WHEN clause. I want it to account for the shift start time being 4pm, so if they don't do anything till 4:41PM then I would see that listed as a 41 min gap.

SQL: combine case and number to date conversion

I want to do a sql statement which calculates in one table the substraction of 2 Date values. If the value is negative I just want to show it as the 0 value.
The number value is the number of seconds a payment is in a current state.
I convert it with a trick to a time value(Date type).
My current code is
SELECT
max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN to_char(to_date(max(round(SYSDATE - t1.time_event) * 24 * 60 * 60)),'ssssss'),'hh24:mi:ss') else to_char(to_date(0)) END) as "current_age"
from tbl_dummyfeed t1 join tbl_dummyfeed t2 on t1.payment_Id = t2.payment_id
where t1.event = 'accepted' and t2.event = 'enriched');
You could use a similar date 'trick', which is to add the fractional-days difference to a nominal date which has the time portion as midnight - you could use a fixed date, or trunc(sysdate), as long as the time ends up as midnight - without having to multiply by 24*60*60. (Your to_date() solution does the same thing implicitly, effectively adding the number of seconds to midnight on the first day of the current month; but this might be a little clearer). But you can also move the case clauses into the where filter
select to_char(date '1970-01-01'
+ nvl(max(sysdate - t1.time_event), 0), 'HH24:MI:SS') as "current_age"
from tbl_dummyfeed t1
join tbl_dummyfeed t2
on t1.trax_id = t2.trax_id
where t1.event = 'accepted'
and t1.time_event < sysdate
and t2.event = 'enriched'
and t2.time_event > sysdate;
You could also use an analytic approach so you only have to hit the table once, with a subquery that pairs up each 'enriched' time with the previous 'accepted' time for that ID, and which you then filter against the current time:
select to_char(date '1970-01-01'
+ nvl(max(sysdate - time_accepted), 0), 'HH24:MI:SS') as "current_age"
from (
select last_value(case when event = 'accepted' then time_event end ignore nulls)
over (partition by trax_id order by time_event) as time_accepted,
case when event = 'enriched' then time_event end as time_enriched
from tbl_dummyfeed
)
where time_accepted < sysdate
and time_enriched > sysdate;
This works, only the conversion to time hh24:mi:ss still needs to happen.
SELECT max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN round((SYSDATE - t1.time_event) * 24 * 60 * 60) else 0 END) as "current_age"
from tbl_dummyfeed t1 join tbl_dummyfeed t2 on t1.payment_id= t2.payment_id
where t1.event = 'accepted' and t2.event = 'enriched';
When I add the conversion to hh24:mm:ss The solution looks like this
SELECT to_char(to_date(max(CASE WHEN t1.time_event < SYSDATE and t2.time_event > SYSDATE THEN round((SYSDATE - t1.time_event) * 24 * 60
* 60) else 0 END),'sssss'),'hh24:mi:ss') as "current_age" from tbl_dummyfeed t1 join tbl_dummyfeed t2 on t1.trax_Id = t2.trax_id where t1.event = 'accepted' and t2.event = 'enriched';
This is the only good solution for my question. Hope this helps people.

SQL - creating date range

I am using the following follow script to show me the cap_date and max_capacity in the future and ... and sub-queries to show me the last_date and last_qty where older than the first select statement.
select
trunc(gp.cap_date) cap_date, gp.max_capacity,
(select max(trunc(gp1.cap_date))
from GPS_CAPACITY gp1
where gp.plant = gp1.plant
and gp.work_center_no = gp1.work_center_no
and gp1.cap_date = (select max(gp2.cap_date)
from GPS_CAPACITY gp2
where gp.plant = gp2.plant
and gp.work_center_no = gp2.work_center_no
and gp2.cap_date < gp.cap_date)) last_date,
(select max(gp1.max_capacity)
from GPS_CAPACITY gp1
where gp.plant = gp1.plant
and gp.work_center_no = gp1.work_center_no
and gp1.cap_date = (select max(gp2.cap_date)
from GPS_CAPACITY gp2
where gp.plant = gp2.plant
and gp.work_center_no = gp2.work_center_no
and gp2.cap_date < gp.cap_date)) last_qty
from
GPS_CAPACITY gp
where
gp.plant = 'W'
and gp.work_center_no = 'HPKG'
and trunc(gp.cap_date) > trunc(sysdate)
Output from this script looks like ...
... what I would like is to do is create a date list starting from the 'last_date' and show a qty for each date equal to last_qty; once the date list reaches the 'cap_date' qty should change to the max_capacity (the dates should run 7 days after the 'cap_date', i.e.
Any ideas?
Thanks
First create a cal table containing the range of dates of interest. Lets call that table cal with one column dt of type date. Assume your table above is called table_cap
Now do a
select cal.dt, case when cal.dt<cap_date then qty else max_capacity end
from cal
inner join table_cap on
(table_cap.last_date <= cal.dt
and cal.dt < adddate(capdate, interval 7 day )