NULLIF how to set it properly in this query - db2

I am using the following query to get some tablespaces usage at a glance:
db2 "select substr(tbsp_name,1,30) as Tablespace_Name, tbsp_type as Type, substr(tbsp_state,1,20) as Status, (tbsp_total_size_kb / 1024 ) as Size_Meg, smallint((float(tbsp_free_size_kb)/ float(tbsp_total_size_kb))*100) as Percent_Free_Space, int((tbsp_free_size_kb) / 1024 )as Meg_Free_Space from sysibmadm.tbsp_utilization where smallint((float(tbsp_free_size_kb)/ float(tbsp_total_size_kb))*100) < 20 order by Percent_Free_Space"
however, I'm stuck with the following error:
SQL0801N Division by zero was attempted. SQLSTATE=22012
I understand you can fix this error with a NULLIF however I can't find the correct way to set in in the query, thanks for the help.
( using "DB2 v9.7.0.11", "s150922", "IP23937", and Fix Pack
"11")

Maybe you should use case:
CASE WHEN tbsp_total_size_kb=0 THEN NULL ELSE (tbsp_total_size_kb / 1024 ) END as Size_Meg
if tbsp_total_size_kb can be null IFNULL like this:
CASE WHEN IFNULL(tbsp_total_size_kb,0)=0 THEN NULL ELSE (tbsp_total_size_kb / 1024 ) END as Size_Meg

Try this:
smallint(float(tbsp_free_size_kb) / float(nullif(tbsp_total_size_kb,
0)) * 100)

Related

mismatched input 'as'. Expecting: ',', <expression>

My query in PRESTO returns this error
Query failed (#20220506_153121_03035_vycq3): line 6:41: mismatched
input 'as'. Expecting: ',',
I don't know why, anybody could find the issue?
select
droh.created_by as deliverymen_email,
count(distinct o.order_number) as deliveries,
sum(case when o.last_status = 10 then 1 else 0 end) quantity_canceled,
cast(sum(quantity_canceled as decimal))/cast(count(deliveries as decimal)) as
delivery_cancellation_fee
from sensitive_raw_courier_api.deliveryman_route_order_history droh
left join raw_courier_api."order" o
on droh.order_number = o.order_number and droh.state = 'DM_PICKED_UP'
where 1=1
and o.created_date >= {{date_begin}}
and droh.created_at >= {{date_end}}
and o.customer_email = {{costumer_email}}
group by 1
order by 2 desc
There is an error with the position of 2 brackets.
We count(~) first and then cast( count(~) ) the result.
cast(sum(quantity_canceled as decimal))/cast(count(deliveries as decimal)) as
should be
cast(sum(quantity_canceled) as decimal)/cast(count(deliveries) as decimal) as
Without the context and further information, it's not sure if this is your only issue in this query, but you can't "mix" a cast with a sum or a count like you do. You need to do the cast first and then sum or count the values (or vice versa). So as example this syntax in your query is incorrect:
CAST(SUM(quantity_canceled AS DECIMAL))
It should be this one instead:
SUM(CAST(quantity_canceled AS DECIMAL))
Or this one:
CAST(SUM(quantity_canceled) AS DECIMAL)
You must fix all occurences of this mistake and then check if the query is correct or contains further problems.
A last note: Doing a division could always end in a division by zero exception if you don't prevent this. So you should take care of this.

How to use st_Line_Locate_Point() with a MULTILINESTRING convertion in PostGIS?

I am trying to get the index position of a POINT in a MULTILINESTRING.
Here is the whole query I'm stuck with :
SELECT req.id, (dp).geom, netgeo_point_tech.id, ST_Line_Locate_Point(st_lineMerge(geom_cable), (dp).geom)
FROM (SELECT id, ST_DumpPoints(geom) as dp, geom as geom_cable FROM netgeo_cable_test ) as req
JOIN netgeo_point_tech ON ST_dwithin(netgeo_point_tech.geom, (dp).geom, 1)
ORDER BY req.id, (dp).path [ 1] ASC
The error I get is : line_locate_point : 1st arg isnt a line.
The error is due to the return of st_lineMerge() function that is returning LINESTRING but also MULTILINESTRING.
I don't get this. st_lineMerge() is supposed to return only LINESTRING.ST_LineMerge()
When I jsut try a simple query like this :
select st_astext(st_linemerge(geom)) from netgeo_cable_test
The output is :
)
I want to learn from this, so, if possible, explain to me what I'm doing wrong here, or if my approach is lacking insight.
Thanks to #JGH for the suggestion to use ST_Dump I came up with this function:
create or replace function MultiLineLocatePoint(line geometry, point geometry) returns numeric as $$
select (base + extra) / ST_Length(line)
from (
select
sum(ST_Length(l.geom)) over (order by l.path) - ST_Length(l.geom) base,
ST_LineLocatePoint(l.geom, point) * ST_Length(l.geom) extra,
ST_Distance(l.geom, point) dist
from ST_Dump(line) l
) points
order by dist
limit 1;
$$ language SQL;

Correct My DB2 Query that I am Getting Error

