UPDATE in postgresql with JOINS - postgresql

I have a form where some input has a value that is made with this query:
SELECT e.tree.nombre
FROM d.p
JOIN e.theme ON id = id_capa
LEFT JOIN e.tree ON e.theme.id_tree = e.tree.id
WHERE id_capa = 816
e and d are schemas. The id_capa = 816 is passed as an argument to the query from the form that I'm editing. It returns a value correctly. Now I want to edit that value on my form, so I need to UPDATE but I have multiple tables, I read that I can't do an UPDATE with JOINS, how should I do that UPDATE?

In your original SQL query, the table d.p is not used, neither in SELECT nor in conditions. So it can be skipped; the query can be rewritten as:
SELECT e.tree.nombre
FROM e.theme
LEFT JOIN e.tree ON e.theme.id_tree = e.tree.id
WHERE e.theme.id = 816
Since the query selects values from table joined with LEFT JOIN, the result can be NULL, ie joined record from e.tree table can be missed. In this case there is nothing to update.
Existing matching record can be updated with the query:
UPDATE e.tree
SET nombre = <NEW_VALUE>
FROM e.theme
WHERE e.theme.id = 816 AND e.theme.id_tree = e.tree.id

Related

more than one row returned by a subquery used as an expression in postgreSql query with IN clause

I am facing an issue with the query in postgreSql. Below is the query.
UPDATE t_e20so1_fieldrulethen AS fthen
SET c_thenfieldid = t1.c_fieldschemaid
FROM t_sys_fieldschema AS t1
WHERE fthen.c_lyrathenfieldid = t1.c_lyraid
AND fthen.c_rulefor = 5
AND t1.c_fieldtype = 18
AND t1.c_tablegroupsid IN (
CASE
WHEN fthen.c_iffieldid = t1.c_id THEN (SELECT
c_targettableid
FROM t_sys_tablegroups
WHERE c_parentid = 'c0b2f85c-bc93-466b-a54d-b1330440db98')
ELSE (SELECT c_targettableid
FROM t_sys_tablegroups
WHERE c_parentid =
'c0b2f85c-bc93-466b-a54d-b1330440db98')
END );
As per above query, i am updating t_e20so1_fieldrulethen table from t_sys_fieldschema. One of the conditions to check is t_sys_fieldschema.c_tablegroupsid should be having specific values which are in and I am fetching them from table t_sys_tablegroups.
Above query gives me error as shown below:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
Here, if I remove case from the query (below is what I mean) it works properly.
UPDATE t_e20so1_fieldrulethen AS fthen
SET c_thenfieldid = t1.c_fieldschemaid
FROM t_sys_fieldschema AS t1
WHERE fthen.c_lyrathenfieldid = t1.c_lyraid
AND fthen.c_rulefor = 5
AND t1.c_fieldtype = 18
AND t1.c_tablegroupsid IN (SELECT c_targettableid
FROM t_sys_tablegroups
WHERE
c_parentid = 'c0b2f85c-bc93-466b-a54d-b1330440db98')
Now I have only one select query in the "IN" clause.
I've checked that kind of case statement(two nested selects) - and it cannot be done that way. You generate two separate lists in one CASE.
One list for all records where fthen.c_iffieldid = t1.c_id and the other for ELSE statement.
As I wrote many times never use nested selects in "IN" clause. It is killing performance and causing many problems. Use "EXISTS".
As your CASE seems to be redundant (both WHEN and ELSE returns same value) change it that way and it will be faster.
UPDATE t_e20so1_fieldrulethen AS fthen
SET c_thenfieldid = t1.c_fieldschemaid
FROM t_sys_fieldschema AS t1
WHERE fthen.c_lyrathenfieldid = t1.c_lyraid
AND fthen.c_rulefor = 5
AND t1.c_fieldtype = 18
AND exists (select from t_sys_tablegroups t2
where t1.c_tablegroupsid=t2.c_targettableid
and c_parentid = 'c0b2f85c-bc93-466b-a54d-b1330440db98');

T-SQL: Joining on two separate fields based on specific criteria in a query

I have a query in which I am trying to get additional fields from another table through a join field that I manually create. The issue is when the field I create is null, then I want to use another field to join on. I am not sure how to do that without getting duplicate results. I tried a UNION query, but that just displays everything where the values are null when the manually created field value is null. Here is the query:
SELECT
BU = m.BU,
BUFBA = m.BUFBA,
a.CostCenter,
Delegate = m.Delegate,
a.DistrictLookup,
PCOwner = m.PCOwner,
a.PGr,
a.POrg,
PrimaryContact = m.PrimaryContact,
WarehouseManager = m.WarehouseManager,
Zone = m.Zone,
ZoneFBA = m.ZoneFBA
FROM
(SELECT
e.CostCenter,
e.District,
DistrictLookup =
CASE
WHEN e.PGr IN ('N01','BQE','BQA') THEN 'GSS'
WHEN e.PGr = 'BQB' THEN 'BG'
WHEN e.PGr = 'BQF' THEN 'FP'
ELSE e.District
END,
e.PGr,
e.POrg
FROM dbo.E1P e (NOLOCK)
WHERE
e.CoCd = '4433'
) a
LEFT JOIN dbo.Mapping m (NOLOCK) ON m.District = a.DistrictLookup
When the DistrictLookup field is NULL, I need a different join to occur so that the additional fields populate. That join would be:
LEFT JOIN dbo.Mapping m (NOLOCK) ON m.CostCenter = a.CostCenter
How can I write in this second join and not get duplicate results? This is a separate join on different fields and I think it differs from the other methods of doing a conditional join. If it, can someone please explain how to implement that logic into my query?
I believe this is what you are after...
LEFT JOIN dbo.Mapping m (NOLOCK)
ON (a.DistrictLookup IS NOT NULL AND m.District = a.DistrictLookup)
OR (a.DistrictLookup IS NULL AND m.CostCenter = a.CostCenter)

