Passing in an array of items to a node-postgres query - postgresql

I am using node-postgres to query my postgres database. I want to write the following query:
"SELECT * FROM users WHERE user_id IN (xxx)" where xxx is a list of ids of length between 0 and n. I want to filter the users table down to just the users that match an id in my list.
I attempted to write pgClient.query("SELECT * FROM users WHERE user_id IN ($1)",[idsAsString]) where idsAsString = "1,2,3", for instance. but I received this error:
"error: invalid input syntax for type integer: \"25,26,22,27\"",

Related

How to query using a list o id's in where parameter

I have a query:
select * from table
where id = 10
Now I have a list with 300 id's and I want to use the query above to query these 300 id's in the where clause concatenating the results.
How can I do this in Postgresql V14?

How to add pipeline().parameters to Lookup in Azure Data Factory?

I have Lookup Activity in Azure Data Factory.
I have parameter "offset", which have initial value 5.
I want to use parameter value as Integer value in Lookup query, but failing. Please advice.
Original Static Lookup Query:
SELECT *
FROM sales.[Customers]
ORDER BY CustomerId OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY
--Parameterized Lookup Query:
SELECT *
FROM sales.[Customers]
ORDER BY CustomerId #concat('OFFSET ', pipeline().parameters.offset,' ROWS FETCH NEXT 10 ROWS ONLY')
Error of ADF for parameterized Lookup:
A database operation failed with the following error: 'Incorrect syntax near
'#concat'.',Source=,''Type=System.Data.SqlClient.SqlException,Message=Incorrect syntax near
'#concat'.,Source=.Net SqlClient Data
Provider,SqlErrorNumber=102,Class=15,ErrorCode=-2146232060,State=1,Errors=
[{Class=15,Number=102,State=1,Message=Incorrect syntax near '#concat'.,},],'
Put the entire SQL statement in an expression (using the Expression Builder):
#concat('SELECT * FROM sales.[Customers] ORDER BY CustomerId OFFSET ', pipeline().parameters.offset, ' ROWS FETCH NEXT 10 ROWS ONLY')
You can directly call the parameter as well
SELECT *
FROM sales.[Customers]
ORDER BY CustomerId OFFSET #{pipeline().parameters.offset} ROWS FETCH NEXT 10 ROWS ONLY

Select rows in postgres table where an array field contains NULL

Our system uses postgres for its database.
We have queries that can select rows from a database table where an array field in the table contains a specific value, e.g.:
Find which employee manages the employee with ID 123.
staff_managed_ids is a postgres array field containing an array of the employees that THIS employee manages.
This query works as expected:
select *
from employees
where 123=any(staff_managed_ids)
We now need to query where an array field contains a postgres NULL. We tried the following query, but it doesn't work:
select *
from employees
where NULL=any(staff_managed_ids)
We know the staff_managed_ids array field contains NULLs from other queries.
Are we using NULL wrongly?
NULL can not be compared using =. The only operators that work with that are IS NULL and IS NOT NULL.
To check for nulls, you need to unnest the elements:
select e.*
from employees e
where exists (select *
from unnest(e.staff_managed_ids) as x(staff_id)
where x.staff_id is null);
if all your id values are positive, you could write something like this:
select *
from employees
where (-1 < all(staff_managed_ids)) is null;
how this works is that -1 should be less than all values, however comparison with null will make the whole array comparison expression null.

How can I orderby id when I use distinct on Web2py

I'm using postgresql database and have a log table. I want to show the ticket information from this log table and want to sort by id. But ticket id has duplicate data, so I use distinct to filter, and then I can't sort by id when I use distinct.
How can I solve this issue? Thanks!
rows = db(db.log.ticket_id != '').select(db.log.ALL, orderby=~db.log.id, distinct=db.log.ticket_id , limitby=((page-1) * PAGE_ROWS, (page*PAGE_ROWS)))
I got the error message:
SELECT DISTINCT ON expressions must match initial ORDER BY expressions
And I try to:
rows = db(db.log.ticket_id != '').select(db.log.ALL, orderby=~db.log.id|db.log.ticket_id, distinct=db.log.ticket_id , limitby=((page-1) * PAGE_ROWS, (page*PAGE_ROWS)))
But still can't work...

Execute Statement using one value of query array

I am attempting to execute a query using a prepared array. The array has two elements but this query uses only one of them. I am using node-postgres.
Array:
var g=[];
g.push(email,password);
Query:
getter.query('SELECT EXISTS (SELECT 1 FROM users WHERE email_address = $1)',g,
function(err, result){
if (err) console.log(err);
//do stuff including another query
});
This is the error I get:
ERROR: bind message supplies 2 parameters, but prepared statement "" requires 1`
STATEMENT: SELECT EXISTS (SELECT 1 FROM users WHERE email_address = $1)
How do I Execute Statement using only one value of query array? Is there a way to get prepared query statements to accept only one or some elements of an array?