Convert string to tuple in SQL - sql

I'm using python to call a SQL script to run in snowflake. The python script passes a tuple as a string to SQL's where clause, but how can I convert the string back to tuple in SQL?
For example, the SQL query looks like:
where catalog_item_id in '(1180, 3190)' whereas it suppose to be where catalog_item_id in (1180, 3190).
I tried where catalog_item_id in (select cast('(1180, 3190)' as varchar)) and where catalog_item_id in (select replace('(1180, 3190)', '''', '')), none of them works. Can someone help?
Thanks in advance!
Responding to comments, my codes in python:
file = open(fnm, 'r')
product_list = (1180, 3190)
query = file.read().replace('{item_ids}', str(product_list))
file.close()
The sql query looks like
select
catalog_item_id,
score
from ITEM_RECOMMENDATIONS
where catalog_item_id in '{item_ids}'

Using SPLIT_TO_TABLE:
select catalog_item_id,score
from ITEM_RECOMMENDATIONS
where catalog_item_id in (select value from split_to_table(input_param, ','));
where input_param should be provided as: '1180,3190'

Related

Spark SQL column doesn't exist

I am using Spark in databricks for this SQL command.
In the input_data table, I have a string for the st column. Here I want to do some calculations of the string length.
However, after I assign the length_s alias to the first column, I can not call it in the following columns. SQL engine gives out Column 'length_s1' does not exist error message. How can I fix this?
CREATE OR REPLACE VIEW content_data (SELECT LENGTH(st) AS length_s, LENGTH(st)-LENGTH(REGEXP_REPLACE(seq,'[AB]','')) AS AB_c,
length_s - LENGTH(REGEXP_REPLACE(seq, '[CD]', '') AS CD_c, CD_c+AB_c AS sum_c
FROM input_data)
You can't use aliases in the same select
So do
CREATE OR REPLACE VIEW content_data (
SELECT
LENGTH(st) AS length_s
, LENGTH(st)-LENGTH(REGEXP_REPLACE(seq,'[AB]','')) AS AB_c,
LENGTH(st) - LENGTH(REGEXP_REPLACE(seq, '[CD]', '') AS CD_c
, LENGTH(st) - LENGTH(REGEXP_REPLACE(seq, '[CD]', '') + LENGTH(st)-LENGTH(REGEXP_REPLACE(seq,'[AB]','')) AS sum_c
FROM input_data
)

Pass list of dates to SQL WHERE statement in PySpark

In the process of converting some SAS code to PySpark and we previously used a macro variable for the where statement in this code. In adapting to PySpark, I'm trying to pass a list of dates to the where statement, but I keep getting errors. I want the SQL code to pull all data from those 3 months. Any pointers?
month_list = ['202107', '202108', '202109']
sql_query = """ (SELECT *
FROM Table_Blah
WHERE (to_char(DateVariable,'yyyymm') IN '{}')
) as table1""".format(month_list)
Pass the list as a tuple to have the right sql syntax:
month_list = ['202107', '202108', '202109']
sql_query = """ (SELECT *
FROM Table_Blah
WHERE (to_char(DateVariable,'yyyymm') IN {})
) as table1""".format(tuple(month_list))
And you don’t need apostrophe for in statement

How can I count all NULL values, without column names, using SQL?

I'm reading and executing sql queries from file and I need to inspect the result sets to count all the null values across all columns. Because the SQL is read from file, I don't know the column names and thus can't call the columns by name when trying to find the null values.
I think using CTE is the best way to do this, but how can I call the columns when I don't know what the column names are?
WITH query_results AS
(
<sql_read_from_file_here>
)
select count_if(<column_name> is not null) FROM query_results
If you are using Python to read the file of SQL statements, you can do something like this which uses pglast to parse the SQL query to get the columns for you:
import pglast
sql_read_from_file_here = "SELECT 1 foo, 1 bar"
ast = pglast.parse_sql(sql_read_from_file_here)
cols = ast[0]['RawStmt']['stmt']['SelectStmt']['targetList']
sum_stmt = "sum(iff({col} is null,1,0))"
sums = [sum_sql.format(col = col['ResTarget']['name']) for col in cols]
print(f"select {' + '.join(sums)} total_null_count from query_results")
# outputs: select sum(iff(foo is null,1,0)) + sum(iff(bar is null,1,0)) total_null_count from query_results

SQL query customized output

I have below query:
select 'my.MYNAME=' + name from hostnames;
my.MYNAME=abc
my.MYNAME=xyz
my.MYNAME=poi
The query is dynamic is gives3 result, it may give more result depending upon data.
I need following output:
my.MYNAME1=abc
my.MYNAME2=xyz
my.MYNAME3=poi
Numbers appending to MYNAME according to result it gives.
I have tried
select 'my.MYNAME={c}' + name from hostnames where (select count(*) as c from name);
but it is not working.
One way to go about it is:
SELECT CONCAT(CONCAT(CONCAT('my.MYNAME',ROWNUM),'='), name) FROM hostnames
DEMO

How to extract string between two known strings in SQL Result and display it in another Column?

I am using SQL Developer.
Table Name: PRCQA.PRCBUILD_FJ_BAT
Column Name: ETA
Value of 'ETA':
http://reefoats.us.oc.com:8080/atsresultviewer/displayresult?ree_run_id=153464685&type=ree
I am trying to extract the string 153464685 from the above resulting column and display it separately.
I have tried this till now:
SELECT SUBSTR(t.ETA, INSTR(t.ETA, '=')+1, INSTR(t.ETA, 'type')-1), t.ETA AS output
FROM PRCQA.PRCBUILD_FJ_BAT t
WHERE t.DTE_ID = '33782451' AND t.BAT_NAME LIKE '110_170_ECM 140_%'
I am getting the below value as result:
153464685&type=ree
But I want the result as:
153464685
Even if I change the INSTR(t.ETA, 'type')-1, still getting the same result?
What can I do here? Please suggest. Thanks.
Use regexp_substr():
select translate(regexp_substr(eta, '[=][^=&]+[&]', 1, 1), '=&a', 'a')
from PRCQA.PRCBUILD_FJ_BAT t
where t.DTE_ID = '33782451' and t.BAT_NAME like '110_170_ECM 140_%'
This worked:
SELECT SUBSTR(t.ETA, INSTR(t.ETA, '=')+1, (INSTR(t.ETA, 'type')-INSTR(t.ETA, '='))-2), t.ETA AS output
FROM PRCQA.PRCBUILD_FJ_BAT t
WHERE t.DTE_ID = '33782451' AND t.BAT_NAME LIKE '110_170_ECM 140_%'