Informix divide by zero error - sql

I have a query which is erroring out with a divide by zero error. I have isolated the part of the query which errors and tried to fix it using an NVL but it's just not going well, can someone spot my error?
select warehouse.ware_code,
warehouse.desc_text,
maingrp.maingrp_code,
maingrp.desc_text,
prodgrp.prodgrp_code,
prodgrp.desc_text,
left(product.part_code,7) Parent_Product_code,
product.part_code,
product.desc_text,
sum(salesstat.order_qty - salesstat.return_qty) Total_Sales,
sum((salesstat.order_qty - salesstat.return_qty) / 6) Average_Weekly_Sales,
sum(((salesstat.order_qty - salesstat.return_qty) / 6) * 4) Average_Monthly_Sales,
'??' Note,
sum(prodstatus.onhand_qty) Stock_On_Hand,
sum(orderstat.order_qty - orderstat.cancel_qty) Outstanding_orders,
sum(prodstatus.onhand_qty - (orderstat.order_qty - orderstat.cancel_qty)) Stock_Available,
--CASE
--WHEN nvl(sum(salesstat.order_qty - salesstat.return_qty),0) = 0
-- THEN 999
--ELSE sum(prodstatus.onhand_qty / ((salesstat.order_qty - salesstat.return_qty) / 6))
--END as Weeks_Stock_Remaining,
'??' Stock_ordered,
'??' Production_date
from
warehouse,
warereptdetl,
maingrp,
prodgrp,
product,
prodstatus,
salesstat,
orderstat,
Statint
where warehouse.cmpy_code = maingrp.cmpy_code
and warehouse.cmpy_code = warereptdetl.cmpy_code
and warehouse.cmpy_code = prodgrp.cmpy_code
and warehouse.cmpy_code = product.cmpy_code
and warehouse.cmpy_code = salesstat.cmpy_code
and warehouse.cmpy_code = prodstatus.cmpy_code
and warehouse.cmpy_code = orderstat.cmpy_code
and warehouse.cmpy_code = statint.cmpy_code
and product.ware_code = prodstatus.ware_code
and product.ware_code = warehouse.ware_code
and product.ware_code = salesstat.ware_code
and product.ware_code = orderstat.ware_code
and product.ware_code = warereptdetl.ware_code
and product.part_code = salesstat.part_code
and product.part_code = orderstat.part_code
and product.part_code = prodstatus.part_code
and product.prodgrp_code = salesstat.prodgrp_code
and product.prodgrp_code = orderstat.prodgrp_code
and product.prodgrp_code = prodgrp.prodgrp_code
and product.maingrp_code = salesstat.maingrp_code
and product.maingrp_code = orderstat.maingrp_code
and product.maingrp_code = maingrp.maingrp_code
and salesstat.year_num = statint.year_num
and salesstat.int_num = statint.int_num
and salesstat.stat_type_code = statint.type_code
and orderstat.year_num = statint.year_num
and orderstat.int_num = statint.int_num
and orderstat.stat_type_code = statint.type_code
and warehouse.cmpy_code = 'AB'
and warereptdetl.warereptgrp_code = 'NSW'
and product.ware_code = 'HP1'
and product.status_ind <> 3
and left(product.part_code,2) = '21'
and statint.year_num = 2016
and statint.int_num between 29 AND 34
--and statint.int_num = 29
and statint.type_code = 'WLY'
Group by 1,2,3,4,5,6,7,8,9
The commented out part is where I have tried to put in a fix, if I comment it out the query runs fine (albeit a bit slow).
The second part of the question is something that you may not be able to answer without having access to my database but if I run the query for 1, 2 or even 3 weeks (statint.int_num) the query comes back in under 1 minute. as soon as I push it out to 6 weeks (which is what I need) the query takes just over 40 minutes to return the results.
I can't see anywhere in the query where I have created a cartesian but I have stared at it for so long I may be missing it.

In your case statement:
sum(salesstat.order_qty - salesstat.return_qty)
may be non zero, but the individual
salesstat.order_qty - salesstat.return_qty
which is what you're dividing by, may contain zeroes.
EDIT
To clarify,
sum(prodstatus.onhand_qty / ((salesstat.order_qty - salesstat.return_qty) / 6))
may have zeroes in the denomiator because it is a sum of several fractions. Just because all of the denominators sum up to a non-zero number doesn't mean that all the denominators are non-zero.

Related

Check if two lines are intersecting? (if so, not where so)

