Updating multiple fields in a column simultaneously Microsoft Access - sql

Say I have the following table:
App owner owner_id
______________________
1 John NULL
2 Jack 000123
3 John NULL
4 April 000124
5 John NULL
6 April 000124
7 John 000123
8 Ash NULL
9 Ash NULL
10 Ash NULL
If I need to update John and Ash's owner_ids, is there a way to update each owner's owner_id for every occurrence in the table by only entering the information once?
I have to do this for about 1000 owners, but with the amount of duplicates I am looking at around 10000 blank owner_ids.
I do not have the proper rights to restructure or split the table.

If I didn't misunderstand your question, it's just:
update owners_table set owner_id = '000001' where owner = 'john'
update owners_table set owner_id = '000002' where owner = 'ash'
This will set the owner_id to 000001 in every row where the owner is John, and to 000002 in every row where the owner is Ash.
It's just more copy & paste when you have a lot of owners, because you have to create one query for each owner.
Another way would be to create a temporary table with just the owner and his new id:
owner newid
--------------
John 000001
Ash 000002
You can enter hundreds or thousands of owners and their new owner_ids into the table.
Then, you can update them in your main table all at once with the following query:
UPDATE owners_table
INNER JOIN ownertmp ON owners_table .owner = ownertmp.owner
SET owners_table.owner_id = [ownertmp].[newid];
After that, you can delete the temporary table again - it's just needed to run the query.

Related

SQL update values from table

I'm seriously struggling with some easy stuff.
Currently I have 2 tables (tmp_jmo and persons)
table tmp_jmo
----------
ID ADRESS PERSON_ID
115 Street 1 (null)
120 Street 2 (null)
121 Street 3 (null)
Table persons
ID NAME PERSON_ID
----------
115 John 14
120 Ellen 27
121 Mark 114
Now I want to update the Peson_id from tmp_jmo with the values from person_id (persons table)
In this case I've getting the error that there are to many values
Update tmp_jmo t SET person_id = persons.person_id where tmp_jmo.id = persons.id;
I've also tried to with temporary data but also failing.
I'm sorry to interrupt you with this kind of questions but it's ruining my day!
Many thanks!
In Standard SQL, you can do:
update tmp_jmo t
set person_id = (select p.person_id from persons p where tmp_jmo.id = p.id);
Many databases also support join or from in updates, but that is database-specific syntax.

Merge, delete when not on Source

I have an issue with a merge statement into a FACT TABLE
It was pretty simple until the users started to delete records from the source.
Current SQL:
Set Count = 1
WHEN NOT MATCHED
INSERT
WHEN MATCHED
UPDATED
New SQL:
So in this example, a record has been deleted from the source, it no longer matches but there is nothing to insert. I would like it the count to be set to 0.
WHEN DELETED FROM SOURCE
Set Count = 0
.
Source
Bob Jones | 1111
Mary Jones | 1112
James Jones | 1113
Helen Jones | 1114
TARGET
Bob Jones | 1111 | Count 1
Mary Jones | 1112| Count 1
James Jones | 1113| Count 1
Helen Jones | | 1114| Count 1
Peter Market | 1115| Count 0
I’m loading to a fact table using a merge and now they are just blanket deleting records, my facts are off. This must be accounted for somehow?
Thank you for any help at all.
You could do a merge using the target full outer joined with source, but id has to a unique key across both for this to work.
MERGE INTO target tgt
USING (SELECT CASE
WHEN s.id IS NULL THEN t.name --write this logic for all other non id columns.
ELSE s.name
END AS name,
coalesce(s.id, t.id) AS id, --use target's id when no source id is available
CASE
WHEN s.id IS NULL THEN 0 --zero for non matching ids
ELSE 1
END AS source_count
FROM target t
full outer join source s
ON s.id = t.id) src
ON ( src.id=tgt.id)
WHEN matched THEN
UPDATE SET tgt.name = src.name,
tgt.source_count = src.source_count
WHEN NOT matched THEN
INSERT (id,
name,
source_count)
VALUES(src.id,
src.name,
1) ; --insert 1 by default for new rows
Demo

Track which columns were modified by an Update

How do I track the list of updated columns in a table
Table1
Name Place Email Id
John US john#gmail.com 1
Sam US sam#gmail.com 2
Now if I update Name,place columns for John and place,email for Sam like
Name Place Email Id
Johnny UK john#gmail.com 1
Sam UK sammy#gmail.com 2
I want to store the list of updated attributes in another table as following
Id ListOfModifiedAttributes Status othercolumns
1 name,place success
2 place,email success
How to achieve the above in plsql.

SQL - Find duplicate children

I have a table containing meetings:
MeetID Description
-----------------------------------------------------
1 SQL Workshop
2 Cake Workshop
I have another table containing all participants in the meetings:
PartID MeetID Name Role
-----------------------------------------------------
1 1 Jan Coordinator
2 1 Peter Participant
3 1 Eva Participant
4 1 Michael Coordinator
5 2 Jan Coordinator
6 2 Peter Participant
I want to find is a list of all meetings that have 2 or more participants with Role = 'Coordinator'.
Eg. in the example above that would be the meeting with MeetID=1 and not 2.
I cannot for the life of me figure out how to do this, allthough I think it should be simple :-)
(I am using SQL Server 2012)
This is easy to do using group by and having:
select MeetId
from participants p
where Role = 'Coordinator'
group by MeetId
having count(*) >= 2;
Note: Role is a potential keyword/reserved word, so it is a bad choice for a column name.

Simple nested query on the same table

So this is my table set up of 100k rows. I have around 30k rows which have the wrong dealer associated to it even though it refers to the same person but just a different bank designation. This happened due to a failure to port information accurately from a previous version of the database .
Table CustomerName
Name Bank Dealer SSN
John 1 ABC unique1
Mike 1 DEF unique2
Mike 2 wrong unique2
Mark 1 XYZ unique3
Mark 2 wrong unique3
Desired Table set up
Table CustomerName
Name Bank Dealer SSN
John 1 ABC unique1
Mike 1 DEF unique2
Mike 2 DEF unique2
Mark 1 XYZ unique3
Mark 2 XYZ unique3
I want to write a query which will target the rows (Bank 2 rows essentially) and change it to Bank 1 Dealer values. Is there a way to do it ? I'm using T-SQL ( SSMS 2016 )
EDIT :
SSN are like primary keys for the customer. every customer will have one ssn . the bank 2 is basically a delinquent account bank. A customer may or may not have a bank 2 account , but they'll have a bank 1 account. But my problem is that somehow the dealer didn't come through right for bank 2 and I need to update to the correct value
The question is which Dealer to use. Let me guess that it is the first one. You can use CTE and update to accomplish this:
with topudate as (
select cn.*,
max(case when bank = 1 then dealer end) over (partition by ssn) as dealer1
from customername cn
)
update toupdate
set dealer = dealer1
where dealer <> dealer1 or dealer is null;
If you have some other logic for getting the right name, then that would go in the case expression instead.
update bank2
set bank2.dealer = bank1.dealer
from CustomerName bank1
join CustomerName bank2
on bank2.SSN = bank1.SSN
and bank2.Bank = 2
and bank1.Bank = 1