SQL Server - Update value based another table - sql

I have 2 tables :
Table1
parcel_number
pool_type
Table2
parcel_number
need_water
I would like to know if the table 1 "pool" column has a value than update the "need_water" column of the table 2 to yes.
Here what I would like to have for the table 2.
Table 1
parcel_number
pool_type
1
Circle
2
Oval
3
Null
4
Rectangular
Table 2
parcel_number
need_water
1
Yes
2
Yes
3
No
4
Yes
if exists(select a.pool_type
from table1 a
where a.parcel_number = b.parcel_number)
Begin
Update b
set b.need_water = 'Yes'
from table2 b
end
else
Begin
Update b
set b.need_water = 'No'
from table2 b
End
Thank you

A single update statement should be able to solve this:
UPDATE t2
SET t2.need_water = CASE WHEN t1.parcel_number IS NOT NULL THEN 'Yes' ELSE 'No' END
FROM table2 t2
INNER JOIN table1 t1
ON t2.parcel_number = t1.parcel_number

An UPDATE with a JOIN will work
UPDATE t2
SET need_water = 'yes'
FROM Table1 t1
JOIN Table2 t2
ON t1.parcel_number = t2.parcel_number
WHERE t1.pool_type IS NOT NULL

Related

update query join table

I want update status column where id table 1 = id table 2 and status = ''
table1
===========================
id | delivery_status | name
===========================
table2
=============
id | status |
=============
delivery status on table1 and status on table2 are different case.
I just have one row match (id table 1 = id table 2) and 2 rows no match. if I query:
UPDATE myschema.table1
SET status = 'COMPLETED'
FROM myschema.table1 t1, myschema.table2 t2
WHERE t2.delivery_status = 'B' and t1.status = '' and t1.id = t2.id;
If this select, the result have 1 row, but when I execute this update query, 3 rows are update to COMPLETED. how to fix this problem? I just want to update one row. I already add the condition t1.id = t2.id. The id on table 1 and table 2 just match one row.
Only include the table to update once
UPDATE myschema.table1
SET status = 'COMPLETED'
FROM myschema.table2 t2
WHERE t2.delivery_status = 'B' and table1.status = '' and table1.id = t2.id;

Join Two Column into New Column

How do I combine both COLUMN2 of table1 and table2?
SELECT COLUMN1, COLUMN2 FROM TABLE1
C1 C2
A 1
SELECT COLUMN1, COLUMN2 FROM TABLE2
C1 C2
A 1
B 2
C 2
C1 C2 C3
A 1 1
B 2 0
C 2 0
I want to have a third column combining column2 of both tables base on column1 which has a value of A, B, C. Then if it's null in either table the value should be 0 Please see third sample for sample result.
So you want to match records from both tables on the basis of key column COLUMN1. If a record exists in only one table display that. COLUMN3 indicates whether the key exists in both tables.
This solution uses FULL OUTER JOIN, so it will work whether a record exists in T1 but not T2 or in T2 but not T1. The coalesce() function displays the first non-null argument.
SELECT coalesce(t1.COLUMN1, t2.COLUMN1) as COLUMN1
, coalesce(t1.COLUMN2, t2.COLUMN2) as COLUMN2
, case when t1.COLUMN1 is not null
and t2.COLUMN1 is not null then 1 else 0 end as COLUMN3
FROM TABLE1 t1
full outer join TABLE2 t2
on t1.COLUMN1 = t2.COLUMN1
Assumption. This query ignores the scenario where t1.COLUMN1 = t2.COLUMN1 but t1.COLUMN2 != t2.COLUMN2. It will just show t1.COLUMN2 in the result set. If this is not the outcome you desire please **edit your question ** to include more sample data and the full required output.
To avoid confusion, lets say table_1 has 2 columns(C1,C2) and table_2 has 2 columns(C3,C4). I just renamed column 1 & 2 of table_2 to column 3 & 4.
From What I understood from your question you want all records of table_2 in the result along with a new column which contains values from table_1 based c3 column.
The requires Table_1 right outer join Table_2 with NVL to display 0 against which value is missing in table_1 (B & C)
Full Query is as follows
SELECT Y.COLUMN_3, Y.COLUMN_4, NVL (X.COLUMN_2, 0)
FROM TABLE_1 X RIGHT OUTER JOIN TABLE_2 Y ON (X.COLUMN_1 = Y.COLUMN_3);
Hope this answers your query. Please mark the answer accepted if this solves your problem.
Please try this code...
select dbo.Table_2.C1,
dbo.Table_2.C2,
[C3] = (select Case when dbo.Table_1.C2 = dbo.Table_2.C2 then 1 else 0 end)
from dbo.Table_2
left join dbo.Table_1 on dbo.Table_1.C1 = dbo.Table_2.C1
This is what you must be looking for
SELECT tbl1.C1,
tbl2.C2,
[C3] =
(
SELECT CASE
WHEN tbl1.C2 Is Null OR tbl2.C2 is null
THEN 0
ELSE 1
END
)
FROM tbl1
INNER JOIN tbl2 ON tbl1.C1 = tbl2.C1;
Try This
select table2.c1,
table2.c2,
case when table1.c2 is null or
table2.c2 is null
then 0 else 1 end c3
from table1,table2 where table1.c1(+)=table2.c1;

