Execute Statement using one value of query array - postgresql

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?

Related

Postgres SQL: Query doesn't return data with ANY() clause when array is empty

There is postgres sql query, shown below:
Select agencyId, SUM(open),
from agencies
where agency_id= 100 and agency_platform = any(#platformIds)
GROUP BY agencyId;
If platformIds list is empty then this query doesn't return any data, and on the other hand below query data with no issues:
Select agencyId, SUM(open),
from agencies
where agency_id= 100
GROUP BY agencyId;
any thoughts?

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

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\"",

Querying Postgres SQL JSON Column

I have a json column (json_col) in a postgres database with the following structure:
{
"event1":{
"START_DATE":"6/18/2011",
"END_DATE":"7/23/2011",
"event_type":"active with prior experience"
},
"event2":{
"START_DATE":"8/20/11",
"END_DATE":"2/11/2012",
"event_type":"active"
}
}
[example of table structure][1]
How can I make a select statement in postgres to return the start_date and end_date with a where statement where "event_type" like "active"?
Attempted Query:
select person_id, json_col#>>'START_DATE' as event_start, json_col#>>'END_DATE' as event_end
from data
where json_col->>'event_type' like '%active%';
Returns empty columns.
Expected Response:
event_start
6/18/2011
8/20/2011
It sounds like you want to unnest your json structure, ignoring the top level keys and just getting the top level values. You can do this with jsonb_each, looking at resulting column named 'value'. You would put the function call in the FROM list as a lateral join (but since it is a function call, you don't need to specify the LATERAL keyword, it is implicit)
select value->>'START_DATE' from data, jsonb_each(json_col)
where value->>'event_type' like '%active%';

How to run for loop in PostgreSQL over answers of function results or select query in PostgreSQL

I was looking for a way to do nested iteration over the PostgreSQL function results or select query that finally give me desired output
for(answers of query 1)
{
for(answer of query 2)
{
select x from Table A where a='answer from query 1 & b>='answer of query 2(1)' & b<='answer from query 2(2)'
}
}
My 'second for' loop returns 2 value using each and every result from my 'first for loop'.
I found this post but its doesn't explains me nested for loop

Unable to figure out filter in slickdb

Using scala with slickdb. I have table called persons. And I am filtering out persons by name as below
table.Persons.filter({ row => {
println("inside filter")
req.personName.map(name => row.personName === name).getOrElse(true:Rep[Boolean])
})
The table contains 3 rows. But still println() is executed only once. How is this filter working?
First of all when you write something like
personTable.filter(p => { .... })
It evaluates it self as a Query which can generate the SQL Query when needed for actual DB querying. The generated SQL will be something like,
SELECT ...
FROM persons
WHERE ...
Now this SQL query is submitted to the DB for execution.
So, you code inside { ... } gets evaluated to generate the Query itself. And it has no relation to how many rows do you have in your DB table.
So, the println in your example will run just once even if your DB table has 0 rows, 1 row or a million rows.