Update Multiple ID's based on field value - sql

I want to find all lines of data that have a value, say 'N/A'
I want to then update ALL the lines of data where they have the same CommonID into the NewDataHere Column so in the example below ID 1 and 2 would be affected
I am doing this in a stored procedure
Code for far:
DECLARE #sqlPRatingChange NVARCHAR(MAX)
SET #sqlPRatingChange = 'UPDATE TblAsbestos SET NewDataHere= ''NA''
WHERE (SELECT ID FROM TBLASBESTOS WHERE VALUE= ''N/A'')'''
EXEC (#sqlPRatingChange)
Example of Database as it should look after code
ID-VALUE-NewDataHere
1-N/A-N/A
2-C-N/A
2-N/A-N/A
1-A-N/A
3-B-''

Why are you using dynamic SQL? Your query seems close enough. It just needs a correlation clause:
UPDATE TblAsbestos
SET NewDataHere = 'NA'
WHERE (SELECT ID FROM TBLASBESTOS a2 WHERE VALUE = 'N/A' AND TblAsbestos.id = a2.id);
In SQL Server, I might be inclined to do this using a CTE and window functions:
with toupdate as (
select a.*,
sum(case when value = 'N/A' then 1 else 0 end) over (partition by id) as numNAs
from TblAsbestos a
)
update toupdate
set NewDataHere = 'NA'
where numNAs > 0;

This should possibly be a comment on Gordon's answer, but it got too long and the formatting that an answer affords helps...
The incorrect syntax error is because in SQL Server you cannot alias the target of an update statement (not to be confused with using an alias as the target which is allowed). The where clause is also incorrect, it appears to be mix between an EXISTS clause and an IN clause, but has ended up as neither. I think you need:
UPDATE TblAsbestos
SET NewDataHere = 'NA'
WHERE EXISTS
( SELECT 1
FROM TblAsbestos AS a
WHERE a.Value = 'N/A'
AND a.id = TblAsbestos.id
);
Or
UPDATE TblAsbestos
SET NewDataHere = 'NA'
WHERE ID IN (SELECT a.ID FROM TblAsbestos AS a WHERE a.Value = 'N/A');

Try this. Just replace table1 with your table name :
update a
set NewDataHere = 'N/A'
from table1 as a
where exists(select 1 from table1 as b where a.id = b.id and b.value = 'N/A')

Try this:
UPDATE TblAsbestos a
SET NewDataHere = 'NA'
WHERE Exists (SELECT Top 1 ID
FROM TBLASBESTOS a2
WHERE Upper(value) = 'N/A'
AND a.id = a2.id);

Related

Select statement in Case statement in Oracle

SELECT (CASE WHEN T.ID = ( SELECT cte.REFERENCE FROM trans cte WHERE T.ID
= CTE.PARENT_ID) THEN cte.REFERENCE ELSE null END) AS name
FROM trans T
Example: I am picking one transaction value as an example. In trans table whose ID=1 then in the same table I need to look for PARENT_ID=1.
when I look for parent_ID=1 then it's ID value will be different.
This is not ID=Parent_ID.
once I look for parent_ID=1 then print its corresponding reference value as name.
I tried above sql statement in oracle, but it didn't work. Could you please let me know, how to write this statement in case statement.
Instead of a subquery, why not try a self-join?
SELECT CASE
WHEN nvl(t1.id,-1) = nvl(t2.reference, -1) THEN t2.reference
ELSE 1
END AS number_col
FROM trans t LEFT JOIN trans t2 ON (t.id = t2.parent_id);
You can also try it as a subquery without a case statement
SELECT t.id,
NVL ((SELECT t2.reference
FROM trans t2
WHERE t.id = t2.parent_id AND t.id = t2.reference AND ROWNUM = 1),
1) AS number_val
FROM trans t

SQL AND to see if two flags are set in two different tables?

SELECT 1 FROM
(SELECT 1 FROM mytable1 WHERE parentid = 'ID1' AND flag = 'Y') as X,
(SELECT 1 FROM mytable2 WHERE id = 'ID2' AND flag = 'Y') as Y
I'm making a query to see if two flags are set in two tables, where 'parentid' and 'id' are both primary keys. The query should return a row only if both flags are set to 'Y', or return nothing otherwise, then I do stuff with that result in my backend code.
I've tested this and it works but I feel like it looks wonky and could be optimized. Any ideas?
To get what You want:
SELECT 1
FROM mytable1 AS a, mytable2 AS b
WHERE a.parentid = 'ID1' AND a.flag = 'Y'
AND b.id = 'ID2' AND b.flag = 'Y'
But in fact, I would prefer a query with LEFT JOIN, which always gives one row, like this:
SELECT CASE WHEN a.flag = 'Y' AND b.flag = 'Y' THEN 1 ELSE 0 END AS result
FROM TABLE ( VALUES 1 ) AS always(present)
LEFT JOIN mytable1 AS a ON a.parentid = 'ID1'
LEFT JOIN mytable2 AS b ON b.id = 'ID2'
Your query is fine (although I would use CROSS JOIN. However, I would prefer a row with a specific value. I would phrase that as:
SELECT (CASE WHEN EXISTS (SELECT 1 FROM mytable1 WHERE parentid = 'ID1' AND flag = 'Y') AND
EXISTS (SELECT 1 FROM mytable2 WHERE id = 'ID2' AND flag = 'Y')
THEN 1 ELSE 0
END) as flag
You may need from dual, depending on your database.
It better to use JOIN instead of doing subqueries
SELECT mytable1.parentid, mytable2.id
FROM mytable1
JOIN mytable2 ON mytable2.flag = "Y" AND mytable1.flag = "Y"

sql case statement update based on other table

I want to update a table (table1) based on the values of one or more fields in another table (table2). I believe this should be a case statement but I'm unsure how to incorporate a case statement and an update clause based on another table in one statement. Here's what I have so far which I know does not work:
update table1 i, table2 s
set i.sales = 'F'
where s.payment = 'Y'
and i.order_no = s.order_no;
I know how to do a select based on two tables but that's not very helpful since I don't want to create a new database object - I just want to update an existing object (table1):
create or replace view merge as
select
i.order_no
, case when s.payment = 'Y'
then 'F'
end as sales
from table1 i, table2 s
where i.order_no = s.order_no;
And I know how to update WITHIN a case statement:
UPDATE table1
SET sales = (
SELECT CASE
WHEN foo = 'X'
THEN 'F'
ELSE null
END
FROM table1
)
;
I considered a where clause instead of a case statement but it ends up selecting EVERY record and the 2nd table definitely has different values in the payment field:
update t1
set sales = 'F'
where exists (select table2.payment
from table2
where table2.order_no = table1.order_no
and table2.payment = 'Y');
try this:
update table1 i
set i.sales = (select case when x.payment = 'Y'
then 'F'
else i.sales end
from table2 x
where x.order_no = i.order_no);
I don't have an oracle running right now (so I cannot check the syntax propertly), but I think you can try something like this:
update table1 i
set i.sales = 'F'
where i.order_no IN (select s.order_no from table2 s where s.payment = 'Y')
Hope it helps!

SQL Conditional filter with different values

I have found lots of posts on coniditonal filtering in the where clause, but they all seem to be based off of using the same value, such as:
WHERE (o.OrderID = #orderid OR #orderid IS NULL)
I need to do something slightly different, I need to remove a filter and its value completely base on another value, so something like:
select *
from tableA
where 1 = 1
case when a = 1 then
and b in (select b from tableB)
else
-- do nothing
end
I know that the above is not allowed, and I am just writing as an example of what I am trying to do. does Anyone have any idea of a good way to do this? I know i could use if statements and duplicate the query, but it is a large one, and i am trying to avoid that.
Thanks
SELECT *
FROM tableA
WHERE a <> 1
OR (a = 1 AND EXISTS(SELECT b from TableB WHERE tableA.b = TableB.b))
You could also write this as:
SELECT tableA.*
FROM tableA
LEFT JOIN tableB
ON tableA.b = tableB.b
WHERE tableA.a <> 1
OR (tableA.a = 1 AND tableB.b IS NOT NULL)
"Correcting" your WHERE clause:
select *
from tableA
where 'T' = case
when a = 1 then case
when b in (select b from tableB) then 'T'
end
else 'T'
end;

SQL Server: IF EXISTS ; ELSE

I have a tableA:
ID value
1 100
2 101
2 444
3 501
Also TableB
ID Code
1
2
Now I want to populate col = code of table B if there exists ID = 2 in tableA. for multiple values , get max value.
else populate it with '123'. Now here is what I used:
if exists (select MAX(value) from #A where id = 2)
BEGIN
update #B
set code = (select MAX(value) from #A where id = 2)
from #A
END
ELSE
update #B
set code = 123
from #B
I am sure there is some problem in BEGIN;END or in IF EXIST;ELSE.
Basically I want to by-pass the else part if select statement in IF-part exist and vice- versa. For example if select statement of IF=part is:
(select MAX(value) from #A where id = 4)
It should just populate 123, coz ID = 4 do not exist !
EDIT
I want to add the reason that your IF statement seems to not work. When you do an EXISTS on an aggregate, it's always going to be true. It returns a value even if the ID doesn't exist. Sure, it's NULL, but its returning it. Instead, do this:
if exists(select 1 from table where id = 4)
and you'll get to the ELSE portion of your IF statement.
Now, here's a better, set-based solution:
update b
set code = isnull(a.value, 123)
from #b b
left join (select id, max(value) from #a group by id) a
on b.id = a.id
where
b.id = yourid
This has the benefit of being able to run on the entire table rather than individual ids.
Try this:
Update TableB Set
Code = Coalesce(
(Select Max(Value)
From TableA
Where Id = b.Id), 123)
From TableB b
I know its been a while since the original post but I like using CTE's and this worked for me:
WITH cte_table_a
AS
(
SELECT [id] [id]
, MAX([value]) [value]
FROM table_a
GROUP BY [id]
)
UPDATE table_b
SET table_b.code = CASE WHEN cte_table_a.[value] IS NOT NULL THEN cte_table_a.[value] ELSE 124 END
FROM table_b
LEFT OUTER JOIN cte_table_a
ON table_b.id = cte_table_a.id