Set the value in temp table while checking two columns - sql

I'm using SQL Server: I need to update a column(phone number) in temp table based on checking two column values one after another. Here is my current version of code: Basically what it does is, it sets the value of column in the temp table based on where it match the join condition.
update r
set t.phone_number = tb.phone_number
from #temptable t
inner join phone_number_records tb
on t.id = tb.id
and 1 = tb.is_this_valid
Now I need to check another column value (ready_to_accept_new) in phone_number_records table and update the phone_number field in temp table as per ready_to_accept_new value. If ready_to_accept_new is equal to "1" and "id" of temptable matches with "id" of phone_number_records, i need to set phone_number value in the temptable with the matching record phone_number(in phone_number_records table). If there are no matching records on this criteria, we need to update the temp table record as previous.( from matching is_this_valid column value "1" record).
Can someone please let me know how to solve this one? Thanks in advance!

In addition to the answer supplied by John, it can be done with IF..ELSE block
if exists(select * from #temptable t
inner join phone_number_records tb
on t.id = tb.id
and 1 = tb.ready_to_accept_new )
update r
set t.phone_number = tb.phone_number
from #temptable t
inner join phone_number_records tb
on t.id = tb.id
and 1 = tb.ready_to_accept_new
else
update r
set t.phone_number = tb.phone_number
from #temptable t
inner join phone_number_records tb
on t.id = tb.id
and 1 = tb.is_this_valid

Quickest way is to join to your table twice.
Use a left join for the per ready_to_accept_new value join and then use a case statement to see if this exists.
IE:
update r
set t.phone_number =
CASE
WHEN tb_1.id IS NOT NULL THEN tb_1.phone_number
ELSE tb.phone_number
END
from #temptable t
inner join phone_number_records tb
on t.id = tb.id
and 1 = tb.is_this_valid
left join phone_number_records tb_1
on t.id = tb.id
and 1 = tb.per ready_to_accept_new value
apologies for the formatting!

Related

Update column in SQL table with values from another row in same table

I have a table with 5 columns (name, record, un, svun, svrecord):
Name
idrecord
un
svun
svrecord
John Doe
JD123
johndoe
NULL
JM123
Jane Doe
JaD123
janedoe
NULL
OR123
Olive Err
OR123
oliverr
NULL
GH123
I'm trying to populate the svun column with the value from the idrecord column when the svrecord matches an idrecord from another row.
For instance, row #2 should update the svun column to OR123 because the svrecord (OR123) matches the idrecord (OR123) from the table. Hope this makes sense.
I've started trying to come up with a query for this below but can't quite figure it out...I'm sure i'm missing a parameter to make this work or maybe an additional select statement...:
UPDATE table
SET svun = idrecord
WHERE (svrecord = idrecord)
First, take a look at update joins here: https://www.sqlservertutorial.net/sql-server-basics/sql-server-update-join/
You will see this example:
UPDATE
t1
SET
t1.c1 = t2.c2,
t1.c2 = expression,
...
FROM
t1
[INNER | LEFT] JOIN t2 ON join_predicate
WHERE
where_predicate;
Now, we need to apply this for your table:
UPDATE
t1
SET
t1.svun = t1.idrecord
FROM
yourtable t1
JOIN
yourtable t2
ON
t1.svrecord = t2.idrecort
Your initial query will only update svun = idrecord where svrecord = idrecord in the same row.
UPDATE table SET svun = idrecord WHERE (svrecord = idrecord)
In order to update records based on values that match in different rows you'll have to use a self join
with new_results as (
select a.Name, a.svun, a.idrecord
from table a
inner join table b
where a.svrecord = b.idrecord
)
update new_results set svun = idrecord where svun is null
I don't recommend running this code without testing, but this should get you started
Update table1
SET table1.svun = table2.idrecord
FROM table table1
INNER JOIN table table2
ON table1.svrecord = table2.idrecord
You could join the table to itself.
UPDATE YourTable
SET a.svun = b.idrecord
FROM YourTable a
INNER JOIN YourTable b
ON a.svrecord = b.idrecord

