Need to show as loss or gain as a separate column based on the output in sql oracle - sql

I need to show like loss/gain for a column based on the amount
If the result amount is -10000 then I need to show as loss , if its 10000 means gain in sql query..
Here is my query if the output is negative then i should display the amount and one more column as loss and if the output is positive then i should display the amount and one more column as gain
select ((NVL (asset_cost, 0) - NVL (accumalated_dep, 0))- NVL (closed_lability, 0))
From Asset_management

just wrap in a sub query or CTE to avoid typing it out again:
select value,case when value =-10000 then 'loss' case when value =10000 then 'gain' else '??' end
from
(
select ((NVL (asset_cost, 0) - NVL (accumalated_dep, 0))- NVL (closed_lability, 0)) as value
From Asset_management
) as calc
or
with
(
select ((NVL (asset_cost, 0) - NVL (accumalated_dep, 0))- NVL (closed_lability, 0)) as value
From Asset_management
) as calc
select value,case when value =-10000 then 'loss' case when value =10000 then 'gain' else '??' end
(approximate syntax due to no usable scripts)

Related

How do I set an upper bound to an INT value within a select statement in SQL?

I'm looking to make part of my query more performant by cutting down on the number of case statements I use. I have a select statement as below currently:
SELECT
ID,
CASE WHEN sum(Value1) > 0 THEN 1 ELSE 0 END as [Value1],
CASE WHEN sum(Value2) > 0 THEN 1 ELSE 0 END as [Value2],
CASE WHEN sum(Value3) > 0 THEN 1 ELSE 0 END as [Value3],
CASE WHEN sum(Value4) > 0 THEN 1 ELSE 0 END as [Value4],
....
FROM table
Essentially I want the query to simply produce a boolean, either a 0 or a 1, but the case statements have tripled the runtime for my query which is less ideal. Is there a way I can force a boolean output or set a maximum value for my select and trim away the cases?
As John Cappelletti has provided, the expected output of limiting an INT value into a Boolean 0/1 output is achieved via the SIGN function. This reduces the runtime for the query significantly compared to the case statement.

SQL - get only numbers including minus value not alphabets and character from a column

I have a column which has numbers(including negative values),alphabets,alphanumeric and single character symbol(like -,&,#).
How to get rid of alphabets,alphanumeric and symbols and get the sum of the column.
I have to use the condition only in select statement not in where condition. Because it should not affect other column results.
This is what I've tried:
SELECT COUNT(*), CASE
WHEN REGEXP_LIKE(REMD_PART_CSN, '^-?[0-9]\d*(\.\d+)?$') THEN SUM(REMD_PART_CSN)
ELSE NULL
END
FROM <TABLE>
How about this?
SELECT
COUNT(*),
SUM(
CASE
WHEN REGEXP_LIKE(REMD_PART_CSN, '^-?\d+(\.\d+)?$')
THEN CAST(REMD_PART_CSN AS NUMBER)
ELSE 0
END
)
FROM yourtable
I.e. for every row, if REMD_PART_CSN looks like a number, then convert it to a number, else use 0 instead. At the end, return the sum of all those values.

How to have 3 conditions to fulfill in CASE WHEN in SQL

I am having difficulty when trying to have 3 conditions to fulfill in CASE WHEN in SQL server. Here is my original script but only return value 0 instead of 1 and 0. My purpose is to check when a customer purchase ITEM_01 with more than or equal to 0.06 AND ITEM_02 with more than or equal to 0.06 THEN return value 1 else 0. Here is my script:
CASE WHEN SUM ( CASE WHEN ITEM_01)='DH' AND (ITEM_02)='DH Classic' THEN NVL
(SALE_OUTLETD.PRE_SALES_QTY * SKU_UOM_CONV.MULTIPLIER,0) + NVL
(SALE_OUTLETD.SALES_QTY * SKU_UOM_CONV.MULTIPLIER, 0) -NVL
(SALE_OUTLETD.FRESH_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) - NVL
(SALE_OUTLETD.OLD_RTN_QTY * SKU_UOM_CONV.MULTIPLIER,0) - NVL
(SALE_OUTLETD.DAMAGED_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) - NVL
(SALE_OUTLETD.WITHDRAWAL_RTN_QTY *SKU_UOM_CONV.MULTIPLIER, 0) ELSE 0 END )
>= 0.06 THEN 1 ELSE 0 END 3_PACK_AND_ABOVE
Please help on this question because I have been stuck for few hours now. Any help would be greatly appreciated!
I am finding your example impossible to read but the basic form of CASE is:
select case WHEN first_boolean THEN first_value
WHEN second_boolean THEN second_value
WHEN third_boolean THEN third_value
...
ELSE default_value
Always try to build things off that model. Nested CASE statements like what you are doing are never fun and tend to be fairly error prone.

