Dynamic raw query (select clause) Django - sql

I'm trying to execute a raw query in Django where I dynamically want to pick column names.
Eg
def func(all=True){
if all:
query_parameters = {
'col': '*'
}
else:
query_parameters = {
'col': 'a,b,c'
}
with connections["redshift"].cursor() as cursor:
cursor.execute(
"select %(col)s from table limit 2 ;",
query_parameters,
)
val = dictfetchall(cursor)
return val
}
Django is executing it like.
select "*" from table limit 2;
so the output is just like select "*"
*
and in the else case it is executed like
select "a,b,c" from table limit 2;
so the output is a,b,c
How can I run the command so that Django run it like
select a , b , c from table limit 2
so that the output is
a b c
1 2 3
4 5 6

I found a hack myself.
See Here
Explanation
Prepared query step by step
Input data (Columns I need)
self.export_col = “a,b,c”
def calc_col(self):
if self.exp == 'T':
select_col = ""
self.export_col = self.export_col.split(',')
for col in self.export_col:
select_col += col + ","
select_col = select_col[:-1]
self.export_col = select_col
else:
self.export_col += '*'
def prepare_query(self):
query += " SELECT "
query += self.export_col
query += """ from table limit 2;"""

Related

SELECT a loop over an Array type. BigQuery

Is there any way to loop over an array column within SELECT clause in Google's BigQuery?. Using pseudo-sql:
-- This is some arbitrary complex logic which can be resume as: sum some product while some accumulator is less than a value
accumulator1 = 0
accumulator2 = 0
user_defined_loop =
FOR (x, y) in array
accumulator1 += x * y
accumulator2 += y
IF accumulator2 <= 5000
CONTINUE
ELSE
BREAK
END IF
RETURN accumulator2
END;
WITH example AS (
SELECT [(1,20), (3,40), (5,60)] as array_type_column
UNION ALL
SELECT [(7,80), (9,100), (5,110)]
UNION ALL
SELECT [(5,7), (30,40), (50,60)]
)
SELECT user_defined_loop(array_type_column)
FROM example
Thanks in advance
Try JavaScrpt UDFs:
CREATE TEMP FUNCTION user_defined_loop(arr ARRAY<STRUCT<x INT64, y INT64>>)
RETURNS INT64
LANGUAGE js
AS """
var accumulator1 = 0
var accumulator2 = 0
for (const item of arr)
{
accumulator1 += parseInt(item.x) * parseInt(item.y)
accumulator2 += parseInt(item.y)
if (accumulator2 > 5000) break
}
return accumulator2;
""";
WITH example AS (
SELECT [(1,20), (3,40), (5,60)] as array_type_column
UNION ALL
SELECT [(7,80), (9,100), (5,110)]
UNION ALL
SELECT [(5,7), (30,40), (50,60)]
)
SELECT user_defined_loop(array_type_column)
FROM example

How to Update with subquery in PostgreSQL

I have a function in MS SQL Server just like this:
UPDATE r
SET
monthly =
(
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = #type_rel_aux
AND h.hcar_day > #date_month_before
AND h.hcar_day <= #date_base
)
FROM #Month_Table r
WHERE type = 1;
and thats the result (after update):
Seq monthly
2 102471,34
1 -5129,46
3 -29841,23
4 0
But when I execute the same update in a fuction in PostgreSQL, all the rows get the same value:
UPDATE Month_Table
SET variacao_mes_rs = (
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = v_type_rel_aux
AND h.hcar_day > v_date_month_before
AND h.hcar_day <= v_date_base) FROM Month_Table r WHERE type = 1;
Result (after update), all the same value of Seq 3:]
Seq monthly
1 -29841,23
2 -29841,23
3 -29841,23
4 -29841,23
I don't see the cause of the problem...
Does PostgreSQL have different rules on UPDATE?
Can anyone help me?
Remove the FROM clause from Postgres:
UPDATE Month_Table r
SET variacao_mes_rs = (
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = v_type_rel_aux
AND h.hcar_day > v_date_month_before
AND h.hcar_day <= v_date_base)
WHERE type = 1;
The FROM clause in an UPDATE behaves differently in the two databases, as you have discovered.

The value is truncated in JSON object when the '-' is contained in the string value