How to display the records but show the value as 0 in the result if the condition is not true SQL

I have a query like this
SELECT
TA.MOBILE_NUMBER,
TA.AMOUNT,
TA.REFERENCEID,
TB.KEYCOST
FROM TABLEA TA
LEFT JOIN TABLEB TB ON TA.REFERENCEID = TB.REFERENCEID
WHERE TA.STATUS = 0
AND TB.ALIAS = 'KEYCOST'
Some referenceid's tableA have an entry with ALIAS = 'KEYCOST' in tableb but some referenceid's doesn't have any entry with ALIAS = 'KEYCOST'.
I want to display all the referenceid's from tableA with the KEYCOST they have in TableB. If a referenceid in TableA doesn't have any KEYCOST, then the result should have that data but display 0 in the KEYCOST column.
Please if someone could help me here.
thank you
Move the join condition to ON statement, as for NULL values simple NVL should help, it will show the second value if the first one is null. Like this:
SELECT TA.MOBILE_NUMBER, TA.AMOUNT, TA.REFERENCEID, NVL(TB.KEYCOST,0) KEYCOST FROM TABLEA TA
LEFT JOIN TABLEB TB
ON TA.REFERENCEID = TB.REFERENCEID AND TB.ALIAS = 'KEYCOST'
WHERE TA.STATUS = 0
When using LEFT JOIN, conditions on the second table need to go into the ON clause:
SELECT TA.MOBILE_NUMBER, TA.AMOUNT, TA.REFERENCEID, TB.KEYCOST
FROM TABLEA TA LEFT JOIN
TABLEB TB
ON TA.REFERENCEID = TB.REFERENCEID AND TB.ALIAS = 'KEYCOST'
WHERE TA.STATUS = 0;
Otherwise, the NULL values produced by the LEFT JOIN get filtered out.
If you need to get 0 instead of NULL, then use COALESCE(). It is not clear which column you are referring to.
If you really want to display all the referenceid's from tableA with the KEYCOST they have in TableB. If a referenceid in TableA doesn't have any KEYCOST, then the result should have that data but display 0 in the KEYCOST column.
SELECT
TA.MOBILE_NUMBER,
TA.AMOUNT,
TA.REFERENCEID,
NVL(TB.KEYCOST, 0) KEYCOST
FROM TABLEA TA
LEFT JOIN TABLEB TB ON TA.REFERENCEID = TB.REFERENCEID
AND TB.ALIAS = TA.ALIAS
WHERE TA.STATUS = 0
AND TA.ALIAS = 'KEYCOST'
According to you explanation, you want to SELECT data with ALIAS = KEYCOST so we have to keep condition for tableA also and add AND in join with ALIAS column.

When using UPDATE and SET with SQL rows appear to be missing

