How can reiterate a certain statement in select in oracle - sql

There is a column of text with JSON expressions in it. It is not at all clear how long the JSON array is, and in the example code below I have repeated the phrase up to six times (it can be more than six repetitions). How can I repeat a duplicate (case when) based on the longest array length?
I also want to specify the column names with the variables d_i and a_i (here i is the counter).
Can I use a while or loop? If Yes, HOW?
Note: If in any row, the first value in the JSON expression is not greater than 0, then the length of the JSON array in that row is zero, and this continues until the end of the representation. This means that if the first cell of the JSON array has a value, the second cell may have a value, and if the second cell has no value, then the length of the array is definitely 1.
If this condition occurs, the loop must start again.
I hope I have stated what I mean correctly.
select t.tx_id,
--00
case WHEN t.fee[0]:amount>0 then t.fee[0]:denom end as d_0,
case when t.fee[0]:amount>0 then t.fee[0]:amount/1000000 end as a_0,
--01
case WHEN t.fee[1]:amount>0 then t.fee[1]:denom end as d_1,
case when t.fee[1]:amount>0 then t.fee[1]:amount/1000000 end as a_1,
--02
case WHEN t.fee[2]:amount>0 then t.fee[2]:denom end as d_2,
case when t.fee[2]:amount>0 then t.fee[2]:amount/1000000 end as a_2,
--03
case WHEN t.fee[3]:amount>0 then t.fee[3]:denom end as d_3,
case when t.fee[3]:amount>0 then t.fee[3]:amount/1000000 end as a_3,
--04
case WHEN t.fee[4]:amount>0 then t.fee[4]:denom end as d_4,
case when t.fee[4]:amount>0 then t.fee[4]:amount/1000000 end as a_4,
--05
case WHEN t.fee[5]:amount>0 then t.fee[5]:denom end as d_5,
case when t.fee[5]:amount>0 then t.fee[5]:amount/1000000 end as a_5,
--06
case WHEN t.fee[6]:amount>0 then t.fee[6]:denom end as d_6,
case when t.fee[6]:amount>0 then t.fee[6]:amount/1000000 end as a_6
from terra.transactions t
where t.tx_id not in (select s.tx_id from terra.swaps s) and fee[0].amount>0 limit 1000

Assuming that you have the table:
CREATE TABLE transactions (
tx_id NUMBER PRIMARY KEY,
fee JSON
);
With the data:
INSERT INTO transactions (tx_id, fee) VALUES (
1,
'[{"denom":"ABC","amount":100},{"denom":"DEF","amount":0},{"denom":"GHI","amount":1}]'
);
Then the simplest method is to output the data as rows (and not as columns):
select t.tx_id,
j.*
from terra.transactions t
CROSS JOIN JSON_TABLE(
t.fee,
'$[*]'
COLUMNS
denom VARCHAR2(20) PATH '$.denom',
amount NUMBER PATH '$.amount'
) j
where t.tx_id not in (select s.tx_id from terra.swaps s)
and j.amount>0
Which outputs:
TX_ID
DENOM
AMOUNT
1
ABC
100
1
GHI
1
If you want to dynamically pivot the rows to columns then this is best done in whatever middle-tier application (PHP, C#, Java, Python, etc.) that you are using to access the database. If you want to do it in Oracle then you can look at the answers to this question.
db<>fiddle here

I use flatten table:
with flattenTable as (
SELECT
tx_id,
fee,
b.value as fee_parsed,
b.value:amount as fee_amount,
b.value:denom as fee_denom
FROM terra.transactions, TABLE(FLATTEN(terra.transactions.fee)) b
where tx_id not in (select s.tx_id from terra.swaps s ) and fee_amount>0)
SELECT f.*,
case when f.fee_denom='uusd' then f.fee_amount/1000000 else f.fee_amount/1000000*(select
avg(price_usd)
from terra.oracle_prices o,flattenTable f
where o.CURRENCY = f.fee_denom and o.block_timestamp=CURRENT_DATE) end as Fee_USD
from flattenTable f
limit 100

Related

NULL values not filtered out with WHERE statement

SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE
When I select these columns, I see that the results have some NULL values. They don't seem to be strings. However, when I try to filter the NULL values out with a where statement:
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME = NULL
I don't see any results. What might be the possible causes? I also tried filtering with 'NULL' but that would throw an error since the column type is double.
use this for only want null recode
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME IS NULL
or
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE ISNULL(VOLUME,'') = ''
if you get not null value then use
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE ISNULL(VOLUME,'') <> ''
or
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME IS NOT NULL
a more full answer is NULL is not directly comparable, much like NaN is not comparable in floating point numbers. Both represent the "lack of a value" if you "have not value here" how can you compare it to something.
"There is nobody next to you, what is their name?" it just doesn't make sense.
So to test you ask column IS NULL or column IS NOT NULL or you can use a compact logic expression see Conditional Expressions but some common ones in Snowflake are:
short form
ANSI long
snowflake long
NVL(column,'')
CASE WHEN column IS NOT NULL THEN column ELSE '' END
IFF(column IS NOT NULL, column, '')
NVL2(column,'a','b')
CASE WHEN column IS NOT NULL THEN 'a' ELSE 'b' END
IFF(column IS NOT NULL, 'a', 'b')
ZEROIFNULL(column)
CASE WHEN column IS NOT NULL THEN column ELSE 0 END
IFF(column IS NOT NULL, column, 0)
COALESCE/NVL/IFNULL are all interchangable so I will only show one (expect COALESCE can handle N items which are checked in order)
You can use the where is function or is not function to filter all the null values.

Snowflake if String Pattern replace

I have column which should only contain numbers[0-9], But in some cases we started see alphanumeric[eg:p8eSadfghrc] values in that column.
I wanna write a condition where if the value is not completely numeric{0-9}, i wanna replace it with another value from another column.
Something like this?
update t
set col = <other value>
where regexp_like(col, '[^0-9]');
This updates the data. You could also just do this in a query:
select t.*,
(case when regexp_like(col, '[^0-9]') then <other value> else col end)
from t;
In Snowflake, I would recommend try_to_decimal(). It attempts to convert the number to a decimal value (you control the target precision and scale with arguments) and rturns null if that fails:
select t.*, case when try_to_decimal(mycol) is null then myothercol else mycol end newcol
from mytable
If you want an update statement:
update mytable
set mycol = myothercol
where try_to_decimal(mycol) is null
When given no second and third argument, the number of allowed digits is 38, with 0 decimals (which seems to be what you want).

Hive - joining two tables to find string that like string in reference table

I stumbled in a case where requires to mask data using keyword from other reference table, illustrated below:
1:
Table A contains thousands of keyword and Table B contains 15 millions ++ row for each day processing..
How can I replace data in table B using keyword in table A in new column?
I tried to use join but join can only match when the string exactly the same
Here is my code
select
sourcetype, hourx,minutex,t1.adn,hostname,t1.appsid,t1.taskid,
product_id,
location,
smsIncoming,
case
when smsIncoming regexp keyword = true then keyword
else 'undef' end smsIncoming_replaced
from(
select ... from ...
)t1
left join
(select adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount,max(datex)
from ( select adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount,datex ,last_update,
max(last_update) over (partition by keyword) as last_modified
from sqm_stg.reflex_service_map ) as sub
where last_update = last_modified
group by adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount)t2
on t1.adn=t2.adn and t1.appsid=t2.appsid and t1.taskid=t2.taskid
Need advice :)
Thanks
Use instr(string str, string substr) function: Returns the position of the first occurrence of substr in str. Returns null if either of the arguments are null and returns 0 if substr could not be found in str. Be aware that this is not zero based. The first character in str has index 1.
case
when instr(smsIncoming,keyword) >0 then keyword
else 'undef'
end smsIncoming_replaced

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.

