Update a table by using a join [duplicate] - sql

This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 8 years ago.
I have two tables ta, tb. ta columns - cId, c1, c2. c1 and c2 contain nulls and need to be filled with data. tb columns - cId, c3, c4. The data for c1 and c2 will come from c3 and c4 respectively.
So, I tried to do a simple inner join first. Both tables were aliased as al_ta and al_tb respectively. Then, I put an update statement -
UPDATE ta SET
al_ta.c1 = al_tb.c3,
al_ta.c2 = al_tb.c4
FROM ta AS al_ta
INNER JOIN tb AS al_tb
ON al_tb.cId = al_tb.cId
This does not work and I get an error - The multi-part identifier al_ta.c1 could not be bound. How do I make this work ?
Sample tables -
ta
cId c1 c2
1 NULL NULL
2 NULL NULL
3 NULL NULL
tb
cId c3 c4
1 11 111
2 22 222
3 33 333
4 44 444

When referencing the columns, you need to use the alias, not the base table name, if you've abstracted the table names away in the JOIN. Guessing at what your join might look like, you probably meant to write it this way:
UPDATE ta SET
ta.c1 = tb.c3,
ta.c2 = tb.c4
FROM dbo.some_long_table_name_a AS ta
INNER JOIN dbo.some_long_table_name_b AS tb
ON ta.cId = tb.cId
WHERE ta.c1 IS NULL OR ta.c2 IS NULL;
I don't understand the purposes of saying:
FROM ta AS al_ta
Why would you bother using an alias here that is actually harder to write than the original table name?

Please Try it
update ta set
ta.c1 = b.c3,
ta.c2 = b.c4
from ta a join tb b on a.cid = b.cid

Related

How do I put the different data into one cell [duplicate]

This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 2 years ago.
This is the sample data that I've got
id
program
Community ID
sdg
246
#NoWasteGiving2020
97
11
246
#NoWasteGiving2020
97
17
246
#NoWasteGiving2020
97
10
With the data on the table above, how do I get the data to just be shown in one row and the sdg shown in one row. so at the end of the day the data will look like this:
246 #NoWasteGiving2020 97 10,11,17
This is the query that I've wrote
select Distinct a.id, a.title as program, b.community_id, c.goal_id as sdg
from programs_program as a
join associates_programcommunity as b on a.id = b.program_id
join associates_communitygoal as c on b.community_id = c.community_id
You can use string_agg as follows:
select a.id, a.title as program, b.community_id, string_agg(c.goal_id, ',') as sdg
from programs_program as a
join associates_programcommunity as b on a.id = b.program_id
join associates_communitygoal as c on b.community_id = c.community_id
group by a.id, a.title, b.community_id

Replace names of variable into the same column and join with other table

How to replace multiples names into the same column and join with other table?
I have to join 2 tables but the problem in the first table I have a variable (VAR) that is equal with the other (VAR1), I have to replace different things:
I have to convert all uppercase, I have to change:
'transform_' should match '_TRANS_'
'_difference_' should match '_DIF_'
'_upper_' should match '_UPP_'
AN EXAMPLE:
Table T
id var
-----------------------
1 col_difference_tr
2 pro_transform_rd
3 cap_upper_ld
Table S
process var1
-------------------------
32 COL_DIF_TR
45 PRO_TRANS_RD
32 CAP_UPP_LD
Then to get cross and coincided with the other table, I have already tried this:
SELECT A.* , IV.VALUE, VAR
FROM TABLA_T A
LEFT JOIN TABLA_S IV ON REPLACE(UPPER(IV.VAR),'_transform_', '_TRANS_')=
A.VAR1
LEFT JOIN TABLA_S IV ON REPLACE(UPPER(IV.VAR),'_difference_', '_DIF_')=
A.VAR1
LEFT JOIN TABLA_S IV ON REPLACE(UPPER(IV.VAR),'_upper_', '_UPP_')=
A.VAR1
so the final tables have to look like this:
id process var1
----------------------------
1 32 COL_DIF_TR
2 45 PRO_TRANS_RD
3 32 CAP_UPP_LD
Perhaps what you want is a more complicated JOIN condition?
SELECT A.*, IV.VALUE, IV.VAR
FROM TABLA_T A LEFT JOIN
TABLA_S IV
ON A.VAR1 IN (REPLACE(UPPER(IV.VAR), '_transform_', '_TRANS_'),
REPLACE(UPPER(IV.VAR), '_difference_', '_DIF_'),
REPLACE(UPPER(IV.VAR), '_upper_', '_UPP_')
);
This may not be efficient. You might also find a single replace() is best:
SELECT A.*, IV.VALUE, IV.VAR
FROM TABLA_T A LEFT JOIN
TABLA_S IV
ON A.VAR1 = REPLACE(REPLACE(REPLACE(UPPER(IV.VAR), '_transform_', '_TRANS_'), '_difference_', '_DIF_'), '_upper_', '_UPP_');
if the requirement is on oracle-plsql, then we can directly use decode function in where condition
DECODE(IV.VAR,
'_transform_', '_TRANS_',
'_difference_', '_DIF_',
'_upper_', '_UPP_',
IV.VAR) = A.VAR1;

