Search SQL table for value, lookup another value in that record and replace with value - sql

I want to run a script on a SQL table that will search the table for the users supervisor and then replace the supervisor value with the fullname value. How can I do this? I am using MSSQL and have one table containing this data.
Before:
fullname,username,supervisor
Timothy Dalton,tdalton,rmoore
Pierce Brosnan,pbrosnan,rmoore
Sean Connery,sconnery,rmoore
Roger Moore,rmoore,dcraig
Daniel Craig,dcraig,
After script:
fullname,username,supervisor
Timothy Dalton,tdalton,Roger Moore
Pierce Brosnan,pbrosnan,Roger Moore
Sean Connery,sconnery,Roger Moore
Roger Moore,rmoore,Daniel Craig
Daniel Craig,dcraig,
Thanks

Try something like this
Update t1
set t1.supervisor = t2.Fullname
from YourTable t1
join YourTable t2 on t1.supervisor = t2.username
This code hasn't been tested ... so make sure to backup table before using it

Try with the below query.
Please note that below query will fail if there are multiple supervisors with same last name and first name starts with same character.
(for example if the supervisor is rmoore and there are Roger Moore and Royal Moore in full name,then the below query will update the supervisor with any of these names)
UPDATE y
SET y.supervisor=y1.fullname
FROM YourTable y
JOIN YourTable y1
ON y1.fullname like LEFT(y.supervisor,1)+'%'
AND LTRIM(RTRIM(SUBSTRING(y1.fullname,CHARINDEX(' ',y1.fullname),LEN(y1.fullname))))=LTRIM(RTRIM(SUBSTRING(y.supervisor,2,LEN(y.supervisor))))

Related

Merge SQL Rows in Subquery

I am trying to work with two tables on BigQuery. From table1 I want to find the accession ID of all records that are "World", and then from each of those accession numbers I want to create a column with every name in a separate row. Unfortunately, when I run this:
Select name
From `table2`
Where acc IN (Select acc
From `table1`
WHERE source = 'World')
Instead of getting something like this:
Acc1
Acc2
Acc3
Jeff
Jeff
Ted
Chris
Ted
Blake
Rob
Jack
Jack
I get something more like this:
row
name
1
Jeff
2
Chris
3
Rob
4
Jack
5
Jeff
6
Jack
7
Ted
8
Blake
Ultimately, I am hoping to download the data and somehow use python or something to take each name and count the number of times it shows up with each other name at a given accession number, and furthermore measure the degree to which each pairing is also found with third names in any given column, i.e. the degree to which they share a cohort. So I need to preserve the groupings which exist with each accession number, but I am struggling to find info on how one might do this.
Could anybody point me in the right direct for this, or otherwise is the way I am going about this wise if that is my end goal?
Thanks!
This is not a direct answer to the question you asked. In general, it is easier to handle multiple rows rather than multiple columns.
So, I would recommend that you put each acc value in a separate row and then list the names as an array:
select t2.acc, array_agg(t2.name order by t2.name) as names
from `table2` t2
where t2.acc in (Select t1.acc
From `table1` t1
where t1.source = 'World'
)
group by t2.acc;
Otherwise, you are going to have a challenge just naming the columns in your result set.

MS Access join on like returning null column

I have two tables in MS Access that have the same Name_ID column, the only problem is that they differ slightly. Name_ID in table 1 looks like this:
Name_ID
Newton, Kate Little
River, Jane Armen
Barker, Bob Jep
Jake, Lee
And in table 2 it looks like this:
Name_ID
NEWTON, KATE L MD
RIVER, JANE A DO MS
BARKER, BOB J. (MD)
JAKE, LEE I.
I'm struggling with how to join the tables. I tried doing a join using like, based on Access/SQL Server 2008 Join using Like not working, but it's not working:
Select table1.*, table2.col from table1
left join
table2
on table1.Name_ID like '*' & table2.Name_ID & '*';
I also tried:
Select table1.*, table2.col from table1
left join
table2
on instr(table1.Name_ID, table2.Name_ID) > 0;
Both queries execute, but return blank columns. Any idea why it's not working, or a better way?
Both queries are looking for a full name string within another full name string. How would you expect Newton, Kate Little to match to *NEWTON, KATE L MD*? And for the second query, NEWTON, KATE L MD is not within Newton, Kate Little.
Probably best can do is extract last name part and match on that. Assumes names are not repeated and not multiple people with same last name and always last name followed by a comma.
SELECT Table1.Name_ID, Table2.Name_ID
FROM Table1 LEFT JOIN Table2
ON Table1.Name_ID LIKE Left(Table2.Name_ID, InStr(Table2.Name_ID,",")) & "*";

