postgreSQL There is an entry for table "xxx", but it cannot be referenced from this part of the query - postgresql

What is the proper way to write this query in postgreSQL?
I am trying to normalize (i.e. standardize) an address. I can use pagc with no problem if I feed it a hardcoded address. However, I need to feed it an address parsed from parts. I see there are several similar questions here on stack overflow referencing the same error. These queries are complex and are all pretty different from mine, so I couldn't get to the solution from reading the other posts.
I have tried:
with full_address as (home_address1 || ','|| home_city ||','|| home_state ||,','|| home_zip) update contacts set (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num) = (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal) FROM pagc_normalize_address(full_address) AS addy where contact_id = 833826;
This throws error:
syntax error at or near "home_address1"
LINE 26: with full_address as (home_address1 || ','|| home_city |.
I have also tried:
update contacts set (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num) = (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal) FROM pagc_normalize_address(home_address1 ||','||home_city||','||home_state||','||','||home_zip) AS addy where contact_id = 833826;
Error:
ERROR: invalid reference to FROM-clause entry for table "contacts"
LINE 24: ...abbrev,addy.internal) FROM pagc_normalize_address(home_addre...
^
HINT: There is an entry for table "contacts", but it cannot be referenced from this part of the query.
SQL state: 42P10
Character: 2297

The first query is gibberish, the second makes sense but fails because you cannot use a lateral reference to the updated table's columns in the FROM clause.
Try a CTE like this:
WITH addy AS (
SELECT addy.* FROM
contacts
CROSS JOIN LATERAL
pagc_normalize_address(home_address1
|| ',' || home_city || ',' || home_state || ',' || ',' || home_zip) AS addy
WHERE contacts.contact_id = 833826
)
UPDATE contacts
SET (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num)
= (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal)
FROM addy
WHERE contact_id = 833826;

Related

Syntaxerror, when creating an MATERIALIZED VIEW of tsvectors for fulltextsearch in postgresql

I am trying to implement a full text search, taking spelling mistakes into account.
Therefor, I try to create a MATERIALIZED VIEW of tsvector of all relevant columns.
CREATE MATERIALIZED VIEW unique_lexeme AS
SELECT word FROM ts_stat(
'SELECT to_tsvector('simple', cve.descriptions) ||
to_tsvector('simple', cpeMatch.criteria) ||
to_tsvector('simple', array_to_string(reference.tags, ' '))
FROM cve
JOIN cpeMatch ON cpeMatch.cve_id = cve.id
JOIN reference ON reference.cve_id = cve.id
GROUP BY cve.id');
But when I run this code, I get:
SQL-Fehler [42601]: FEHLER: Syntaxfehler bei »simple«
Position: 92
Saying there is a syntax error at 'simple'.
I have no idea how to resolve this issue.
Just to make clear, I installed pg_trgm but didn't make any configs ore changes.
You need to quote simple but you are already in a quoted string. The easiest is to change the string delimiter:
CREATE MATERIALIZED VIEW unique_lexeme AS
SELECT word FROM ts_stat(
$$SELECT to_tsvector('simple', cve.descriptions) ||
to_tsvector('simple', cpeMatch.criteria) ||
to_tsvector('simple', array_to_string(reference.tags, ' '))
FROM cve
JOIN cpeMatch ON cpeMatch.cve_id = cve.id
JOIN reference ON reference.cve_id = cve.id
GROUP BY cve.id$$);

Postgresql - How to make NOT LIKE + SELECT statement, comparing two columns between tables

Hit a bit of a brickwall here, I haven't found anything that seems to work.
SELECT *
FROM glsltransaction gls
INNER JOIN glhistory h ON gls.sltrxid = h.schedxrefid
INNER JOIN apvendor ap ON ap.vendorid = gls.acctid
INNER JOIN glchartofaccounts coa USING (acctdeptid)
WHERE h.description <> gls.description
AND h.description not like || ap.name || '%'
AND gls.description <> ''
This line here AND h.description not like || ap.name || '%' is what i'm having issues with.
I get the following error when trying to run that statement above:
'Error occurred in running query from editor : ERROR: operator does not exist: || text Hint: No operator matches the given name and argument type. You might need to add an explicit type cast. Position: 563'
I'm effectively wanting it to function like a.column not like b.column%
Any help will be greatly appreciated, thanks!

sqlalchemy to create temporary table

I created a temporary table with sqlalchemy (with an underlying postgres database) that is going to be joined with a database table. However, in some cases when a value is empty '' then postgres throws the error:
failed to find conversion function from unknown to text
SqlAlchemy assembles everything to the following context
[SQL: 'WITH temp_table AS \n(SELECT %(param_1)s AS id, %(param_2)s AS email, %(param_3)s AS phone)\n SELECT campaigns_contact.id, campaigns_contact.email, campaigns_contact.phone \nFROM campaigns_contact JOIN temp_table ON temp_table.id = campaigns_contact.id AND temp_table.email = campaigns_contact.email AND temp_table.phone = campaigns_contact.phone'] [parameters: {'param_1': 83, 'param_2': '', 'param_3': '+1234567890'}]
I assemble the temporary table as follows
stmts = []
for row in import_data:
row_values = [literal(row[value]).label(value) for value in values]
stmts.append(select(row_values))
subquery = union_all(*stmts)
subquery = subquery.cte(name="temp_table")
The problem seems to be the part here
...%(param_2)s AS email...
which after replacing the param_2 results in
...'' AS email...
which will cause the error mentioned above.
One way to solve the issue is to perform a cast
...''::text AS email...
However, I don't know how to perform ::text cast with sqlalchemy!?

Why am I getting a syntax error in my PostgreSQL query?

I apologize I'm very new to PostgreSQL. When I try and run a migration that works for everyone else, I get errors on my computer. I took the script that's failing from the migration and ran it in pgAdmin3 to see what was going on.
Here is my query:
DROP VIEW IF EXISTS vw_admin_question_export_text;
CREATE VIEW vw_admin_question_export_text AS
select name || '|' || q.question_key || '|' || qt.type_name || '|' || qo.options import from question q
full JOIN (SELECT question_id, COALESCE(string_agg(option_text || '|' || option_value, '|'),'') as options
FROM question_option
GROUP BY question_id) qo
on (q.question_id = qo.question_id)
JOIN question_type qt
on (q.questiontype_id = qt.questiontype_id);
This is the error I'm getting:
ERROR: syntax error at or near "import"
LINE 4: ...n_key || '|' || qt.type_name || '|' || qo.options import fro...
^
********** Error **********
ERROR: syntax error at or near "import"
SQL state: 42601
Character: 179
My coworker thinks it may be an issue with my PostgreSQL version which is 9.5.2
The problem here is that import is a reserved keyword in PostgreSQL, so you can't use it as an alias without double-quoting it or using AS keyword.
Simpler test:
postgres=# SELECT 'foo' import;
ERROR: 42601: syntax error at or near "import"
LINE 1: SELECT 'foo' import;
^
LOCATION: scanner_yyerror, scan.l:1082
postgres=# SELECT 'foo' AS import;
import
--------
foo
(1 row)
postgres=# SELECT 'foo' "import";
import
--------
foo
(1 row)
To solve your issue, you can simple add AS keyword to define the alias:
CREATE VIEW vw_admin_question_export_text AS
select
name || '|' || q.question_key || '|' || qt.type_name || '|' || qo.options AS import
from question q
full JOIN (
SELECT
question_id,
COALESCE(string_agg(option_text || '|' || option_value, '|'),'') as options
FROM question_option
GROUP BY question_id
) qo
on (q.question_id = qo.question_id)
JOIN question_type qt
on (q.questiontype_id = qt.questiontype_id);
Another option is to simple chose another name, that does not collide with one in reserved keyword list (I recommend that option). In any case, always use AS is a good idea to make things more explicit and avoid problems like this one.

ERROR: missing FROM-clause entry for table "movies"

I am new to SQL and need to query a database to extract certain information before I can import it into another software I am familiar with to analyse the data. This query was sent to me by a friend who I don't have access to at the moment, and I cannot figure out why it gives me the following error:
ERROR: missing FROM-clause entry for table "movies"
LINE 8: FROM (SELECT movies.movieid
Here is the query:
SELECT innerselect.movieid
,innerselect.title
,innerselect.year
,innerselect.imdbid
,innerselect.budget[1] AS budget_currency
,TO_NUMBER(innerselect.budget[2], '999999999999990.00') AS budget_total
,innerselect.businesstext
FROM (SELECT movies.movieid
,movies.title
,movies.year
,movies.imdbid
,business.businesstext
,regexp_matches(business.businesstext, '^BT:[ ](USD)[ ](-?(?!0)(?:\d+|\d{1,3}(?:,\d{3})+))', 'g') AS budget -- creates a PostgreSQL Array which contains the content matched with the RegEx Groups FROM movies LEFT JOIN business ON movies.movieid=business.movieid WHERE movies.movieid > 2753500
) AS innerselect
Any help would be greatly appreciated.
Problem is you put the FROM on the same line as the comment, so the FROM clause was ignored.
SELECT innerselect.movieid
,innerselect.title
,innerselect.year
,innerselect.imdbid
,innerselect.budget[1] AS budget_currency
,TO_NUMBER(innerselect.budget[2], '999999999999990.00') AS budget_total
,innerselect.businesstext
FROM (SELECT movies.movieid
,movies.title
,movies.year
,movies.imdbid
,business.businesstext
,regexp_matches(business.businesstext, '^BT:[ ](USD)[ ](-?(?!0)(?:\d+|\d{1,3}(?:,\d{3})+))', 'g') AS budget -- creates a PostgreSQL Array which contains the content matched with the RegEx Groups
FROM movies LEFT JOIN business ON movies.movieid=business.movieid WHERE movies.movieid > 2753500
) AS innerselect