Combining Cypher results with APOC SQL queries - sql

I am trying to update node properties of a PostgreSQL database that I have running in a virtual environment. I am building a Jazz knowledge base and would like to use the node ids to query additional properties from the PSQL database using APOC.
I have tried unwinding the ids and using them to run the SQL statements with APOC.
This is the code I have tried thus far:
MATCH (a:Artist)
WHERE a.genre = 'jazz'
WITH COLLECT(a.mbid) AS ids
UNWIND ids AS id
CALL apoc.load.jdbc('myDB',
"select DISTINCT a.gid, ar.name as country FROM artist a INNER JOIN area ar on a.area = ar.id WHERE a.gid = ?", [id]) YIELD row
MATCH (a:Artist) WHERE a.mbid = row.gid SET a.country = row.country
RETURN COUNT(a.country)
I am running into an error saying "ERROR: operator does not exist: uuid = character varying". Is there anyway to use the ids from the Cypher query to update each node individually through individual SQL statements with APOC?

Assuming the gid column is a UUID type in your Postgres database and that the error is when executing the query via apoc.load.jdbc, the following explicit casts of your IDs in the SQL should at least have the comparison work and have the query return a string value you can compare on:
MATCH (a:Artist)
WHERE a.genre = 'jazz'
WITH COLLECT(a.mbid) AS ids
UNWIND ids AS id
CALL apoc.load.jdbc('myDB',
"select DISTINCT a.gid::text, ar.name as country FROM artist a INNER JOIN area ar on a.area = ar.id WHERE a.gid = UUID(?)", [id]) YIELD row
MATCH (a:Artist) WHERE a.mbid = row.gid SET a.country = row.country
RETURN COUNT(a.country)

Related

Arel UpdateManager with Join creates invalid SQL - how to rephrase?

Apparently there is an issue in Arel core, where Arel::UpdateManager, when performing a column update on a join, does not generate the table name for the update column. It results in invalid SQL.
I ran into this in a Rails 5.2 app, where I had an SQL literal UPDATE statement that I was trying to rephrase in Arel.
UPDATE observations o, names n
SET o.lifeform = n.lifeform
WHERE o.name_id = n.id
AND o.lifeform != n.lifeform
In Arel, i wrote this:
names = Name.arel_table
obs = Observation.arel_table
join_source = Arel::Nodes::JoinSource.new(
obs, [obs.create_join(names)]
)
Arel::UpdateManager.new.
table(join_source).
where(obs[:id].eq(names[:id]).
and(obs[:lifeform].not_eq(names[:lifeform]))).
set([[obs[:lifeform], names[:lifeform]]])
This returns:
Mysql2::Error: Column 'lifeform' in field list is ambiguous:
The problem is at the end. The SQL generated from this does not specify the table where the column is to be set.
UPDATE `observations`
INNER JOIN `names`
SET `lifeform` = `names`.`lifeform`
WHERE (`observations`.`id` = `names`.`id`)
AND (`observations`.`lifeform` != `names`.`lifeform`)
Elsewhere, Arel-generated SQL usually qualifies columns with table names to avoid ambiguity. But the source code for update_manager.rb definitely uses Nodes::UnqualifiedColumn.new(column). (I have added my description to the Arel issue on GitHub.)
For now I'd maybe like to rephrase my Arel some other way. Is there a way to force Arel to quote the table name, similar to connection.quote_table_name?
Or would using a CTE be appropriate?
I guess one way to do this is with ActiveRecord's connection.update_all.
names = Arel::Table.new(:names)
Observation.joins(:name).
 where(names[:correct_spelling_id].not_eq(nil)).
  update_all("`observations`.`name_id` = `names`.`correct_spelling_id`")
This generates the desired SQL:
UPDATE `observations`
INNER JOIN `names`
ON (`observations`.`name_id` = `names`.`correct_spelling_id`)
AND (`names`.`correct_spelling_id` IS NOT NULL)
SET `observations`.`name_id` = `names`.`correct_spelling_id`
I think this is the way to go.

PostgreSQL, how to resolve multiple row errors returned by a subquery used as expression

I'm trying to output rows consisting of a value and a list of names. This is my query:
Update person set institution_v2 = (select dv.entity_id
from dictionary_v2 dv
left join dictionary_entry_v2 dev on dev.dictionary_id = dv.id
left join person p on p.name = dev.entry_value
JOIN journal_person_relation jpr on jpr.person_id = p.person_id
JOIN journal j on jpr.journal_id = j.journal_id)
But it fails with:
SQL Error [21000]: ERROR: more than one row returned by a subquery used as an expression
how can i solve this problem?
Presumably, you intend a correlated subquery. So, don't re-use person in the subuqery:
update person p
set institution_v2 = (select dv.entity_id
from dictionary_v2 dv join
dictionary_entry_v2 dev
on dev.dictionary_id = dv.id
where p.name = dev.entry_value
);
Note: This could still return duplicates. It is possible that a single value is not appropriate for the column -- perhaps you want an array -- or if an arbitrary matching value works, use limit 1.
I don't think the journal tables are adding anything to the logic.

SQL - update table from another table - syntax error

