ANSI SQL Function Dynamic parameter - sql

I am using below query in Azure Synapse Spar SQL notebook. I need to dynamically give function parameter. Is there a way I could achieve this?
select ROUND(
'9.000083',cast(NumDigitsAfterDecimal as INT)),NumDigitsAfterDecimal from (
SELECT
2 NumDigitsAfterDecimal
) a
Thank you so much for the help

Azure Synapse Spark SQL notebook. I need to dynamically give function parameter.
To set and pass parameter in spark SQL you can try below code:
SET NumDigitsAfterDecimal = 2;
select ROUND( '9.18456083',cast(${NumDigitsAfterDecimal} as INT)),${NumDigitsAfterDecimal} as NumDigitsAfterDecimal
--OR
select ROUND( '9.180083',cast(${NumDigitsAfterDecimal} as INT)),NumDigitsAfterDecimal from ( SELECT 2 NumDigitsAfterDecimal) a
Execution and output:

Related

How to change from regexp_extract to regexp_substr in Snowflake

I have some expressions in Hive that I need to change into Snowflake
(regexp_extract(subtransactionxml,'(.*?)()',1) in('REFUND'))
I tried to use this one but it gives me 0 results
(regexp_substr(subtransactionxml,'\W+(\W+)',1,1,'e',1) in('REFUND'))
Where is my mistake?
Based on your sample, the following query should work:
with xmldata as (
select '<transactionDate>2019-07-26T14:06:05.575-04:00</transactionDate> <type>CANCEL</type>' subtransactionxml )
select regexp_substr(subtransactionxml,'<type>(.*)<\/type>',1,1,'e',1) in ('CANCEL')
from xmldata;

Optimizing multiple identical operator and function calls in Hive?

I'm new to Hive and trying to optimize a query that is taking a while to run. I have identical calls to regexp_extract and get_json in my SELECT and WHERE statements, and I was wondering if there is a way to optimize this by storing the results from one statement and using them in the other (or if Hive is already doing something like this in the background).
Example query:
SELECT
regexp_extract(get_json(json, 'url'), '.*[&?]q=([^&]*)') as query
FROM
api_request_logs
WHERE
LENGTH(regexp_extract(get_json(json, 'url'), '.*[&?]q=([^&]*)')) > 0
Thanks!
You can use a derived table to specify the regex only once but I don't think it runs faster
select * from (
select regexp_extract(get_json(json, 'url'), '.*[&?]q=([^&]*)') as query
from api_request_logs
) t where length(query) > 0

How to implement width_bucket function in a single query

Oracle SQL provides
width_bucket(expression,min_value,max_value,num_buckets)
function to create a histogram. WIDTH_BUCKET Oracle SQL Reference. I want to know if the same functionality can be achieved using a nested query or something ?
Update: If it is not possible through a single query, I would like to know which of the following methods to implement the histogram function would be fastest in performance?
SQL PL stored procedure
JAVA stored procedure
JDBC program
WIDTH_BUCKET ( e, m, M, p ) is an ISO SQL function that can be convert in a simple mathematic formulae...
The strict equivalent in "plain" SQL is :
CASE
WHEN e < m
THEN 0
ELSE 1 + FLOOR((e - m ) / ((M - m)/p))
END

Pivot Command fails on Multi Parameter CLR Aggregate Function... ideas why?

I have written a couple of AGGREGATE functions in CLR
abj.median
abj.percentile
A bit of an interesting issue. The functions, in structure , are very similar other than a small difference in the way the results are calculated AND, PERCENTILE requires 2 parameters, while median only one.
The common parameter on both functions is the field name. The percentile function also carries a value to determine which percentile (10, 75, 90 etc.....)
This command works fine.....
;
WITH p1 AS (
SELECT WAITTIMES_DAY / 7.0 AS waitWeeks,
abj.fyq(surg_sx_date) as fiscalYear,
SURG_SITE_ZONE
FROM dbo.Surgery
)
SELECT *
FROM p1 p
PIVOT (abj.median(waitweeks)
FOR fiscalYear IN ( [2013/14-Q1], [2013/14-Q2], [2013/14-Q3], [2013/14-Q4] )) b
This command fails with INCORRECT SYNTAX NEAR '90'. Expecting '.', ID, or QUOTED_ID.
;
WITH p1 AS (
SELECT WAITTIMES_DAY / 7.0 AS waitWeeks,
abj.fyq(surg_sx_date) as fiscalYear,
SURG_SITE_ZONE
FROM dbo.Surgery
)
SELECT *
FROM p1 p
PIVOT (abj.percentile(waitweeks,90)
FOR fiscalYear IN ( [2013/14-Q1], [2013/14-Q2], [2013/14-Q3], [2013/14-Q4] )) b
Has anybody ran into this Wierdness before, and how did they fix (other than to breakdown and write the PERCENTILE function with only one parameter, with the second changed to default of 90)
Thanks
Sven
When you use CLR functions you use them as is. If function abj.median was written to use only one parameter. It will works like this and only like this. If you want that median was able to use 2 parameters you need ask developer of this function rewrite it for you.

Binary mask oracle

I have a SQL command in SQL-Server, and I need to migrate it to Oracle, but there's a part of the sql that I don't understand how to translate it. Here's the sql:
select * from myTable where id = #id and (Mask & #Mask) = #Mask
I think that It's a binary mask, but I'm not quite sure and I don't know if I can do that in Oracle, could you help me?
Thank you very much
You are correct - this is binary mask.
Oracle provides only BITAND function, others (BITOR, BITXOR) must be self made.
Using BITAND function this select will return value 2:
SELECT BITAND(6,2) FROM DUAL;
So your query in Oracle can be rewritten in following way:
select * from myTable where id = :id and BITAND(Mask, :Mask) = :Mask