Checking the same values in two different columns of same table and then compare in sql

cid ctypeid tid check asscid ci
19149 6 2 0 NULL 0
253440 1 1 0 22297922 1
1361285 5 2 0 NULL 1
22297922 2 1 1 NULL NULL
49821961 5 1 0 NULL 1
I have to check whether asscid i.e 22297922 is in the cid column which is there actually.
So I have to compare when the asscid is in the cid and then get the value of ci in case of asscid (where cid is 253440)which is 1 in this case and then assign the same value 1 of ci to cid column 22297922 which is null in this case.
Use an update with a self join.
Select:
SELECT t.asscid, t2.cid, t.ci, t2.ci --We will next update t2.ci with t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
Update:
UPDATE t2
SET t2.ci = t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
Update with condition: (So that only rows that have different ci are updated)
UPDATE t2
SET t2.ci = t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
and t.ci <> t2.ci
Select Assid from emp A
Where exists ( select 1 from Emp B where a.assid=b.Cis )

SQL Server - Set a variable if a value from one table exists in another

I have tables that are set up as shown below:
Table 1:
ID Name Date
1 a 2000-01-01
2 b 2001-01-01
3 c 2002-01-01`
Table 2:
ID Name
2 b
3 c
I would like to return all in Table 1 and then have a column that will hold a variable of either 'Yes' or 'No', based on whether they exist in Table 2, as shown below.
As shown:
Results:
Name Date Yes/No
a 2000-01-01 No
b 2001-01-01 Yes
c 2002-01-01 Yes
I have:
DECLARE #boolean as varchar(10)
IF EXISTS(
SELECT ID FROM Table 2
)
SET #boolean = 'Yes'
ELSE SET #boolean = 'No'
SELECT Name, Date, #boolean as 'Yes/No'
FROM Table 1
LEFT JOIN Table 2 u ON Table 1.ID = Table 2.ID
However, this returns the results as shown below:
Name Date Yes/No
a 2000-01-01 Yes
b 2001-01-01 Yes
c 2002-01-01 Yes
Any ideas on how to manipulate this query to return what is expected?
One option would be to LEFT JOIN the first table to the second one, and then check each record in the first table to see whether its ID matched anything in the second table.
SELECT t1.Name,
t1.Date,
CASE WHEN t2.ID IS NOT NULL THEN 'Yes' ELSE 'No' END AS [Yes/No]
FROM table1 t1
LEFT JOIN table2 t2
ON t1.ID = t2.ID
Demo here:
Rextester
By using LEFT JOIN and checking using CASE if there is no matching result on the second table (t2.ID IS NULL), you can easily get what you need as below:
SELECT Name, Date, CASE WHEN t2.ID IS NULL THEN 'No' ELSE 'Yes' END AS [Yes/No]
FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.ID = t2.ID
If a left join might return multiple unwanted results you can use outer apply() instead:
select t1.*, [Yes/No]=coalesce(x.Yes,'No')
from table_1 t1
outer apply (
select Yes='Yes'
from table_2 t2
where t1.id = t2.id
) x
Select * , case when exists(select 1 from Table2 as b where b.ID = a.ID and b.Name = a.Name) then 'Yes' else 'No' end as 'Yes/No'
From table1 as a
Your query:
DECLARE #boolean as varchar(10)
IF EXISTS(
SELECT ID FROM Table 2
)
SET #boolean = 'Yes'
will always return true because you only check whether ID exists (which always exists records in table2), you forgot to check that should be the ID exists in table1, and you probably do not do in that way, because the indicator is a dynamic value.

SQL update a table

TABLE 1
id name uf ibge
1 GOIANIA 'GO' null
2 BRASILIA 'DF' null
3 TOCANTINS 'TO' null
TABLE 2
id name uf ibge
1 GOI**Â**NIA 'GO' 5208707
2 BRAS**Í**LIA 'DF' 5300108
3 TOCANTINOPOLIS 'TO' 1721208
I need to update in table 1, the field ibge using some kind of like in the where clause to relate the description of the city with ibge code.
Could anyone help? Very grateful!
Try this...
UPDATE T1
SET T1.ibge = T2.ibge
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.id = T2.id
Assuming you can join both tables on the uf field, like so:
UPDATE Table1
SET ibge = Table2.ibge
FROM
Table2
WHERE
Table1.uf = Table2.uf;
Fiddle here
UPDATE table1 tab1 SET ibge = (SELECT ibge FROM table2 tab2 WHERE tab1.name like (substring(tab2.name, 1, 3) || '%') LIMIT 1);
Assuming that the first 3 characters are the same in field name for table 1 and table 2