When i use int in where condition it gives me result but when i use string it gives me error.
Gives result: select * from dbname where version = 4
Gives Error: select * from dbname where name = abc
or
select * from dbname where name = 'abc'
What about :
select * from dbname where name = 'abc'
The documentation on InfluxDB query language
Related
I have this statement:
sql.executeUpdate("UPDATE outbound_message set acknowledged = true, acknowledged_date = NOW(), acknowledged_from = '" + userNameToUse + "' where id in(" + msgList + ") and acknowledged = false")
msgList is a string like 2,4,5 containing the ids.
How can I make this as a prepared statement?
I tried:
long[] test = [1,2]
sql.executeUpdate("UPDATE outbound_message set acknowledged = true, acknowledged_date = NOW(), acknowledged_from = :user where id in(:list) and acknowledged = false", [user:userNameToUse, list:test])
I get this exception:
org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = bigint[]
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
How can I perform a IN() with a prepared statement?
I am trying to create a stored procedure in Snowflake that pivot's the data in a derived table. The pivot columns are dynamic in nature. I have found a way to do this through parameter passing. I am trying to do the same without passing any parameters but the code is not working.
My approach has been to apply a loop at the table from which I am extracting the columns into a variable. Then pass this variable in the pivot. The while loop by itself seems to be working fine. The error comes when this variable is being passed to the pivot.
The code I am using:
CREATE OR REPLACE PROCEDURE LOOP_EXMPL_5()
returns varchar
language javascript
as
$$
var column1 = 'qwerty';
var command = `SELECT ATTR_NAME FROM TBL_DIM`;
var stmt = snowflake.createStatement({sqlText: command});
var rs = stmt.execute();
while (rs.next())
{
var column1 = column1.concat(",","'",rs.getColumnValue(1),"'");
}
var column2 = column1
var command_1 = `CREATE OR REPLACE VIEW HIERARCHY_VIEW_2 AS SELECT * FROM (SELECT MSTR.PROD_CODE AS
PROD_CODE,DIM.ATTR_NAME AS ATTR_NAME,MSTR.ATTR_VALUE AS ATTR_VALUE FROM TBL_DIM DIM INNER JOIN
TBL_MSTR MSTR ON DIM.ATTR_KEY=MSTR.ATTR_KEY ) Q
PIVOT (MAX (Q.ATTR_VALUE) FOR Q.ATTR_NAME IN ('${column2}'))
AS P
ORDER BY P.PROD_CODE;`;
var stmt_1 = snowflake.createStatement({sqlText: command_1});
var rs_1 = stmt_1.execute();
return 'success'
$$;
The error I am getting:
Execution error in store procedure LOOP_EXMPL_5: SQL compilation error: syntax error line 2 at position 73 unexpected 'Region'. At Statement.execute, line 16 position 21.
The variable value that is being passed:
qwerty,'Region','Sub-Region','Division','Company-Product','Company-Mfg','Company-Ship From to Customer','Business Unit','Category','Sub-Category','Segment','Sub-Segment','Brand','Aggregate Brand','Sub-Brand','PPG'
I will remove the qwerty part in the SQL somehow.
Here is the working code. Thanks a lot for your help Felipe and Greg.
CREATE OR REPLACE PROCEDURE LOOP_EXMPL_9()
returns varchar
language javascript
as
$$
var column1 = "";
var command = `SELECT ATTR_NAME FROM TBL_DIM`;
var stmt = snowflake.createStatement({sqlText: command});
var rs = stmt.execute();
while (rs.next())
{
if (column1 != "") column1 += ",";
column1 += `'${rs.getColumnValue (1)}'`;
}
var column2 = column1;
var command_1 = `CREATE OR REPLACE VIEW HIERARCHY_VIEW_2 AS SELECT * FROM (SELECT
MSTR.PROD_CODE AS PROD_CODE,DIM.ATTR_NAME AS ATTR_NAME,MSTR.ATTR_VALUE AS ATTR_VALUE
FROM TBL_DIM DIM INNER JOIN TBL_MSTR MSTR ON DIM.ATTR_KEY=MSTR.ATTR_KEY ) Q
PIVOT (MAX (Q.ATTR_VALUE) FOR Q.ATTR_NAME IN (${column2}))
AS P
ORDER BY P.PROD_CODE;`;
var stmt_1 = snowflake.createStatement({sqlText: command_1});
var rs_1 = stmt_1.execute();
return 'success'
$$;
For your objectives, see:
https://medium.com/snowflake/dynamic-pivots-in-sql-with-snowflake-c763933987c
How to pivot on dynamic values in Snowflake
To debug the current problem: column1.concat(",","'",rs.getColumnValue(1),"'"); is not escaping and quoting the column names correctly.
Set the UDF to return that value and then the value of var command_1 so you can debug appropriately.
A good way to escape the columns:
select '\\''
|| listagg(distinct pivot_column, '\\',\\'') within group (order by pivot_column)
|| '\\''
And then use:
for pivot_column in (${col_list}))
You can start with an empty string:
var column1 = "";
You can then concatenate the column list like this:
if (column1 != "") column1 += ",";
column1 += `"${rs.getColumnValue(1)}"`);
The reason you're getting the SQL syntax error is that the column names are in single quotes when they should be in double quotes.
I need to get the last value of each sourceTag. Please let me know if this query is correct.
SELECT * FROM "measurement1" where rig = '338' and ( sourceTag = 't1' or sourceTag =
't2' or sourceTag = 't3' ) group by sourceTag order by time desc limit 1
Almost. Just change SELECT * to SELECT last(*).
Am Geocoding hundreds of thousands of records, while this query is running if the address does not produce a Lat and Long value for a particular row it shows an error "invalid input syntax for integer: "J199" ". So if this line
(geocode_intersection(crashroad,crashreferenceroad,state,city,'',1)
Produces a value like "J199",it has to skip that row. So how to do this?
update nj.condition_3
set (rating,new_address,points) = ( COALESCE((g.geo).rating,-1),pprint_addy((g.geo).addy),st_astext(ST_SnapToGrid((g.geo).geomout, 0.000001)))
-- Replace in limit value if error occurs
FROM (SELECT addid FROM nj.condition_3 WHERE rating IS NULL ORDER BY addid LIMIT 3) As a
LEFT JOIN (SELECT addid, (geocode_intersection(crashroad,crashreferenceroad,state,city,'',1)) As geo
-- Replace in limit value if error occurs
FROM nj.condition_3 As ag WHERE ag.rating IS NULL ORDER BY addid LIMIT 3) As g ON a.addid = g.addid
WHERE a.addid = nj.condition_3.addid;
I have written a function to overcome this Error. So now it is working fine.
CREATE OR REPLACE FUNCTION geocode_all_values() RETURNS VOID AS
$$
DECLARE
r record;
g record;
BEGIN
FOR r IN select * from TableName where rating is null order by Sno
LOOP
BEGIN
FOR g IN select * from geocode_intersection(r.Street1,r.Street2,r.state,r.city,'',1)
LOOP
update TableName
set new_address = pprint_addy(g.addy),
rating = g.rating,
points = ST_AsTEXT(g.geomout)
where sno = r.sno;
END LOOP;
EXCEPTION WHEN OTHERS THEN
END;
END LOOP;
END;
$$
LANGUAGE plpgsql;
I can query docs using views just fine, but switching to N1QL gives me Success property as false. What went wrong ?
let cluster = new Cluster()
let bucket = cluster.OpenBucket("mydoc","")
let query = """SELECT * FROM mydoc where SET = 'SET24MM2SCLV01'"""
let result = bucket.Query(query)
Console.WriteLine(result.Success) //would give false
SET is a reserved word in N1QL. In order to use it as an identifier, you need to escape it with backticks, eg:
SELECT * FROM mydoc where `SET` = 'SET24MM2SCLV01'
If you don't you'll get a syntax error:
"errors": [
{
"code": 3000,
"msg": "syntax error - at SET"
}
]
You should change your query to
let query = """SELECT * FROM mydoc where SET = 'SET24MM2SCLV01'"""
let query = "SELECT * FROM mydoc where `SET` = 'SET24MM2SCLV01'"
EDIT
The query result also contains an Error property with the errors that occured in the query. This should be checked always if Success returns false. If the query fails even after escaping SET, this will explain what other error prevents the query from running.
For example, I just noticed that the entire query is enclosed in double quotes. This would send a string literal to the server instead of a query.