When I run the query below :
SELECT COUNT(x.objectID)
FROM db0..table0 as t
INNER JOIN db1..table1 as x ON t.objID = x.slaveID
INNER JOIN db1..table2 as table2 ON table2.sourceID = x.objectID
WHERE (****)
I get 268'466 results. However when I update and add a column to db0..table0 with x.objectID as follows, I get 145'346 of these items into my db0.table0
ALTER TABLE db0..table0 ADD new_objID bigint;
UPDATE db0..table0
SET db0..table0.new_objID = x.objectID
FROM db0..table0 as t
INNER JOIN db1..table1 as x ON t.objID = x.slaveID
INNER JOIN db1..table2 as table2 ON table2.sourceID = x.objectID
WHERE (****)
Can anyone see what is going wrong? The only difference between the queries is the first line in the first query is replaced with the first two lines in the second query.
To count the number of new values that end up in my table I use,
SELECT COUNT(new_objID)
FROM db0..table0
This should return all the none NULL instances of new_objID.
EDIT
So the table structures are
table0
table0_ID
table1
table1_ID
other_table1_ID
value
table0 and table1 are linked by table0_ID and table1_ID in a many to one relationship. One table0_ID corresponds to many table1_ID. I realised that table2 was no longer necessary - in the past I wanted information from this table but not any longer.
Effectively all I am trying to do is add the other_table1_ID entry, which corresponds to the smallest entry of value for each group of table1_ID into table0.
The issue is the discrepancy between these queries suggest I am doing something wrong I just can't work out what.
QUERY ONE
SELECT COUNT(table1.table1_ID)
FROM db0..table0 as table0
INNER JOIN db1..table1 as table1
ON table0.table0_ID = table1.table1_ID
WHERE table1.value IN (SELECT MIN(value)
FROM db1..table1 as new_table1
WHERE new_table1.table1_ID = table1.table1_ID)
QUERY TWO
ALTER TABLE db0..table0 ADD newID bigint
UPDATE db0..table0
SET db0..table0.newID = table1.other_table1_ID
FROM db0..table0 as table0
INNER JOIN db1..table1 as table1
ON table0.table0_ID = table1.table1_ID
WHERE table1.value IN (SELECT MIN(value)
FROM db1..table1 as new_table1
WHERE new_table1.table1_ID = table1.table1_ID)
UPDATE: after some discussion and question update by OP we came to the conclusion that conditions in both queries should be changed to the following:
new_table1.table1_ID = table1.table1_ID should instead be table0.table0_ID = new_table1.table1_ID
then both SELECT queries (original and the one which counts the newID field) return same count of 206146 records.
In the first query you do COUNT(x.objectID) but in the UPDATE call you SET db0..table0.new_objID = x.objID .
Notice, different column names: x.objectID in the first case and x.objID in the second.
Change your second query to the following:
UPDATE db0..table0
SET db0..table0.new_objID = x.objectID
FROM db0..table0 as t
INNER JOIN db1..table1 as x
ON t.objID = x.slaveID
INNER JOIN db1..table2 as table2
ON table2.sourceID = x.objectID
WHERE (****)

Postgresql Update using Inner Join set

I have two tables that they share two fields (myfield1, myfield2) and one of the two tables has 2 other fields of interest.
Here is what I want to do:
1. Inner Join the two tables
2. Update a column (field1) in Table2 with either fields (afield or anotherfield) from the other table depending on afield is null or not.
The code below runs fine but the targeted set field (field1) doesn't get updated with anything.
Update Table2
Set field1 = (
CASE
WHEN os.afield is not null
THEN (os.afield)
Else os.anotherfield
End
)
from Table1 os
inner join Table2 fd
ON fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2;
Update Table2 fd
Set fd.field1 =
(select CASE WHEN os.afield is not null THEN (os.afield) Else os.anotherfield End
from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2);
It's called a correlated subquery which is executed for each row in Table2. But you must be sure that subquery returns single or zero rows.
This query will update all rows in Table2 if you want to update only those rows which exist in Table1 you need a WHERE
Update Table2 fd
Set fd.field1 =
(select CASE WHEN os.afield is not null THEN (os.afield) Else os.anotherfield End
from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2)
where exists (
select 1 from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2);

how to update specific row using inner join?

I need to update a specific row in a table, to select this row i need to use inner join, I wrote this:
UPDATE Items
SET [Seq] = '0'
WHERE EXISTS
(SELECT *
FROM Items
inner join Dictionary on items.Member = Dictionary.Member WHERE
Items.ID = '1' and Items.Member ='23')
All rows in Items table were updated, not the specific row (the select statement works fine and I get the row I need)
Do I miss something?
Sql Server
UPDATE Items
SET Items.[Seq] = '0'
FROM Items inner join Dictionary
on items.Member = Dictionary.Member
WHERE Items.ID = '1' and Items.Member ='23'
Mysql
UPDATE Items
INNER JOIN Dictionary
ON items.Member = Dictionary.Member
SET Items.[Seq] = '0'
WHERE Items.ID = '1' and Items.Member ='23'
In Sql Server the format is:
Update a
set field1 = b.field2
from table1 a
join table2 b on a.id = b.table1id
where b.somefield = 'test'