divisor is equal to zero in sql need to add some sort of try catch

I have this query that i get errors when data gets loaded to this table and the divisor is zero which is the b. living_units column. I want to know if I can do some sort of try catch or if its zero to show null instead of failing or something that will help not error out?
SELECT a.SERVICE_TYPE_GRP,
a.HSIA_TYPE,
a.STAT_DYN_IND,
a.VIDEO_IND,
a.VOICE_IND,
a.CUST_CNT,
b.LIVING_UNIT_CNT,
a.DT_MODIFIED,
a.CUST_CNT / b.LIVING_UNIT_CNT AS ALLRGN_TK_RT_PCT
FROM ( SELECT lp.SERVICE_TYPE_GRP SERVICE_TYPE_GRP,
lp.HSIA_TYPE HSIA_TYPE,
lp.STAT_DYN_IND STAT_DYN_IND,
lp.VIDEO_IND VIDEO_IND,
lp.VOICE_IND VOICE_IND,
lp.DT_MODIFIED DT_MODIFIED,
SUM (lp.CUST_CNT) CUST_CNT
FROM RPT_SUBSCR_REGION_DTL lp
GROUP BY SERVICE_TYPE_GRP,
HSIA_TYPE,
STAT_DYN_IND,
VIDEO_IND,
VOICE_IND,
DT_MODIFIED) a,
( SELECT DT_MODIFIED, SUM (LIVING_UNIT_CNT) LIVING_UNIT_CNT
FROM RPT_REGION_CUST_DTL
WHERE dt_modified = (SELECT dt_modified
FROM ls_dt_modified
WHERE NAME = 'RPT_REGION_CUST_DTL')
GROUP BY DT_MODIFIED) b
WHERE a.DT_MODIFIED = b.DT_MODIFIED;
The error i get is ORA-01476: divisor is equal to zero.
If you have a complex divisor expression and a NULL result is acceptable, there is an alternative that does not require repeating the divisor expression:
a.CUST_CNT / nullif(b.LIVING_UNIT_CNT, 0)
The NULLIF function returns NULL if the first parameter is equal to the second parameter. In this case it returns NULL if b.LIVING_UNIT_CNT is equal to zero. And anything divided by NULL also becomes NULL. It's a bit of a "trick" maybe, but it saves the expression repetition of the CASE statement.
Try a case statement:
case
when b.LIVING_UNIT_CNT = 0 -- the divisor
then 0 -- a default value
else a.CUST_CNT / b.LIVING_UNIT_CNT
end
ALLRGN_TK_RT_PCT

How to Return a Value using CASE, DECODE, and/or NVL

I'm having a bit of trouble with this. I need a formula that will give me an actual result regardless of whether or not the values are NULL and/or 0. See the following;
SELECT
[...columns...],
(NVL(SUM(table1.qty_rtnd), 0) + NVL(SUM(table1.qty_defective), 0)) / CASE (NVL(table1.sales, 0)) WHEN 0 THEN 1 END AS six_wk_pct_defective,
[...more columns...]
Values in this particular instance:
table1.qty_rtnd = NULL
table1.qty_defective = 7
table1.sales = 560
If the CASE statement is not in this formula and the divisor is 0, Oracle SQL Developer throws an error back to me telling me I cannot divide by zero. That is fine, but when I try to apply a CASE statement to the dividend portion of this formula, the field in the query result is NULL when it should not be (in this particular case, the math makes it that is should be 0.0125).
What am I doing wrong? How can I use CASE, NVL, DECODE or any other functions to fix this issue?
Thanks,
-Ant
UPDATE:
For those looking for an answer. One was provided by someone which is the following;
SELECT (NVL (qty_rtnd, 0) + NVL (qty_defective, 0)) / NVL (NULLIF (sales, 0), 1) FROM table1
How about
SELECT (NVL (qty_rtnd, 0) + NVL (qty_defective, 0)) / NVL (sales, 1) FROM table1
or
SELECT (NVL (qty_rtnd, 0) + NVL (qty_defective, 0)) / NVL (NULLIF (sales, 0), 1) FROM table1
to safeguard sales=0
It's rather hard to understand what you are asking here since you haven't provided us a test case. You haven't told us, for example, what values the various columns have. You told us that, apparently, the desired result is 0.0125 but without any idea what inputs we're supposed to use, we're a bit blind.
If the only problem is your denominator, I suspect that you want the denominator to be
CASE WHEN NVL( table1.sales, 0 ) = 0
THEN 1
ELSE table1.sales
END
If that guess is not correct, please help us out by posting a reproducible test case.