IBM Maximo Where Clause - sql

I am trying to create a where clause to return all work orders between set dates where the there are no ACTUALS recorded (no labtrans). I have an existing WC which i am using as a template for this one but i am stuck with the "where" element.
(woclass = 'WORKORDER' or woclass = 'ACTIVITY') and
istask = 0 and
worktype = 'PPM' and *This could be removed*
targcompdate >= { ts '2020-05-01 00:00:00.000' } and
targcompdate >= { ts '2020-05-05 00:00:00.000' } and
not exists?? there are no actuals are on the work order?
Many Thanks,
G

The easy way, trusting that the rest of Maximo has done its job like it normally does, would be something like the following. (I'm going from memory. You can double-check the column names on the WORKORDER object / table in Database Configuration or in your database browser tool, like SQL Developer.)
(woclass = 'WORKORDER' or woclass = 'ACTIVITY') and
istask = 0 and
worktype = 'PPM' and *This could be removed*
targcompdate >= { ts '2020-05-01 00:00:00.000' } and
targcompdate >= { ts '2020-05-05 00:00:00.000' } and
actlabcost = 0 and
actmatcost = 0 and
actservcost = 0 and
acttoolcost = 0

This works for me:
(woclass = 'WORKORDER' or woclass = 'ACTIVITY') and
(targcompdate between '2020-05-01' and '2020-05-05' and historyflag = 0 and istask = 0)
and not exists(select 1 from labtrans where refwo=workorder.wonum and siteid=workorder.siteid)

Related

sql query works in azure data studio but returns nothing via vs code

I have the following SQL query that returns a few rows when I try in Azure Data Studio:
SELECT
A.ReportRefreshDate, COUNT(ActivityName) AS TotalActivity,
ActivityName as ActivityType
FROM
WidgetActivity A
INNER JOIN
WidgetUsers ON LOWER(WidgetUsers.UserPrincipalName) = LOWER(A.UserId)
WHERE
A.TMID = 4
AND WidgetUsers.Branch = '52'
AND A.ReportRefreshDate BETWEEN DATEADD(DD, -300, GETDATE()) AND GETDATE()
AND A.ActivityName IN ('Action1', 'Action2')
GROUP BY
A.ActivityName, A.ReportRefreshDate
This returns a few rows like this:
ReportRefreshDate TotalActivity ActivityType
-----------------------------------------------------
2022-05-16 07:18:06.000 9 Action1
2022-05-16 07:18:06.000 3 Action2
But when I try this in VS Code, it returns an empty result set.
Here's the code snippet:
Console.WriteLine(activityFilter);
activityByActivityType = conn.Query<WidgetActivityDetailsByActivityTypes>(
#"
SELECT A.ReportRefreshDate, COUNT(ActivityName) AS TotalActivityCount, ActivityName AS ActivityType
FROM WidgetActivity A
INNER JOIN WidgetUsers
ON LOWER(WidgetUsers.UserPrincipalName) = LOWER(A.UserId)
WHERE
A.TMID = #TM
AND WidgetUsers.Branch = #Branch
AND A.ReportRefreshDate BETWEEN DATEADD(DD, -#RefreshDate, GETDATE()) AND GETDATE()
AND A.ActivityName IN (#ActivityList)
GROUP BY A.ActivityName, A.ReportRefreshDate
",
new { Branch=branchId, TM = tmid, RefreshDate = dateRange, ActivityList = activityFilter}).ToList();
}
return activityByActivityType;
I think the problem is related to the #ActivityList variable because when I remove that AND clause, a bunch of data is returned.
The Console.Writeline on activityFilter returns this:
'Activity1', 'Activity2'
which looks correct to me.
I'm sure it's something simple I've missed but I can't see it.
Any help would be appreciated.
I had to change the sql to look like this:
activityByActivityType = conn.Query<WidgetActivityDetailsByActivityTypes>(
$#"
SELECT A.ReportRefreshDate, COUNT(ActivityName) AS TotalActivityCount, ActivityName AS ActivityType
FROM WidgetActivity A
INNER JOIN WidgetUsers
ON LOWER(WidgetUsers.UserPrincipalName) = LOWER(A.UserId)
WHERE
A.TMID = #TM
AND WidgetUsers.Branch = #Branch
AND A.ReportRefreshDate BETWEEN DATEADD(DD, -#RefreshDate, GETDATE()) AND GETDATE()
AND A.ActivityName IN ({activityFilter})
GROUP BY A.ActivityName, A.ReportRefreshDate
",
new { Branch=branchId, TM = tmid, RefreshDate = dateRange}).ToList();
}

Oracle query works in Toad Developer but not in .net application

This query works in Toad developer but not(wont return results) in an .net application in visual studio. Any clues why?
The query work if I omit the line -- having sum(total_cust_cnt) >= NVL (:customers_out, 100)
select d.region_nbr, pi.city, d.case_id, c.cause_desc cause, sum(total_cust_cnt) customers, d.total_duration_minutes
from t_om_cause_of_trouble c,
t_om_archive_hist_device d,
t_om_archive_hist_loc l,
t_om_pod_info pi
where c.cause = d.cause_of_trouble
and pi.city is not null
and pi.total_cust_cnt > 0
and pi.company = l.company
and pi.distribution_location = l.distribution_location_nbr
and l.company = d.company
and l.region_nbr = d.region_nbr
and l.outage_system = d.outage_system
and l.case_id = d.case_id
and d.first_call_date_time >= :start_date
and d.first_call_date_time <= :end_date
and d.service_request_type = 'LGTS'
and d.cause_of_trouble not in
(select distinct cs.cause from t_om_cause_by_summary cs, t_om_cause_of_trouble c
where summary_cause = 'ERROR' and cs.cause = c.cause)
and d.device_type <> 'ERR'
and d.total_duration_minutes >= 60 * NVL(:hours_out,4)
and d.total_duration_minutes > 5
and d.total_customers_affected >= NVL (:customers_out, 100)
and to_char(d.region_nbr) like :region_nbr
group by d.region_nbr, pi.city, d.case_id, c.cause_desc, d.total_duration_minutes
/* having sum(total_cust_cnt) >= NVL (:customers_out, 100) */
order by d.region_nbr, pi.city, d.case_id
)
group by region_nbr, city, cause
order by region_nbr, city, cause
) stats
where r.region = stats.region_nbr
)

