This question already has answers here:
Referring to a select aggregate column alias in the having clause in Postgres
(2 answers)
Closed 6 years ago.
I'm creating a literal inside my select query, and at the end I'm trying to use having to find results that have a distance < 50000.
Coming from a MySql background, I'd think that this query should work.
SELECT "description",
"location",
ST_Distance_Sphere(location, ST_MakePoint(-126.4,45.32)) AS "distance"
FROM "news_agencies" AS "news_agencies"
HAVING distance < 50000;
I also tried adding quotes around the having >>>"distance"<<< text. If I remove 'having distance < 50000', my query runs fine and calculates the distance. Here is my error:
column "distance" does not exist
You cannot use alias name in a having clause. Try like this:
select * from
(
SELECT "description", "location",
ST_Distance_Sphere(location, ST_MakePoint(-126.4,45.32)) AS "distance"
FROM "news_agencies" AS "news_agencies") t
where distance < 50000;
Related
This question already has answers here:
Pass column name as parameter to PostgreSQL using psycopg2
(2 answers)
Closed 6 months ago.
I'm using psycopg2, not sqlalquemy. Basically, I can pass parameters to other portions of query, except for the column name. I'm trying to query some columns in order for a particular row, where the column names are enumerated in a list. The problem is the postgres query doesn't work for column names as strings. Any suggestions as to how to approach this problem?
cols = ['first', 'second', 'third']
query = """
SELECT %s FROM table_x
WHERE year=2021;
"""
for c in cols:
cur.execute(query, [c])
print(cur.fetchone()[0])
Using sql module from psycopg2. An example that I believe is more on point and cleaner then the answer posted in the comment.
import psycopg2
from psycopg2 import sql
con = psycopg2.connect(dbname="test", host='localhost', user='postgres', port=5432)
cols = ['first', 'second', 'third']
qry = sql.SQL("SELECT {} FROM table_x WHERE year=2021").format(sql.SQL(", ").join(map(sql.Identifier, cols)))
print(qry.as_string(con))
SELECT "first", "second", "third" FROM table_x WHERE year=2021
This question already has answers here:
Postgres INTERVAL using value from table
(3 answers)
Closed last year.
I want to add the value of another colum (which is an an integer representing miliseconds) to a date value in an update query.
I tried what this answer suggests but I can't find a way to not hardcode the value in the query but instead read it from the other column.
The two relevant fields are:
ScheduledExecuteDate: DATE(6)
RecurringInterval: INTEGER representing the number of miliseconds to add to the date
My query looks something like this:
UPDATE "Tasks" SET
"ScheduledExecuteDate"="ScheduledExecuteDate" + interval ("RecurringInterval" / 1000) second
WHERE "id" = 62
I've also tried 'second'. In both cases it throws an syntax error "at or near second"
Also note that I have to wrap the column names in quotes since they are case sensitive.
I also tried
"scheduledExecuteDate"=DATE_ADD("scheduledExecuteDate", interval ("Tasks"."recurringInterval" / 1000) seconds)
like answered here
You can use make_interval() passing fractional seconds:
UPDATE "Tasks"
SET "ScheduledExecuteDate" = "ScheduledExecuteDate"
+ make_interval(secs => "RecurringInterval"::double precision / 1000) second
WHERE "id" = 62
This question already has answers here:
What is the difference between single quotes and double quotes in PostgreSQL?
(3 answers)
Error: Column does not exist in postgresql for update [duplicate]
(1 answer)
postgres column "X" does not exist
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Closed 2 years ago.
I am running the query SELECT * FROM app_user WHERE login_id = "john"; and getting -
ERROR: column "john" does not exist
LINE 1: SELECT * FROM public.app_user WHERE login_id = "john";
^
SQL state: 42703
Character: 48
I have also tried SELECT * FROM public.app_user WHERE login_id = "john"; and I still get the same error.
The same error also occurs with any other column but the id column(id is the only non-VARCHAR column and is the primary key).
So, SELECT * FROM app_user WHERE id = 5; is working as expected .
A snapshot of the table follows.
If you write the string in double quotes, postgres will interpret it as a column name in the WHERE clause, just use single quotes:
SELECT * FROM public.app_user WHERE login_id = 'john';
This question already has answers here:
Multiple Counts with single query in mongodb
(3 answers)
Closed 4 years ago.
I need to do multiple count queries on the same collection with 3 different conditions at the same time.
Date <= x
Date <= y
Date <= z
Right now, I do:
collection.count(query1, function() {
collection.count(query2, function() {
collection.count(query3, function() {
Is there a way I can do all 3 queries in a single mongodb query.
Use facet in mongo 3.4 version:
db.col.aggregate(
{"$facet":{
"count1":[{"$match":query1},{"$count":"count"}],
"count2":[{"$match":query2},{"$count":"count"}],
"count3":[{"$match":query3},{"$count":"count"}]
}})
This question already has answers here:
Find sentences with two words adjacent to each other in Pg
(3 answers)
Closed 8 years ago.
I am trying to find sentences with two words adjecent to each other, using Postgres directly, not some command language extension. My tables are:
TABLE word (
spelling text,
wordid serial
)
TABLE sentence (
sentenceid serial
)
TABLE item (
sentenceid integer,
position smallint,
wordid integer
)
I have a simple query to find sentences with a single word:
SELECT DISTINCT sentence.sentenceid
FROM item, word, sentence
WHERE
word.spelling = 'word1' AND
item.wordid = word.wordid AND
sentence.sentenceid = item.sentenceid
and what I want is to filter that in turn by some word whose corresponding item has a item.sentenceid = the current query result's item (or sentence).sentenceid and
item.position = the current query result's item.position + 1
I don't see that ALIAS's work, and I don't know how to reference current state of the query in a subquery.
select distinct sentenceid
from
item i
inner join
item i2 using (sentenceid)
inner join
word w on w.wordid = i.wordid
inner join
word w2 on w2.wordid = i2.wordid
where
w.spelling = 'word1' and
w2.spelling = 'word2' and
i.position = i2.position - 1