How to Update Using Join (Linked Server)

I want to match the data between 2 databases.
I have 2 Databases. Aa and Bb, and I want to compare Aa to Bb. Database Bb is in linked server
I have join code like this
SELECT
B.Employee_Name, B.Employee_NIP, B.DomainName, A.NAMA, A.NIP,
A.StatusEmployee, A.ActiveStatus
FROM
[SERVER-B].Bb.dbo.employee_hierar AS B RIGHT OUTER JOIN
Bb AS B ON B.NIP = A.Employee_NIP
and I want update A.StatusEmployee from Y to N if there is NULL data on B.Employee_Name and B.Employee_NIP
note:
SQLServer
Please Advice
You can use a join. Something like:
update a
set StatusEmployee = 'N'
from bb a LEFT JOIN
[SERVER-B].Bb.dbo.employee_hierar b
on B.NIP = A.Employee_NIP
where b.EmployeeName is null and b.Employee_NIP is null and
a.StatusEmployee = 'Y';
This is same to update multi tables in one database. Standard sql should be written like this:
UPDATE A
SET StatusEmployee = "N"
WHERE NOT EXISTS
( SELECT * FROM B WHERE B.NIP = A.Employee_NIP )

sql update table from a join query

hi I have table as below named enrolment, I need to update the null averagemark from the data from another table so that the average of all assignments done by a student will populate the field. Can I anyone advise how to do this using Isql as the dbms?
enrolment-
student_id- course code - averagemark
1 a1 - 0
2 b2 - 0
3 c3 - 0
4 d4 - 0
assignment-
student_id- course code - assignment number mark
1 a1 1 - 50
1 b2 2 - 55
2 a1 1 - 60
2 b2 2 - 65
This example is based on Microsoft SQL Server. Copying the sample from How can I do an UPDATE statement with JOIN in SQL?:
update u
set u.assid = s.assid
from ud u
inner join sale s on
u.id = s.udid
Try something like this (providing the name of the column in the Student table is AverageMark)
UPDATE s
SET s.AverageMark = AVG(mark)
FROM Student s LEFT OUTER JOIN enrolment e
on e.Student_Id = e.Student_Id
LEFT OUTER JOIN assignment a
ON e.student_id = a.student_id
AND e.course_code = a.course_code
GROUP BY e.student_id, e.course_code

SQL using same codes table for two different columns

I have a table (Foo) that has two columns that store a code value from a codes table:
id - code1 - code2
1 - CC - DD
The Codes table:
Name - Code - Grouping
Call Center - CC - 22
County - DD - 54
I need a SQL that will pull 'Call Center' and 'County' based on the first table. It is assumed that I know Foo.code1 necessarily uses Codes.Grouping=22 and Foo.code2 uses Codes.Grouping=54.
I'm trying to write one SQL that will return both values.
Try this query:
select name from codes c inner join foo f on c.code = f.code1 or c.code = f.code2
Here is sqlfiddle
I am not really sure if this is the answer to your question, because I don't know exactly what you mean. I guess however, that you want to get both codes and groupings for an id value of your Foo table. For this I would
SELECT Foo.id,
C1.Name AS code1_name, C1.Code AS code1_code,
C1.Grouping AS code1_grouping,
C2.Name AS code2_name, C2.Code AS code2_code,
C2.Grouping AS code2_grouping
FROM Foo
INNER JOIN Codes AS C1 ON C1.Code = Foo.code1
INNER JOIN Codes AS C2 ON C2.Code = Foo.code2
WHERE Foo.id = 1;
expanding tuffkid sqlfiddlesqlfiddle