I have two tables t1 and t2. I need to join them and set the blocked column value in t1 as 'X' when t2.inact = '' (is empty) and t2.objval = 1000. Both the tables are joined by obid.
Table t1:
obid
blocked
1
2
Table t2:
obid
inact
objval
1
1000
2
2000
Expected output: The table t1 should now look like this
obid
blocked
1
X
2
In bigquery it is said that it is not possible to use WITH CTE's along with update statement which was my first try..What could be the other way possible? Below is my another SQL attempt with CASE and this is creating a new column called blocked...but the requirement is filling the data in already present column blocked.
WITH abc AS(
SELECT obid,blocked
FROM table1),
def AS (
SELECT obid,inact,objval,
FROM table2
WHERE objval = '1000')
SELECT CASE WHEN t2.inact = '' THEN 'X'
ELSE '' END as blocked
FROM abc t1
JOIN def t2
ON t2.obid = t1.obid
Any help appreciated!!!
You can still use an UPDATE statement on the "t1" table, while checking conditions on the "t2" table, thus simulating a join between "t1" and "t2".
UPDATE t1
SET blocked = 'X'
FROM t2
WHERE t1.obid = t2.obid AND t2.inact = '' AND t2.objval = 1000
Related
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
I am trying to update a column from table1 based off of data from two other tables.
Table 1 has columns id, columnIWantToUpdate, table2FK, table3FK
Table 2 has columns table2FK, table2_unique_id
Table 3 has columns table3FK, table3_unique_id
So I get table2_unique_id and table3_unique_id as inputs and I want to use the columns table2FK and table3FK to update table 1 based off of the unique_ids I received as input
One method uses filtering in the where clause:
update table1
set columnIWantToUpdate = ?
where exists (select 1
from table2 t2
where t2.table2FK = table1.table2FK
) or
exists (select 1
from table3 t3
where t3.table3FK = table1.table3FK
);
It is not clear if you want and and or for the conditions.
How to exclude multiple rows if one of the rows meets the condition
example table:
id/role_id/code
1/1/112233
1/2/221155
1/3/332233
5/1/323233
5/3/988933
6/1/389349
6/2/112233
6/3/232323
Now I want to find only these id-s, which has role_id=3 and exclude all rows, if one of them contains code=112233
In this example, results should show only this row: 5/3/988933
Gordon showed you how to do it via EXISTS and mentions IN but it can also be done via a left self join as follows:
SELECT t1.*
FROM
Table t1
LEFT JOIN Table t2
ON t1.id = t2.id
AND t2.code = 112233
WHERE
t1.role_id = 3
AND t2.id IS NULL
And while I don't like to use IN because it was mentioned here is how you could do it:
SELECT *
FROM
Table
WHERE
role_id = 3
AND id NOT IN (SELECT ID
FROM
Table
WHERE
code = 112233
and ID IS NOT NULL)
Note I include the line ID IS NOT NULL because if you ever compare something to IN (NULL) you will not get your desired result. In this case an ID field is not likely to ever be null so you can probably remove that statement, I put it in to show the nuance.
If you want the original rows, then use exists or in:
select t.*
from t
where role_id = 3 and
exists (select 1 from t t2 where t2.id = t.id and t2.code = 112233);
i have two temp tabels like this:
t1 t2
id a b c id a b c
1 250
2 251
3 .
. .
.
250
251
.
.
id 250 from t2 is equal with id 1 from t1
id 251 from t2 is equal
with id 2 from t1 and so on.
how can i achieve something like this?
select id, a, b, c
from #t1
join #t2
on #t1.id - 249 = #t2.id
I need this because i need to perform calculations, example:
(the value from a with the id 250 from t1 - the value from a with id 1
from t1 ) * 100 in a new column
Thanks
You've got it right except for resolving ambiguity in your query. See below syntax
select
t1.*,
t2.*,
(t2.a-t1.a)*100 as result
from #t1 t1
join #t2 t2
on t1.id - 249 = t2.id
further based on your comment to question
I have 1 table and i perform two different queries to create 2 temp
tables. From there you know the story. I want to insert the result
into another temp table.
I'd suggest that instead of creating temp tables used the select queries as inner queries in join like below so that problems with temp tables being invisible at a part is not a hinderance. This saves on memory as well.
select
t1.*,
t2.*,
(t2.a-t1.a)*100 as result
from
(
select id,a,b,c from orig_table
-- where ....
) t1
join
(
select id,a,b,c from orig_table
-- where ....
) t2
on t1.id - 249 = t2.id
update
Do you have any idea how i could write this for 12 different cases?
insetead copy pasting the query 12 times. example: case when a like
'e1m' or 'e2m', or 'ey1' and so on, 12 in total
Based on asker's comment I'd suggest that following query is a better approach:
select
id as orig_id,
a,
b,
case when c like 'c1' then c else Null end as t1c,
case when c like 'c1' then id-249 else Null end as t1id,
case when c like 'c2' then c else Null end as t2c,
case when c like 'c1' then id-249*2 else Null end as t2id
from orig_table
Put this as inner query and we can quickly do all calculations over this inner query result set instead of all those joins
demo Sql fiddle link here:http://sqlfiddle.com/#!6/b7aee/4
This will work:
select * from #t1 JOIN #t2 ON (#t2.id -249)= #t1.id
If you have Problem with expireed sessions, please try to use global temps with ##
if that is not working try to manualy create table in the Folder
YourDB\Databse\System Databases\Tempdb
Table variables (DECLARE #t TABLE) are visible only to the connection that creates it, and are deleted when the batch or stored procedure ends.
Local temporary tables (CREATE TABLE #t) are visible only to the
connection that creates it, and are deleted when the connection is
closed.
Global temporary tables (CREATE TABLE ##t) are visible to everyone,
and are deleted when all connections that have referenced them have closed.
Tempdb permanent tables (USE tempdb CREATE TABLE t) are visible to
everyone, and are deleted when the server is restarted.
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);