How to use Not Equals in Postgresql Joins

I'm doing this:
select * from schema2."Student" a INNER JOIN
schema1."StudentMapping" b on ( a."StudentID" = b."StudentID")
where a."IsRemoved" = false AND b."IsRemoved" = false
to get only those records from Student table that are present in StudentMapping table, here IsRemoved column I'm using for soft deletion(i.e, whenever any record is to be deleted from any of those tables then only I'm setting it to true, so for IsRemoved = false records are present in the tables) and the query is working fine. Now what I wanted is to get all those records from Student table that are not present in StudentMapping table so I tried this:
select * from schema2."Student" a INNER JOIN
schema1."StudentMapping" b on ( a."StudentId" != b."StudentId")
where a."IsRemoved" = false AND
b."IsRemoved" = false
but this is giving lot of records, more than I expected, what is wrong with this query or is there another way in Postgresql to get all matching recording from one table that are not present in another table.
Do a LEFT OUTER JOIN and then in the where clause, specify to only show recods where the join resulted in a NULL:
select * from schema1."Student" a LEFT OUTER JOIN
schema1."StudentMapping" b on ( a."StudentID" = b."StudentID")
where a."IsRemoved" = false
and b.StudentID IS NULL

comprare aggregate sum function to number in postgres

I have the next query which does not work:
UPDATE item
SET popularity= (CASE
WHEN (select SUM(io.quantity) from item i NATURAL JOIN itemorder io GROUP BY io.item_id) > 3 THEN TRUE
ELSE FALSE
END);
Here I want to compare each line of inner SELECT SUM value with 3 and update popularity. But SQL gives error:
ERROR: more than one row returned by a subquery used as an expression
I understand that inner SELECT returns many values, but can smb help me in how to compare each line. In other words make loop.
When using a subquery you need to get a single row back, so you're effectively doing a query for each record in the item table.
UPDATE item i
SET popularity = (SELECT SUM(io.quantity) FROM itemorder io
WHERE io.item_id = i.item_id) > 3;
An alternative (which is a postgresql extension) is to use a derived table in a FROM clause.
UPDATE item i2
SET popularity = x.orders > 3
FROM (select i.item_id, SUM(io.quantity) as orders
from item i NATURAL JOIN itemorder io GROUP BY io.item_id)
as x(item_id,orders)
WHERE i2.item_id = x.item_id
Here you're doing a single group clause as you had, and we're joining the table to be updated with the results of the group.

Postgres - Get data from each alias

In my application i have a query that do multiple joins with a table position. Just like this:
SELECT *
FROM (...) as trips
join trip as t on trips.trip_id = t.trip_id
left outer join vehicle as v on v.vehicle_id = t.trip_vehicle_id
left outer join position as start on trips.start_position_id = start.position_id and start.position_vehicle_id = v.vehicle_id
left outer join position as "end" on trips.end_position_id = "end".position_id and "end".position_vehicle_id = v.vehicle_id
left outer join position as last on trips.last_position_id = last.position_id and last.position_vehicle_id = v.vehicle_id;
My table position has 35 columns(for example position_id).
When I run the query, in result should appear the table position 3 times, start, end and last. But postgres can not distinguish between, for exemplar, start.position_id, end.position_id and last.position_id. So this 3 columns are group and appear as one, position_id.
As the data from start.position_id and end.position_id are different, the column, position_id, that appear in result, it's empty.
Without having to rename all the columns, like this: start.position_id as start_position_id.
How can i get each group of data separately, for exemple, get all columns from the table 'start'. In MYSQL i can do this operation by calling fetch_fields, and give the function an alias, like 'start'.
But i can i do this in Postgres?
Best Regards,
Nuno Oliveira
My understanding is that you can't (or find it difficult to) discern between which table each column with a shared name (such as "position_id") belongs to, but only need to see one of the sets of shared columns at any one time. If that is the case, use tablename.* in your SELECT, so SELECT trips.*, start.*... would show the columns from trips and start, but no columns from other tables involved in the join.
SELECT [...,] start.* [,...] FROM [...] atable AS start [...]