I want to get the value from database using a SQL query (by sampleId which contains '-' sign), however, the query string is truncated.
Here is some of my script where the sampleId is fetched from the last API call:
* json result = response[0].result
* print result
* def personId = result[0].personid
* def sampleId = result[0].sampleid
Given path 'srehr/SendSample'
* def config = read('classpath:utils/yntestDBConfig.json')
* def DbUtils = Java.type('utils.DBUtils')
* def db = new DbUtils(config)
* def foo = {getBatchIDSQL: '#("select operatetime from sr_sendreceive_sample where sampleid = " + sampleId)'}
* print foo.getBatchIDSQL
Here is the specified log in the report:
09:00:06.130 [print] select operatetime from sr_sendreceive_sample where sampleid = 1cfacfa4-eb06-4413-b060-9507bdebd1eb
mainFlow.feature:72 - javascript evaluation failed: db.readValue(foo.getBatchIDSQL), StatementCallback; bad SQL grammar [select operatetime from sr_sendreceive_sample where sampleid = 1cfacfa4-eb06-4413-b060-9507bdebd1eb]; nested exception is java.sql.SQLSyntaxErrorException: Unknown column '1cfacfa4' in 'where clause'
My question: from the log, you see that unknown column '1cfacfa4' is not expected, it should be '1cfacfa4-eb06-4413-b060-9507bdebd1eb'
Try having the strings in SQL within quotes:
* def sql = "select operatetime from sr_sendreceive_sample where sampleid = '" + sampleId + "'"

Using IF Statement inside a Where Clause in SQL Server

I have two params that are passed in a function which passed down to the SQL string. Variables can be null or has a value (int). If x is not null, use "this" column else use "that" column. I'm using SQL Server.
// Inside a function with two variables passed, x and y
$sql = "
SELECT
[...]
FROM
[...]
WHERE
[...]
AND [...]
-- This is the tricky part
AND
--- if $x is not null, use foo column else use bar column
IF (x, R.column = 2, R.another_column = 3)
[...]
";
Is this possible to select a column based on the value of the variable passed in?
AND
(
($x is not null and R.column = 2) OR
($x is null and R.another_column = 3)
)
Unless I'm misunderstanding, you just need logic, or a case statement would work.
$sql = "
SELECT
[...]
FROM
[...]
WHERE
[...]
AND
[...]
-- This is the tricky part
AND
--- if $x is not null, use foo column else use bar column
-- IF (x, R.column = 2, R.another_column = 3)
(
(X IS NULL AND R.column = 2)
OR
(X IS NOT NULL AND R.another_column = 3)
)
";
AND
--- if $x is not null, use foo column else use bar column
case
when x is NULL then R.another_column = 3
else R.column = 2
end
--in SQL Server...
DECLARE #x INT = 3;
SELECT *
FROM sys.schemas
WHERE (#x IS NULL AND schema_id = 2)
OR (#x IS NOT NULL and schema_id = 3)
--optional depending on how much you execute this query, it may help
OPTION(RECOMPILE)

How can I build a complex where clause in Subsonic 2.0

I'm trying to build a query to use for a search engine, that would look like this one:
SELECT * FROM sometable
WHERE col1 = 1
AND col2 = 2
AND (col3a = 3 OR col3b = 3 OR col3c = 3)
I though the code below would work:
SubSonic.Query query = new SubSonic.Query("sometable");
query = query.WHERE("col1", 1);
query = query.WHERE("col2", 2);
query = query.AND("col3a = " + 3).
OR("col3b = " + 3).
OR("col3c = " + 3);
but it doesn't as it results in something like this:
SELECT * FROM sometable
WHERE col1 = 1
AND col2 = 2
AND col3a = 3
OR col3b = 3
OR col3c = 3
How can I build a query I need?
The following should be pretty close to what you want, if OpenExpression/CloseExpression is supported in 2.0:
SubSonic.Query query = new SubSonic.Query("sometable");
.WHERE("col1", 1);
.AND("col2", 2);
.AND("col3a = " + 3).
.OpenExpression()
.OR("col3b = " + 3).
.OR("col3c = " + 3);
.CloseExpression()
I think you are meant to use WhereExpression/AndExpression/OrExpression to nest expressions reading the documentation but Ive never used it so cant say for sure. Try the below and see if it works
SubSonic.Query query = new SubSonic.Query("sometable");
query = query.WHEREEXPRESSION("col1", 1);
query = query.AND("col2 = " + 2);
query = query.ANDEXPRESSION("col3a = " + 3).
OR("col3b = " + 3).
OR("col3c = " + 3);
You can create two one query first that include all OR's after that from the resulting view..you can filter out AND condition..I dint check it out..but hope it will work...
I don't know SubSonic, but would this work?
query = query.AND("col3a = " + 3 + " OR col3b = " + 3 + " OR col3c = " + 3);
You could easily build that substring programmatically.
I see it's not as simple as I expected, at least not in the version I have so I decided to loop through the DataSet manually to filter out the records from the last check.
Unless there is a better way?
I would recommend you to upgrade to SubSonic 2.2... The new query functions added in 2.1 are much more powerful.