Postgresql updating query based on join - postgresql

I am trying to update a table I created on a simple join. I have tried this many different ways and am still getting errors. Any help is appericated
Update well.formation_assignments as v
set
Primary_Formation = well.geo.primary_formation
from well.geo as s
inner join
on well.formation_assignments.api14=well.geo.api14;
ERROR: syntax error at or near "on"
LINE 6: on well.formation_assignments.api14=well.geo.api14;
^
SQL state: 42601
Character: 123

Try this instead :
UPDATE well.formation_assignments v
SET Primary_Formation = s.primary_formation
FROM well.geo s
WHERE v.api14 = s.api14;
As standard Postgresql update syntax is as follows :
[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table [ [ AS ] alias ]
SET { column = { expression | DEFAULT } |
( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
[ FROM from_list ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
Documentation : https://www.postgresql.org/docs/9.1/sql-update.html

Related

Group Neighbors by Outgoing Edge Type and Add Self to Result List

I want a result like this:
[
{
"vertex": [ the_vertex_itself ]
},
{
"outgoingEdgeGroup1": [ list_of_outgoing_neighbors_with_edge_type_outgoingEdgeGroup1 ]
},
{
"outgoingEdgeGroup2": [ list_of_outgoing_neighbors_with_edge_type_outgoingEdgeGroup2 ]
}
]
I'm able to get this:
[
{
"outgoingEdgeGroup1": [ list_of_outgoing_neighbors_with_edge_type_outgoingEdgeGroup1 ]
},
{
"outgoingEdgeGroup2": [ list_of_outgoing_neighbors_with_edge_type_outgoingEdgeGroup2 ]
}
]
With the following query:
g.V('{unitId}').outE().group().by(label()).by(inV().fold())
But how would I append on the target vertex itself?
One way is just to use a union. If you need a more complete key/value type of structure that can be created also by adding project steps or nested group steps.
Using the air-routes data set:
gremlin> g.V(44).union(identity().values('city'),inE().group().by(label()).by(outV().fold())).fold()
==>[Santa Fe,[contains:[v[3742],v[3728]],route:[v[13],v[31],v[20],v[8]]]]

how to update a table with a join or left join condition in postgresql?

I have tables
Table A
a_id
b_id
a_field_1
a_field_2
a_field_3
Table B
b_id
b_field_1
b_field_2
b_field_3
if I want to update a_field_1 only if b_field_1 mets certain condition ? how to do that ? i tried this but it doesn't work
UPDATE A SET A.a_field_1 = 1 , A.a_field_2 = 2
WHERE B.b_field_1 = 1 AND A.b_id = B.b_id;
According to Postgres document you can use like below:
[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table_name [ * ] [ [ AS ] alias ]
SET { column_name = { expression | DEFAULT } |
( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |
( column_name [, ...] ) = ( sub-SELECT )
} [, ...]
[ FROM from_item [, ...] ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
Sample Query:
UPDATE A
SET a_field_1 = 1, a_field_2 = 2
FROM B
WHERE
B.b_field_1 = 1
AND A.b_id = B.b_id;

Postgresql get keys from array of objects in JSONB field

Here' a dummy data for the jsonb column
[ { "name": [ "sun11", "sun12" ], "alignment": "center", "more": "fields" }, { "name": [ "sun12", "sun13" ], "alignment": "center" }, { "name": [ "sun14", "sun15" ] }]
I want to fetch all the name keys value from jsonb array of objects...expecting output -
[ [ "sun11", "sun12" ], [ "sun12", "sun13" ], [ "sun14", "sun15" ] ]
The problem is that I'm able to fetch the name key value by giving the index like 0, 1, etc
SELECT data->0->'name' FROM public."user";
[ "sun11", "sun12" ]
But I'm not able to get all the name keys values from same array of object.I Just want to get all the keys values from the array of json object. Any help will be helpful. Thanks
demo:db<>fiddle (Final query first, intermediate steps below)
WITH data AS (
SELECT '[ { "name": [ "sun11", "sun12" ], "alignment": "center", "more": "fields" }, { "name": [ "sun12", "sun13" ], "alignment": "center" }, { "name": [ "sun14", "sun15" ] }]'::jsonb AS jsondata
)
SELECT
jsonb_agg(elems.value -> 'name') -- 2
FROM
data,
jsonb_array_elements(jsondata) AS elems -- 1
jsonb_array_elements() expands every array element into one row
-> operator gives the array for attribute name; after that jsonb_agg() puts all extracted arrays into one again.
my example
SELECT DISTINCT sub.name FROM (
SELECT
jsonb_build_object('name', p.data->'name') AS name
FROM user AS u
WHERE u.data IS NOT NULL
) sub
WHERE sub.name != '{"name": null}';

Update with inner join Postgresql

Because doing this update does not work for the where clause? The update does it for me all.
UPDATE ventas SET eav_id = 7
FROM ventas AS A
inner join ventasDetalle AS e on A.act_id = e.act_id and e.exp_id = A.exp_id
where a.eav_id = 1
update ventas a
set eav_id = 7
from ventasDetalle e
where a.eav_id = 1 and (a.act_id, a.exp_id) = (e.act_id, e.exp_id)
The Postgresql UPDATE syntax is:
[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table [ [ AS ] alias ]
SET { column = { expression | DEFAULT } |
( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
[ FROM from_list ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
So I think you want:
UPDATE ventas AS A
SET eav_id = 7
FROM ventasDetalle AS e
WHERE (A.act_id = e.act_id and e.exp_id = A.exp_id)

syntax issue ? passing objects as parameters in neo4j cypher statement

I am running a query like this using the REST API to the transaction endpoint:
{
"statements" : [{"statement":"MATCH (n)-[r]-(m) WHERE id(n) IN {diagramnodes} return [type(r),labels(m)] ",
"parameters" :{
"diagramnodes" : [28]
}}]
}
which returns the expected result:
{
"commit": "http://myserver:7474/db/data/transaction/542/commit",
"results": [
{
"columns": [
"[type(r),labels(m)]"
],
"data": [
{
"row": [
[
"CONTAINS",
[
"Sentence"
]
]
]
},
{
"row": [
[
"CONTAINS",
[
"Prologram",
"Diagram"
]
]
]
},
.......
]
}
],
"transaction": {
"expires": "Sun, 07 Sep 2014 17:50:11 +0000"
},
"errors": []
}
When adding another parameter and a filter to limit the types of rels that are returned:
{"statements": [{
"statement": "MATCH (n)-[r]-(m) WHERE id(n) IN {diagramnodes} AND [type(r),labels(m)] IN {includerels} return r ",
"parameters": {
"diagramnodes": [28],
"includerels": [
[
"CONTAINS",
[
"Prologram",
"Diagram"
]
],
[
"HAS_TARGET",
["Term"]
]
]
}
}]}
it does not return any results. Why?
I found a workaround, by concatenating the reltype and labels, and comparing it to a collection of primitive types. This is the cypher (added some CRLF to make it easier to read)
{
"statements" : [{"statement":"
MATCH (n)-[r]-(m)
WHERE id(n) IN {diagramnodes}
WITH type(r) as rtype, REDUCE(acc = '', p IN labels(m)| acc + ' '+ p) AS mlabels,m
WITH rtype+mlabels As rtypemlabels,m
WHERE rtypemlabels IN {includerels}
RETURN rtypemlabels,id(m) ",
"parameters" :{
"diagramnodes" : [28],
"includerels": ["HAS_TARGET Term","CONTAINS Sentence","CONTAINS Prologram Diagram"]
}}]
}
Note 1 : type(r)+REDUCE(acc = '', p IN labels(m)| acc + ' '+ p) does not work, you have to insert an additional WITH
Note 2 : comparing a collection of nested objects with an IN clause should be possible and remains on my wish list. ;)
IN operations very probably only work for collections of primitive values.
what you can try to do is to rewrite it into ALL(x in coll WHERE expr(x)) predicate.
for an input like:
[["CONTAINS",["Prologram","Diagram"]],
["HAS_TARGET",["Term"]]]
you can try:
ALL(entry in {includerels} WHERE type(r) = entry[0] AND ALL(l in labels(n) WHERE l in entry[1]))
You can use UNWIND on your parameters array instead of using IN. Depending on your data, you might have to use DISTINCT as well. But UNWIND works well for me.