UPDATING a column to take first letter from another column

I would like to UPDATE my table to replace / insert in the INITIALS column, the first letter in the first name column
Table name: Mano
Title Firstname Lastname Telephone Initial Gender More columns
1 Mr Adam Smith 001
2 Mrs Angela Evans 002 AE
3 Mr Bill Towny 003
4 Miss Dame Beaut 004
I am interested in transforming it as per below
Title Firstname Lastname Telephone Inital Gender More columns
1 Mr Adam Smith 001 A
2 Mrs Angela Evans 002 A
3 Mr Bill Towny 003 B
4 Miss Dame Beaut 004 D
Many thanks
This seems like a simple update:
update t
set initials = left(firstname, 1);
I should point out that you don't even need a column. You can declare this as a computed column:
alter t add initials as (left(firstname, 1));
This would provide a column called initials (assuming that name is not already used) that always has the first letter of the firstname column -- without an update.
You can use substring function,
SELECT SUBSTR(Firstname, 1, 1) from mango;
Hope using this select statement you can update the table.
If you need the update query, let me know will give you
Left is the best and would more better if we use this with TRIM to prevent the extra spaces if any:
UPDATE TABLE table1 SET Initial = LEFT(LTRIM(Firstname), 1)
Add a computed column, will never be inconsistent:
alter table Mano add Initial as SUBSTR(Firstname, 1, 1)

Rename Duplicates in Access with SQL

I have a table in Access, and there is one column of data that has duplicates. I need to rename these duplicates (preferably by appending a 1, 2, 3, etc. at the end). For example, assume my data table looks like so:
ID Name Title
1 George Washington PRES
2 Martha Washington FL
3 John Adams PRES
4 Thomas Jefferson PRES
5 Benjamin Franklin NA
I want to make it look like:
ID Name Title
1 George Washington PRES-1
2 Martha Washington FL-1
3 John Adams PRES-2
4 Thomas Jefferson PRES-3
5 Benjamin Franklin NA-1
I wish to accomplish with an SQL Query in Access, but I'm open to other options. I just need to stay within Access. I'm very new to SQL, but open to learning anything!
Access UPDATE queries which incorporate a subquery can trigger
error #3073 ("Operation must use an updatable query").
When that happens, you can try a "domain function" instead of the subquery to get an updatable query. In your case, DCount is the appropriate domain function.
I tested this query with your sample data in Access 2010 and it did what you requested:
UPDATE YourTable AS y
SET y.Title =
[y].[Title]
& '-'
& DCount("*", "YourTable", "[Title]='" & [y].[Title] & "' AND ID <= " & [y].[ID]);
Note you must replace YourTable with your table name in two places in that query.
Also note the basic concept is similar to Gordon's answer. But it's adapted for Access.
However be aware that DCount and the other Access domain functions (DSum, DMin, DMax, DAvg, etc.) are not portable to other databases.
In Access, I think you can do this with a correlated subquery in an update:
update table as t
set title = title & '-' & (select count(*)
from table as t2
where t2.title = t.title and t2.id <= t.id
);

VBA Access SQL - field within LIKE operator

Can I use a table column within a Like operator? I've created an example,
TableA
Names Location
Albert Smith Senior Aberdeen
John Lee London
Michael Rogers Junior Newcastle
Mary Roberts Edinburgh
TableB
Names
Albert Smith
John Lee
Michael Rogers
I want to do a query such as:
SELECT TableA.Location
into NewTable
FROM TableA
WHERE TableA.Names Like '*[TableB.Names]*';
In this case, there would be no match for Mary Roberts, Edinburgh but the first three locations would be returned.
Is it possible to put a column into a like statement?
If not does anyone have any ideas how I could do this?
Hope you can help
PS I can't use an actual asterisk since this is removed and the text italicised, also I have read about using % instead but this has not worked for me.
You can join the two tables and use LIKE within the JOIN clause:
SELECT TableA.Location
into NewTable
FROM TableA
INNER JOIN TableB ON TableA.Names LIKE TableB.Names & '*';
Honestly, I had no idea that you can do this in Access before I tried it just now :-)