Updating field from 3 different tables - sql

I have a question using below 3 tables:
1. Company (table)
CONAME
COPOINTS
2. Group_Member (table)
CONAME
NAME
3. Member (table)
NAME
MPOINTS
I would like to have a correct query with the following condition:
Update Member
Set MPOINTS=MPOINTS+5
Where Company.CONAME=Group_Member.CONAME
And
Group_Member.NAME=Member.NAME
Can you please correct above query?

In T-SQL (MS-SQL Server dialect) this would be
Update Member
set MPOINTS = MPOINTS + 5
from Group_Member
join Company on Company.CONAME = Group_Member.CONAME
where Group_Member.NAME = Member.NAME
I would like to remind you, that names are not a good choice for neither primary nor foreign key relations. You will never be able to change a name without violating primary key constraints. Use a (numeric, autoincrement) ID column instead.

try this:
Update M
Set MPOINTS=MPOINTS+5
from Member M
join Group_Member GM
on GM.NAME=M.NAME
join Company C
on C.CONAME=GM.CONAME

SQL Server 2005+
UPDATE x
SET x.MPOINTS += 5
FROM (SELECT m.MPOINTS FROM Company c JOIN Group_Member g ON c.CONAME = g.CONAME
JOIN Member m ON g.NAME = m.NAME) x

Related

update a column in a sql table with a value from another table based on a relationship

I am working on a SQL query to update a column values with an Id from a different table
Example
Organization Table
Id Name
1 AA
2 BB
Events Table
Id Name OrgId
1 AA NULL
2 AA NULL
3 BB NULL
Now, I would like to update OrgId of Events table with its respective Id from Organization table
I did try the below query but I had explicitly do it for each organization
UPDATE Event SET OrId=
(SELECT DISTINCT O.ID FROM Organization O WHERE O.Name='AA') WHERE Name='AA'
May I know a better way to do it automatically?
Use a join:
update e
set orid = o.id
from event e join
organization o
on o.name = e.tenant;
You can perform a Merge using
MERGE Event AS e
USING Organization AS o
ON (e.Name= o.name)
WHEN MATCHED THEN
UPDATE SET e.OrgId = o.id
OUTPUT $action, inserted.*;
The output clause is optional and will print out the Ids that are inserted into the Event table.
The Merge is quite powerful as it has other clauses that can be used for cases when data is only in one table and not the other. Here's a nice post which explains things clearly. https://www.simple-talk.com/sql/learn-sql-server/the-merge-statement-in-sql-server-2008/

SQL code for updating a column where the WHERE condition is from another table

In SQL Server 2012, I have two tables: WINE_TYPE and WINE.
WINE_TYPE is the parent table, and wtCode is the primary key (foreign key in WINE).
I'm trying to execute code that will update the price of the wine (winePrice) by 50 cents, or 0.50. The catch is... my WHERE condition is from the parent table (I only need the wines that are made by the lead Amanda McPhee (wtLeadFirst='Amanda', wtLeadLast='McPhee') to be increased by 50 cents).
Inner join doesn't seem to work here. Help?
You can use UPDATE ... FROM syntax to update table with JOIN condition:
UPDATE WINE
SET WINE.WinePrice = WINE.WinePrice + 0.5
FROM
WINE
INNER JOIN
WINE_TYPE ON WINE.wtCode = WINE_TYPE.wtCode
WHERE
WINE_TYPE.wtLeadFirst='Amanda' AND WINE_TYPE.wtLeadLast='McPhee'
Update + Exists
Use Exists operator to check the existence of wtLeadFirst='Amanda' and wtLeadLast='McPhee' in parent table
update W
Set Wineprice = x + Wineprice
from Wine W
where exists (select 1
from wine_type WT
where W.wtCode =WT.code
and Wt.wtLeadFirst='Amanda'
and Wt.wtLeadLast='McPhee' )

Oracle sql - identify and update primary and secondary rows in same table

