powershell to read multiple lines of sql - 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 %)

Related

Cannot cast result from Sub-Select to numeric

I am selecting a list of IDs as a sub-query in a condition but it says it cannot convert '123,456' to numeric. The problem occurs in the last line. DB is Sybase-SQL-Anywhere.
SELECT
ISNULL(SUM(a.menge), 0) AS menge,
ISNULL(SUM(a.wert), 0) AS wert
FROM admin.p_ws_ix_kontrakte_ernte_auswertung_jensek a
WHERE
(a.KtrErnteJahr = ? OR ? IS NULL)
AND (
(a.KtrDispoKennz >= ? OR ? IS NULL)
AND
(a.KtrDispoKennz <= ? OR ? IS NULL)
)
AND a.artikelstammid IN ((SELECT LIST(artikelstammId) FROM admin.ws_ix_auswertung_cfg_spalten_artikel WHERE columnId = $column))
Remove the LIST():
# replace this:
AND a.artikelstammid IN ((SELECT LIST(artikelstammId) FROM admin.ws_ix_auswertung_cfg_spalten_artikel WHERE columnId = $column))
# with this:
AND a.artikelstammid IN (SELECT artikelstammId FROM admin.ws_ix_auswertung_cfg_spalten_artikel WHERE columnId = $column)
Another option would be an exists/correlated subquery:
# replace this:
AND a.artikelstammid IN ((SELECT LIST(artikelstammId) FROM admin.ws_ix_auswertung_cfg_spalten_artikel WHERE columnId = $column))
# with this:
AND exists (SELECT 1 FROM admin.ws_ix_auswertung_cfg_spalten_artikel b WHERE b.columnId = $column and b.artikelstammId = a.artikelstammid)

Why is "[COUNT (*): 0]" returned instead of just "0" when executing sql query in groovy?

Why is "[COUNT (*): 0]" returned instead of just "0" when executing sql query in groovy? What can I do to return only a number?
def res = sql.firstRow("""SELECT count(*) FROM bd.pas WHERE INN = ?
AND SYSDATE >= RECDATEBEGIN AND SYSDATE < RECDATEEND AND NVL(DLL, 0) = 0
""",['test'])
println(res)
Use an alias in your query:
def res = sql.firstRow("""SELECT count(*) as num FROM bd.pas WHERE INN = ?
AND SYSDATE >= RECDATEBEGIN AND SYSDATE < RECDATEEND AND NVL(DLL, 0) = 0
""",['test'])
and then read the specific field:
println(res.num)
The firstRow() returns a GroovyRowResult which is essentially a Map.
That's why you get [COUNT (*): 0].
To get a number only out of it you can do:
def res = sql.firstRow("""SELECT count(*) FROM bd.pas ...""",['test']).values().first()

IBM Maximo Where Clause

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)

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.