Outer join operator not allowed with OR in my WHERE clause - Error: ORA-01719

I need to check for the conditions at the bottom, but I cannot change use OR because of the (+) that are involved. I did not set that part up, so I am not sure how to rearrange it. Can someone tell me what I could do to get those conditions to go through?
SELECT DISTINCT
t.tkt_sid,
ts.tkt_sts_dsc,
tp.prty_dsc,
a.agt_cont_nbr,
au_a.user_nm assigned_to,
t.rcd_lst_user_ts
FROM
afp_asd.tkt t,
afp_asd.tkt_prty tp,
afp_asd.tkt_sts ts,
afp_asd.tkt_type tt,
afp_asd.tkt_log tl,
afp_asd.agt a,
afp_asd.asd_user au_a,
afp_asd.asd_user au_c,
afp_asd.asd_user au_l,
afp_asd.asd_user au_log,
afp_asd.prb_area pa1,
afp_asd.prb_area pa2,
afp_asd.prb_area pa3,
afp_asd.mktg_org mo,
afp_asd.src_sys ss,
afp_asd.agt ofc,
afp_asd.co c,
afp_asd.agt_sts ast
WHERE (
t.prty_cd = tp.prty_cd
AND t.tkt_sts_cd = ts.tkt_sts_cd
AND t.tkt_type_cd = tt.tkt_type_cd
AND t.agt_sid = a.agt_sid
AND t.assigned_id = au_a.asd_user_id
AND t.rcd_crt_user_id = au_c.asd_user_id (+)
AND t.lst_updater_id = au_l.asd_user_id
AND t.tkt_sid = tl.tkt_sid
AND t.prb_area_sid = pa3.prb_area_sid
AND tl.rcd_crt_user_id = au_log.asd_user_id
AND pa3.prb_hier_sid = pa2.prb_area_sid (+)
AND pa2.prb_hier_sid = pa1.prb_area_sid (+)
AND a.mktg_org_cd = mo.mktg_org_cd
AND a.src_sys_cd = mo.src_sys_cd
AND a.src_sys_cd = ss.src_sys_cd
AND a.ofc_id = ofc.agt_cont_nbr (+)
AND a.src_sys_cd = ofc.src_sys_cd (+)
AND a.co_sid = c.co_sid (+)
AND a.agt_sts_cd = ast.agt_sts_cd
AND tl.rcd_lst_user_ts >= :b_start_date
AND t.user_grp_sid = :b_group_id
)
AND (
(TKT_STS_DSC NOT LIKE 'Completed')
OR (
tp.prty_dsc = 'High'
AND ts.tkt_sts_dsc = 'Pending'
AND t.rcd_lst_user_ts < SYSDATE- 2
)
OR (
t.rcd_lst_user_ts < SYSDATE- 3
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'High'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 15
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'Medium'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 28
AND ts.tkt_sts_dsc = 'In Progress'
AND tp.prty_dsc = 'Low'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 7
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'High'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 21
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'Medium'
)
OR (
t.rcd_lst_user_ts < SYSDATE- 43
AND ts.tkt_sts_dsc = 'Pending'
AND tp.prty_dsc = 'Low'
)
)
ORDER BY ASSIGNED_TO,
PRTY_DSC
Don't use the old FROM TableA,TableB WHERE ... join syntax. Just don't.
Instead, write out your joins individually:
FROM TableA
INNER JOIN TableB ON ...
LEFT JOIN TableC ON ...
This isn't just a general rant against the old syntax: using the new (I say "new", but it's more than 20 years old now), standard syntax will fix your problem in this case.