I have a scenario where i need to identify a combination of records which are duplicates and use them to mark and identify which one is primary and which is secondary and then use an additional column to update the keys. This will help me update another child table which has referential integrity. Here's an example
Table Member
ID Name Birthdt MID
1 SK 09/1988 312
2 SK 09/1988 999
3 SL 06/1990 315
4 GK 08/1990 316
5 GK 08/1990 999
So from the above table my logic to identify duplicate is -- I do a group by on Name and Birthdate and when MID is 999 i consider that as a duplicate
so I created another temp table to capture dups.
Table member_dups
ID NAME BIRTHDT MID M_flg M_Key
1 SK 09/1988 P 1
2 SK 09/1988 S 1
4 GK 08/1990 P 4
5 GK 08/1990 S 4
For my table member_dups im able to load the duplicate records and update the flag . however I'm finding it difficult to get the right M_KEY for records marked as secondary. If i can achieve that then I can take that record and update the other table accordingly.
Thanks for any help offered.
If I understand your logic right the records that had MID = 999 is the secondary in member_dups.
If so you should be able to use an simple update with a join:
update member_dups
set m_key = m.id
from member_dups md
inner join Member m on m.name = mb.name and m.birthdt = mb.birthdt
where mb.m_flg = 's' and m.mid = 999
This example uses MSSQL syntax and thus isn't valid Oracle syntax, but you should get the idea, and hopefully you know Oracle better than I do. I'll try to work out the correct Oracle syntax and update the answer soon.
EDIT: I think this is the working Oracle syntax, but I haven't been able to test it:
MERGE INTO member_dups
USING
(
SELECT id,
name,
birthdt
FROM Member
where m.mid = 999
) m ON (m.name = mb.name and m.birthdt = mb.birthdt and mb.m_flg = 's')
WHEN MATCHED THEN UPDATE
SET member_dups.m_key = m.id

How to make update query using data from two another tables (postgresql)

I have 3 tables:
manager with columns:
id serial PK
money int
facilities_work_data with columns:
id serial PK
income integer
manager_facilities with colunns:
id serial PK
manager_id references manager(id)
facilities_work_data_id references facilities_work_data(id)
The aim is to update all manager table rows by adding to manager.money column the value from facilities_work_data.income which in turn has to have id column selected as:
SELECT facilities_work_data_id from manager_facilities WHERE manager.id = manager_facilities.manager_id
At least I found the following request:
UPDATE A AS a SET money = a.money + b.income FROM B AS b WHERE b.a_id = a.id
But its not my case, seems like I need one more join.
Could you help with such a request?
you can get data from two tables and add the conditions in the where clause like below
update manager M
set money = money + FWD.income
FROM manager_facilities MF, facilities_work_data FWD
WHERE M.id = MF.manager_id
AND MF.facilities_work_data_id = FWD.id

Update field in table with the max value in another table, with the foreign key

I would like to update a "mostRecentDate" field in one table with the max date in another table with the same key value. The simplified table schema:
C(c_id, mostRecentDate)
L(l_id, c_id, theDate)
L.c_id is a foreign key reference to C.c_id.
Since
Select MAX(theDate)
FROM L
Group by L.c_id
Gives the max date for each c_id, I attempted to update via
UPDATE C
SET C.mostRecentDate= (SELECT Max(theDate)
FROM L
Where L.c_id = C.c_id
Group by L.c_id)
But this query does nothing (no errors generated, no rows updated). I am using MS Access 2003.
This one works in Access 2007. I think it should work in 2003 also.
UPDATE C
SET mostRecentDate = DMax("theDate", "L", "c_id = " & C.c_id);
Beware, if you have a c_id value in C which is not present in L, mostRecentDate will be replaced by Null for that row in C. If you want to prevent that, it will take more work.
However I don't see the value of storing those max date values in table C. You already know you can can retrieve them from table L any time you need them with a GROUP BY query of L.
Group by in subquery is not needed here. Try this:
UPDATE C
SET C.mostRecentDate= (SELECT Max(theDate)
FROM L
Where L.c_id = C.c_id)