I am using below DB2 query to get the Specific Length of Character
from CLASSIFICATION Coulmn with Condition 0 before the '\', but I am getting error.
select SERVICE_REQUEST,
(case when
(SUBSTR(CLASSIFICATION,LOCATE('\',CLASSIFICATION)-2,1))='0' then
RIGHT(CLASSIFICATION,LENGTH(CLASSIFICATION)-LOCATE('\',CLASSIFICATION))
ELSE CLASSIFICATION
end)
as CLASSIFICATION2
from REPORTDB3.MAXIMO_INDIA_SR_MONTHLY_REPORT where length(SERVICE_REQUEST)=7
ERROR:
An error occurred while processing the results. -
The statement was not executed because a numeric argument of a scalar function is out of range.. SQLCODE=-138, SQLSTATE=22011, DRIVER=4.19.56
Can you please Help me on this.
Regards,
Sambit
Already Gone to Stackoverflow for the answers
I believe you should care about the situations, when the CLASSIFICATION column data have the following problems:
- No \ characters at all
- \ character in the 1-st 2 positions
Your query passes negative 2-nd parameter to the SUBSTR function in these cases, which is not allowed.
with REPORTDB3_MAXIMO_INDIA_SR_MONTHLY_REPORT(CLASSIFICATION, SERVICE_REQUEST) as (
values
('ab0c|def', '1234567')
, ('\def', '1234567')
, ('a\def', '1234567')
, ('ab0c\def', '1234567')
)
select SERVICE_REQUEST, CLASSIFICATION,
case SUBSTR(CLASSIFICATION, case when pos>2 then pos end -2, 1)
when '0' then SUBSTR(CLASSIFICATION, pos+1)
else CLASSIFICATION
end as CLASSIFICATION2
from (
select SERVICE_REQUEST, CLASSIFICATION, LOCATE('\', CLASSIFICATION) as pos
from REPORTDB3_MAXIMO_INDIA_SR_MONTHLY_REPORT
where length(SERVICE_REQUEST)=7
)

How to use CASE clause (DB2) to display values from a different table?

I'm working in a bank so I had to adjust the column names and information in the query to fit the external web, so if there're any weird mistakes know it is somewhat fine.
I'm trying to use the CASE clause to display data from a different table, I know this is a workaround but due to certain circumstances I'm obligated to use it, plus it is becoming interesting to figure out if there's an actual solution.
The error I'm receiving for the following query is:
"ERROR [21000] [IBM][CLI Driver][DB2] SQL0811N The result of a scalar
fullselect, SELECT INTO statement, or VALUES INTO statement is more
than one row."
select bank_num, branch_num, account_num, client_id,
CASE
WHEN exists(
select *
from bank.services BS
where ACCS.client_id= BS.sifrur_lakoach
)
THEN (select username from bank.services BS where BS.client_id = ACCS.client_id)
ELSE 'NONE'
END username_new
from bank.accounts accs
where bank_num = 431 and branch_num = 170
EDIT:
AFAIK we're using DB2 v9.7:
DSN11015 - DB21085I Instance "DB2" uses "64" bits and DB2 code release "SQL09075" with
level identifier "08060107".
Informational tokens are "DB2 v9.7.500.702", "s111017", "IP23287", and Fix Pack "5".
Use listagg function to include all results.
select bank_num, branch_num, account_num, client_id,
CASE
WHEN exists(
select *
from bank.services BS
where ACCS.client_id= BS.sifrur_lakoach
)
THEN (select LISTAGG(username, ', ') from bank.services BS
where BS.client_id = ACCS.client_id)
ELSE 'NONE'
END username_new
from bank.accounts accs
where bank_num = 431 and branch_num = 170

PostgreSQL View Error Handling

The code given below is a part of a view that I have created. But sometimes it may throw an error saying:
division by zero
The reason for this error is that sum(bills.past_arrear) part may be 0 for some months.
SELECT (SELECT revenue_driver.driver_id
FROM ccdb.revenue_driver
WHERE revenue_driver.driver_desc::text = 'Arrear Collection Efficiency'::text) AS driver_id
,bills.org_unit_id::integer AS section_id
,date_part('Month'::text, bills.due_date) AS mnth
,date_part('Year'::text, bills.due_date) AS yr
,ROUND(SUM(COALESCE(bills.arrear_collected,0::numeric))/sum(bills.past_arrear)*100::numeric, 2) AS per_efficiency
,now() AS creation_dt
FROM ccdb.bills
WHERE bills.due_date::date >= date_trunc('Month'::text,'now'::text::date::timestamp with time zone)::date
AND bills.due_date::date <= 'now'::text::date
AND (bills.bill_type_group_code::text = ANY (ARRAY['EB'::character varying::text, 'Energy'::character varying::text]))
GROUP BY bills.org_unit_id, date_part('Year'::text, bills.due_date), date_part('Month'::text, bills.due_date);
What I want is if ROUND(SUM(COALESCE(bills.arrear_collected,0::numeric))/sum(bills.past_arrear)*100::numeric, 2) throws division by zero error I want to replace the value with 0.
I have not idea how do handle this error. Kindly someone help me on this.
You need to use a CASE WHEN in your select statement like below :-
CASE
WHEN sum(bills.past_arrear) = 0
THEN 0
ELSE ROUND(SUM(COALESCE(bills.arrear_collected, 0::NUMERIC)) / sum(bills.past_arrear) * 10 ‌​0::NUMERIC, 2)
END AS per_efficiency