Sorting a varchar column as integer value in Oracle query

I have a column DOOR which is a VARCHAR2 in a Table ADDRESS. I want to sort the column DOOR.
DOOR contains only two digits and no - sign
currently when I use the query
select sname, door, zip from address a order by door
I get the following result:
a
b
1
10
11
2
3
31
But I want the result to look like this:
a
b
1
2
3
10
11
31
I tried to convert DOOT into a numeric value using to_number as
select sname, to_number(door) dnr, zip from address a order by dnr
but it is giving me an error ORA-01722.
You can do this with the logic in the order by:
order by (case when regexp_like(door, '^[0-9]*$') = 0 then 1 else 0 end) desc,
(case when regexp_like(door, '^[0-9]*$') = 0 then door end),
length(door),
door
This first puts the non-numeric values first. The second clauses sorts these alphabetically. The third and fourth are for numbers. By sorting for the length before the value, you will get the numbers in order.
ORA-01722 error coming because of value 'a' ,'b',
Go for custom function which will take varchar and return number to convert , use custom function in order by clause of your query.
CREATE OR REPLACE FUNCTION tonumber (no_str varchar2)
RETURN number IS
num number := 0;
BEGIN
RETURN to_number(no_str);
EXCEPTION -- exception handlers begin A < B < 1 < 2
WHEN value_error THEN -- handles all other errors
dbms_output.put_line('in other exception catch.');
CASE
WHEN ( upper(no_str) = 'B' ) THEN return -1;
WHEN ( upper(no_str) ='A') THEN return -2;
ELSE return -999;
END CASE;
END;
Add when condition as in required. now assumed it can have only A B. for rest it will return default.
(This approach assumes that there aren't any mixed values like "234abc567".)
So, going old school...just 0-pad the strings to the maximum length of the column so that they'll sort properly as characters. But, to get the "non-numeric" values to sort first, nullify non-numeric values, put the NULLs first and padded values after that.
select door
from address
order by case when replace(translate(door, '012345679', '0000000000'), '0', '') is null
then lpad(door, 10, '0') -- value was entirely made of digits (change the 10 to the max width of the column)
else null
end nulls first
, door -- sorting within the group of NULL rows generated in the previous expression.
use the below query
SELECT PUMP_NAME
FROM MASTER.PUMPS
ORDER BY LPAD(PUMP_NAME, 10);