powershell to read multiple lines of sql

I currently have a powershell script that looks like this:
$Query = "SELECT
t1.BSM_NM
,t1.D_DTM AS MAXDATETIME
,t1.TECHNOLOGY
,t1.VOICEDATA
,CASE
WHEN t1.VOICEDATA = 'VOICE'
AND
(
t1.CUST_BLK_CNT/t1.ATT_CNT >= t2.MAJOR_VOICE_BLOCK AND t1.CUST_BLK_CNT/t1.ATT_CNT < t2.CRITICAL_VOICE_BLOCK
OR
t1.DRP_CALL_CNT/t1.ATT_CNT >= t2.MAJOR_VOICE_DROP AND t1.DRP_CALL_CNT/t1.ATT_CNT < t2.CRITICAL_VOICE_DROP
OR
t1.AXS_F_CNT/t1.ATT_CNT >= t2.MAJOR_VOICE_AXSFAIL AND t1.AXS_F_CNT/t1.ATT_CNT < t2.CRITICAL_VOICE_AXSFAIL
)
THEN 1
WHEN t1.VOICEDATA = 'DATA'
AND
(
t1.CUST_BLK_CNT/t1.ATT_CNT >= t2.MAJOR_VOICE_BLOCK AND t1.CUST_BLK_CNT/t1.ATT_CNT < t2.CRITICAL_DATA_BLOCK
OR
t1.DRP_CALL_CNT/t1.ATT_CNT >= t2.MAJOR_VOICE_DROP AND t1.DRP_CALL_CNT/t1.ATT_CNT < t2.CRITICAL_DATA_DROP
OR
t1.AXS_F_CNT/t1.ATT_CNT >= t2.MAJOR_VOICE_AXSFAIL AND t1.AXS_F_CNT/t1.ATT_CNT < t2.CRITICAL_DATA_AXSFAIL
)
THEN 1
WHEN t1.VOICEDATA = 'VOICE'
AND
(
t1.CUST_BLK_CNT/t1.ATT_CNT >= t2.CRITICAL_VOICE_BLOCK
OR
t1.DRP_CALL_CNT/t1.ATT_CNT >= t2.CRITICAL_VOICE_DROP
OR
t1.AXS_F_CNT/t1.ATT_CNT >= t2.CRITICAL_VOICE_AXSFAIL
)
THEN 2
WHEN t1.VOICEDATA = 'DATA'
AND
(
t1.CUST_BLK_CNT/t1.ATT_CNT >= t2.CRITICAL_DATA_BLOCK
OR
t1.DRP_CALL_CNT/t1.ATT_CNT >= t2.CRITICAL_DATA_DROP
OR
t1.AXS_F_CNT/t1.ATT_CNT >= t2.CRITICAL_DATA_AXSFAIL
)
THEN 2
ELSE 0
END MAJORCRITICAL
FROM DS3R_FH_ALL_TRIGGER_VIEW t1
INNER JOIN
ZDMSN.DS3R_1XRTT_TRIGGERS_THRESHOLD t2
ON
t1.BSM_NM = t2.BSC_NM
AND
t1.TECHNOLOGY = t2.TECHNOLOGY
WHERE t1.BSM_NM = 'ARL1' and t1.D_DTM = (SELECT MAX(D_DTM) FROM DS3R_FH_1XRTT_BTS_LVL_KPI WHERE BSM_NM = 'ARL1')"
$data_set = new-object system.data.dataset
$adapter = new-object system.data.oracleclient.oracledataadapter ($Query, $Connection)
[void] $adapter.Fill($data_set)
$table = new-object system.data.datatable
$table = $data_set.Tables[0]
$bsmNM = $data_set.Tables[0].Rows[0].BSM_NM
$maxDT = $data_set.Tables[0].Rows[0].MAXDATETIME
$majorC = $data_set.Tables[0].Rows[0].MAJORCRITICAL
$table | Select MAJORCRITICAL
{
if ($majorC -match "*2*" -and $majorC -match "*1*")
if ($majorC -match "*1*" )
if ($majorC -match "*2*")
the results came back as such:
it doesn't error but it doesn't seem to be doing what I want it to do. If there is a value of 1 in any of the rows in the MAJORCRITICAL column, I want it to do the 2nd if statement, if there is a 1 in one row and a 2 in another row, I want it to do the 1st if statement. The code runs, but the if statements aren't running...
You are only looking at the first row in your example, as you are not doing foreach or similar.
Additionally you need to use $_ to refer to the current item in foreach (alias %)

Query Parameters not working

Working on a query here in SSRS and when I use the #Date parameters it doesnt seem to work, I dont get an error but it doesnt return values. When I take the =#Date value out of it, seems to run fine. Any Ideas on how I can correct this? I want to be able to input a name and date.
Thanks
SELECT TOP (100) PERCENT A.__$start_lsn, A.__$seqval, A.__$operation, A.__$update_mask, A.t_Refcntd, A.t_Refcntu, A.t_cbrn, A.t_ccon, A.t_ccor, A.t_ccrs, A.t_ccty, A.t_ccur,
A.t_cdec, A.t_cfrw, A.t_cfsg, A.t_clan, A.t_comp, A.t_corg, A.t_cotp, A.t_cpay, A.t_cplp, A.t_creg, A.t_ctrj, A.t_cvyn, A.t_cwar, A.t_ddat, A.t_ddtc, A.t_egen, A.t_odat,
A.t_odis, A.t_orno, A.t_prno, A.t_pspr, A.t_pstx, A.t_ragr, A.t_ratf, A.t_ratp, A.t_refa, A.t_refb, A.t_suno, A.t_txta, A.t_txtb, A.tran_begin_time, A.tran_end_time,
B.event_time, B.event_time_local, B.sequence_number, B.action_id, B.succeeded, B.permission_bitmask, B.is_column_permission, B.session_id,
B.server_principal_id, B.database_principal_id, B.target_server_principal_id, B.target_database_principal_id, B.object_id, B.class_type,
B.session_server_principal_name, B.server_principal_name, B.server_principal_sid, B.database_principal_name, B.target_server_principal_name,
B.target_server_principal_sid, B.target_database_principal_name, B.server_instance_name, B.database_name, B.schema_name, B.object_name, B.statement,
B.additional_information, B.file_name, B.audit_file_offset, CONVERT(VARCHAR(10), A.tran_end_time, 110) AS GenericDate
FROM dbo.ttdpur040101_CT AS A INNER JOIN
dbo.ttdpur040101_Audit AS B ON NOT (A.tran_begin_time > B.event_time_local OR
A.tran_end_time < B.event_time_local) AND (A.__$operation = 2 AND B.action_id = 'IN' OR
(A.__$operation = 3 OR
A.__$operation = 4) AND B.action_id = 'UP' OR
A.__$operation = 1 AND B.action_id = 'DL') AND B.class_type = 'U'
WHERE (B.server_principal_name = #Name) and (CONVERT(VARCHAR(10), A.tran_end_time, 110) = #Date)
ORDER BY B.event_time_local
If you are going to compare them, the formats need to match. Change your WHERE to:
WHERE (B.server_principal_name = #Name)
and (CONVERT(VARCHAR(10), A.tran_end_time, 110) = (CONVERT(VARCHAR(10),#Date, 110))