I have two SQL tables:
matches (columns are hometeam, awayteam, id, gameweek)
teams (columns are teamcode, teamname)
matches.hometeam and matches.awayteam consist of integers that correspond to integers in teams.teamcode. I am trying to get matches.hometeam and matches.awayteam to update to strings that are taken from corresponding strings in teams.teamname. If that is impossible, then I need to create a new table as described.
I have tried the below code, but it produces a syntax error on the penultimate two lines (error 1064 (42000)). I can't figure out why.
UPDATE matches
SET matches.hometeam = teams.teamname
FROM matches
INNER JOIN teams
ON (matches.hometeam = teams.teamcode);
Error 1064 is a MySQL error. If you are using MySQL, the correct syntax is:
UPDATE matches m JOIN
teams t
ON m.hometeam = t.teamcode
SET m.hometeam = t.teamname;
However, this will not really work. What you need to do is add ids:
alter table matches add hometeamcode int;
And then do:
UPDATE matches m JOIN
teams t
ON m.hometeam = t.teamcode
SET m.hometeamcode = t.teamname;
EDIT:
I think I misunderstood the whole situation. Your data model is totally correct. The matches table should have the integer codes, referring to the rows in teams.
You just need to write your query to get the names:
select m.*, th.teamname as hometeamname, ta.teamname as awayteamname
from matches m join
team th
on m.hometeam = th.teamcode join
team ta
on a.hometeam = ta.teamcode;
If you don't want to do the join, then encapsulate the logic in the view.

Alternate solution for the query - Used INTERSECT function in oracle plsql

I am working on the query. I have two tables one is detail table where not grouping happen and its like including all the values and other table is line table which has important column grouped together from detail table.
I want to show all the column from line table and some column from detail table.
I am using below query to fetch my records
SELECT ab.*,
cd.phone_number,
cd.id
FROM xxx_line ab,
xxx_detail cd
WHERE cd.reference_number = ab.reference_number
AND cd.org_id = ab.org_id
AND cd.request_id = ab.request_id
AND ab.request_id = 13414224
INTERSECT
SELECT ab.*,
cd.phone_number,
cd.id
FROM xxx_line ab,
xxx_detail cd
WHERE cd.reference_number = ab.reference_number
AND cd.org_id = ab.org_id
AND cd.request_id = ab.request_id
AND ab.request_id = 13414224
The query is working fine...
But I want to know is there any other way for I can achieve the same result by not even using Intersect.
I purpose is to find out all possible way to get the same output.
The INTERSECT operator returns the unique set of rows returned by each query. The code can be re-written with a DISTINCT operator to make the meaning clearer:
SELECT DISTINCT
xxx_line.*,
xxx_detail.phone_number,
xxx_detail.id
FROM xxx_line
JOIN xxx_detail
ON xxx_line.reference_number = xxx_detail.reference_number
AND xxx_line.org_id = xxx_detail.org_id
AND xxx_line.request_id = xxx_detail.request_id
WHERE xxx_line.request_id = 13414224
I also replaced the old-fashioned join syntax with the newer ANSI join syntax (which makes relationships clearer by forcing the join tables and conditions to be listed close to each other) and removed the meaningless table aliases (because code complexity is more directly related to the number of variables than the number of characters).

Teradata Update Table from Select Statement

Sorry if the title is unclear. Basically I'm trying to select certain records from multiple tables then update a certain column value for the returned records.
T-SQL Implementation
UPDATE
CUSTOMERS
SET
LIKES_US = 'Y'
FROM
RESTAURANT REST INNER JOIN CUSTOMERS CUST ON REST.LINK_ID = CUST.LINK_ID
WHERE
REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL
Oracle
UPDATE
(SELECT CUST.LIKES_US
FROM CUSTOMERS CUST INNER JOIN RESTAURANT REST ON CUST.LINK_ID=REST.LINK_ID
WHERE REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL) NEW_CUST
SET
NEW_CUST.LIKES_US = 'Y';
I am tried doing the same thing in Teradata as I did in Oracle but I get the following error:
Executed as Single statement. Failed [3707 : 42000] Syntax error, expected something like a name or a Unicode delimited identifier or an 'UDFCALLNAME' keyword between the 'UPDATE' keyword and '('.
Elapsed time = 00:00:00.003
STATEMENT 1: Unknown failed.
I looked online for the solution but had no luck.
Have you tried the following syntax with Teradata:
UPDATE CUSTOMERS C1
FROM (SELECT C2.LINK_ID
FROM CUSTOMERS C2
INNER JOIN RESTAURANTS R2
ON C2.LINK_ID = R2.LINK_ID
WHERE R2.REST_TYPE = 'DINER'
AND C2.LIKES_US IS NULL) D1
SET LIKES_US = 'Y'
WHERE C1.LINK_ID = C2.LINK_ID
I think that in this specific case, the below query will perform a little better since it needs one less join.
UPDATE C
FROM CUSTOMERS C, RESTAURANTS R
SET LIKES_US = 'Y'
WHERE
C.LINK_ID = R.LINK_ID
AND R.REST_TYPE = 'DINER'
AND C.LIKES_US IS NULL