I saw this asked but I couldn't understand the answers!
I got 4 vector2s, P1 & P2 for line 1, P3 & P4 for line 2.
Code for intersection position works, but how do I check if that intersection is happening?
More specifically, I want to check what side of a polygon an imaginary line is passing through/colliding with
...
...Had something like it working in an old test-script, however I made no annotations and I can't adapt it. I don't know if there's anything here that could be used but thought I'd share:
if rotation_angle > PI/2 && rotation_angle < 3*PI/2:
if rad_overflow(($Position2D.position-position).angle()-PI/2) < rad_overflow(rotation_angle-PI/2) or rad_overflow(($Position2D.position-position).angle()-PI/2) > rad_overflow(rotation_angle+PI/2):
actives.x = 1
else:
actives.x = 0
if rad_overflow(($Position2D2.position-position).angle()-PI/2) < rad_overflow(rotation_angle-PI/2) or rad_overflow(($Position2D2.position-position).angle()-PI/2) > rad_overflow(rotation_angle+PI/2):
actives.y = 1
else:
actives.y = 0
else:
if rad_overflow(($Position2D.position-position).angle()-PI/2) < rad_overflow(rotation_angle-PI/2) && rad_overflow(($Position2D.position-position).angle()-PI/2) > rad_overflow(rotation_angle+PI/2):
actives.x = 1
else:
actives.x = 0
if rad_overflow(($Position2D2.position-position).angle()-PI/2) < rad_overflow(rotation_angle-PI/2) && rad_overflow(($Position2D2.position-position).angle()-PI/2) > rad_overflow(rotation_angle+PI/2):
actives.y = 1
else:
actives.y = 0
var point1 = $Position2D.position
var point2 = $Position2D2.position
var limit3 = Vector2(0,1).rotated(rotation_angle+PI/2)
var limit4 = Vector2(0,1).rotated(rotation_angle-PI/2)
var det = (point1.x - point2.x)*(limit3.y - limit4.y) - (point1.y - point2.y)*(limit3.x - limit4.x)
var new_position = Vector2(
((point1.x*point2.y - point1.y*point2.x) * (limit3.x-limit4.x) - (point1.x-point2.x) * (limit3.x*limit4.y - limit3.y*limit4.x))/det,
((point1.x*point2.y - point1.y*point2.x) * (limit3.y-limit4.y) - (point1.y-point2.y) * (limit3.x*limit4.y - limit3.y*limit4.x))/det)
if actives.x != actives.y:
print("hit")
else:
print("miss")
You ask:
I got 4 vector2s, P1 & P2 for line 1, P3 & P4 for line 2. Code for intersection position works, but how do I check if that intersection is happening?
If you are using Godot, you can use the Geometry class for this, in particular the line_intersects_line_2d method. I quote from the documentation:
Variant line_intersects_line_2d ( Vector2 from_a, Vector2 dir_a, Vector2 from_b, Vector2 dir_b )
Checks if the two lines (from_a, dir_a) and (from_b, dir_b) intersect. If yes, return the point of intersection as Vector2. If no intersection takes place, returns null.
Note: The lines are specified using direction vectors, not end points.
So that gives you both if they intersect (if it returns null they don't intersect) and where (if it does not return null it returns a Vector2 with the position of the intersection).

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()

How to query Oracle grouping?

I have such a problem and I don't know how to solve it, can you help me? t
The query returns a result that is shown on the photo and I want to get it to be shown in one line instead of many based on type of age.
https://imgur.com/a/OA6CBpa
with x as (
select ai.invoice_id, ai.invoice_num, ai.invoice_amount, ai.amount_paid,
trial.entity_id, trial.acctd_amount, trial.entered_amount, trial.gl_date,
aps.amount_remaining, aps.gross_amount, aps.due_date, aps.payment_status_flag,
trial.gl_date - aps.due_date dni_opoznienia
from ap_invoices_all ai,
xla.xla_transaction_entities xte,
(
select nvl (tr.applied_to_entity_id, tr.source_entity_id) entity_id,
tr.source_application_id application_id,
sum (nvl (tr.acctd_unrounded_cr, 0)) - sum (nvl (tr.acctd_unrounded_dr, 0)) acctd_amount,
sum (nvl (tr.entered_unrounded_cr, 0)) - sum (nvl (tr.entered_unrounded_dr, 0)) entered_amount,
max(tr.gl_date) gl_date
from xla.xla_trial_balances tr
where 1=1
and tr.definition_code = 'AP_200_1001'
and tr.source_application_id = 200
and tr.gl_date <= fnd_date.canonical_to_date('2019-12-13') -- Data KG
group by nvl (tr.applied_to_entity_id, tr.source_entity_id),
tr.source_application_id
) trial,
ap_payment_schedules_all aps
where 1=1
and ai.invoice_id = 3568325
and nvl(xte.source_id_int_1, -99) = ai.invoice_id
and xte.ledger_id = 1001
and xte.entity_code = 'AP_INVOICES'
and xte.entity_id = trial.entity_id
and xte.application_id = trial.application_id
and ai.invoice_id = aps.invoice_id
)
select x.invoice_id, x.invoice_num, x.entity_id, x.acctd_amount, x.gl_date,
x.amount_remaining, x.gross_amount, x.due_date, x.payment_status_flag,
x.dni_opoznienia, aapl.days_start, aapl.days_to,
case
when x.dni_opoznienia between aapl.days_start and aapl.days_to then x.acctd_amount
else 0
end przedzial
from x,
ap_aging_periods aap,
ap_aging_period_lines aapl
where 1=1
and aap.period_name = 'TEST 5 okresow'
and aap.aging_period_id = aapl.aging_period_id
Based on your comment I guess you need the below
select * from (select x.invoice_id, x.invoice_num, x.entity_id, x.acctd_amount, x.gl_date,
x.amount_remaining, x.gross_amount, x.due_date, x.payment_status_flag,
x.dni_opoznienia, aapl.days_start, aapl.days_to,
case
when x.dni_opoznienia between aapl.days_start and aapl.days_to then x.acctd_amount
else 0
end przedzial
from x,
ap_aging_periods aap,
ap_aging_period_lines aapl
where 1=1
and aap.period_name = 'TEST 5 okresow'
and aap.aging_period_id = aapl.aging_period_id)
where przedzial > 0;

Tradingview pine editor. Issue with ATR TP/SL coding from entry

I need to code Take profit and Stop loss with Average true range from the past. I have an issue my code is calculating latest ATR and i cannot find a way to lock ATR number from the entry. ATR is calculating after candle close that means while candle is active it doesn't exist. Adding picture where I have marked what I'm looking for and what pine is calculating.
Paint shows what I'm looking for, original pine shows code calculations
//#version=2
strategy("Heiwa",initial_capital=1000,default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, commission_value = 0.15, overlay=true)
//WADARINDICATOR----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sensitivity = input(200, title="Sensitivity")
fastLength=input(20, title="FastEMA Length")
slowLength=input(40, title="SlowEMA Length")
channelLength=input(20, title="BB Channel Length")
mult=input(2.0, title="BB Stdev Multiplier")
deadZone=input(20, title="No trade zone threshold")
calc_macd(source, fastLength, slowLength) =>
fastMA = ema(source, fastLength)
slowMA = ema(source, slowLength)
fastMA - slowMA
calc_BBUpper(source, length, mult) =>
basis = sma(source, length)
dev = mult * stdev(source, length)
basis + dev
calc_BBLower(source, length, mult) =>
basis = sma(source, length)
dev = mult * stdev(source, length)
basis - dev
t1 = (calc_macd(close, fastLength, slowLength) - calc_macd(close[1], fastLength, slowLength))*sensitivity
t2 = (calc_macd(close[2], fastLength, slowLength) - calc_macd(close[3], fastLength, slowLength))*sensitivity
e1 = (calc_BBUpper(close, channelLength, mult) - calc_BBLower(close, channelLength, mult))
//e2 = (calc_BBUpper(close[1], channelLength, mult) - calc_BBLower(close[1], channelLength, mult))
trendUp = (t1 >= 0) ? t1 : 0
trendDown = (t1 < 0) ? (-1*t1) : 0
Waddardtdis = input(50, minval=1)
//WADARINDICATOR----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//ATR---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
atrBandDays = input(15, minval=1, title="Days for ATR", type=integer)
atrBand = atr(atrBandDays)
atrPlus1 = close + atrBand
atrPlus2 = close + atrBand*2
atrPlus3 = close + atrBand*3
atrStop = close - atrBand*2
plot(atrPlus1, color=green)
plot(atrPlus2, color=orange)
plot(atrPlus3, color=red)
plot(atrStop, color=blue)
//ATR---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Heikinashi---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
haTicker = heikinashi(tickerid)
haOpen = security(haTicker, period, open)
haClose = security(haTicker, period, close)
//Heikinashi---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
longCondition = haClose > haOpen and haOpen > haClose[1] and trendUp>e1
exitCondition = haClose < haOpen and trendDown>Waddardtdis
strategy.entry("Long", strategy.long, when=longCondition)
strategy.exit("TP1", "Long", qty_percent = 60, limit=atrPlus1)
strategy.exit("TP2", "Long", qty_percent = 20, limit=atrPlus2)
strategy.exit("TP3", "Long", qty_percent = 20, limit=atrPlus3)
strategy.exit("SL", "Long", stop = atrStop)
strategy.close ( "Long", when = exitCondition)
I'd just preserve the atr value at entry, like that:
entryAtr = entryAtr[1]
if (longCondition)
strategy.entry("Long", strategy.long)
entryAtr := atrBand // save the value to use it for exit

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
)