Updating multiple columns using Case - sql

I have a query that looks like below:
UPDATE some_table
SET column_x = CASE WHEN x_specific_condition THEN new_value_for_x ELSE column_x END,
column_y = CASE WHEN y_specific_condition THEN new_value_for_y ELSE column_y END,
WHERE some_more_conditions
Problem with above is, each column (x, y) still gets updated with their own value if some_more_conditions return true irrespective of their specific conditions returning true. I tried removing ELSE from above, but no luck
I am combining as some_more_conditions are same for both cases, I think its better to perform all in 1 update (suggestions welcome)
Do you know if there is a way that I can perform the above update in 1 query by skipping the individual columns where the specific conditions do not match (basically avoid overwriting of same values)

To do this in one update, you would need to expand the where clause:
UPDATE some_table
SET column_x = CASE WHEN x_specific_condition THEN new_value_for_x ELSE column_x END,
column_y = CASE WHEN y_specific_condition THEN new_value_for_y ELSE column_y END,
WHERE some_more_conditions AND
(x_specific_condition OR
y_specific_condition
);
An alternative is to use multiple updates, but that could be more expensive:
UPDATE some_table
SET column_x = new_value_for_x
WHERE some_more_conditions AND x_specific_condition;
UPDATE some_table
SET column_y = new_value_for_y
WHERE some_more_conditions AND y_specific_condition;

update [table_name]
set [column_name_1] = case when [column_name_1] = [condition_1] then [condition_2]
else [column_name_1]
end,
[column_name_2] = case when [column_name_2] = [condition_1] then [condition_2]
else [column_name_2]
end,
where condition;
Note: [table_name] is your table name, [column_name] is your column name in which you want to update values.
you can update values without where condition as well.
find screenshot

Related

use Case in Where Clause SQL

Hi to make this short.
I need only the column when a condition is met.
if the condition is not met, then I can proceed running my Query without that column as validation.
CONDITION NEEDED.
REFERENCE TABLE - TABLE_A
inside TABLE_A there is PARAMETER_NAME AND PARAMETER_VALUE
REQUIRED: PARAMETER_NAME= 'SMS_SEND'
PARAMETER_VALUE = 'TRUE'
COLUMN FOR VALIDATION
INSIDE TABLE_B
INVALID_STAT ---> COLUMN
IF PARAMETER_NAME= 'SMS_SEND' and PARAMETER_VALUE = 'TRUE'
THEN I NEED TO CHECK INVALID_STAT IF ITS EQUAL TO 'Y'
ELSE
I DONT NEED TO VALIDATE INVALID_STAT AT ALL
WHERE (CASE
WHEN 1 = (SELECT DECODE (PARAMETER_VALUE, 'FALSE', 1, 2)
FROM TABLE_A
WHERE PARAMETER_NAME = 'SMS_SEND')
THEN
INVALID_STAT
ELSE
'Y'
END) = 'Y'
based upon your commentary, this will get you what you have asked for.
However, I doubt this is what you need. Please update your question as requested in the commentary above. If this does answer your question, please accept it as the answer and close the question.
I expect that the real requirement will be to wrap the following query in an outer join to a larger query (which you have not supplied) along with a coalesce to deal with the possible no rows scenario.
select
case
when PARAMETER_VALUE = 'FALSE' THEN 'Y'
else ''
end as SMS_Send_Ind
from Table_A
where PARAMETER_Name = 'SMS_SEND'
;
If there is no record in the table with Parameter_name = 'SMS_SEND', then the query will return no rows. Otherwise it will a Y or a blank as determined by the number of records in Table_A with parameter_name = 'SMS_SEND'

Update a Column with multiple values for different where conditon - Hibernate

I need to UPDATE a column in multiple rows(around 2000) with a different value for each WHERE condition. I've List of values for Where condition and another list of values to update the column.
My updates are being done as individual queries like this:
UPDATE tablename SET widget='xxx' WHERE widget='zamu';
UPDATE tablename SET widget='yyy' WHERE widget='flabu';
Is there any way to do something like this in a single query?
Thanks.
Use CASE statement and IN operator
UPDATE tablename SET widget = CASE WHEN widget='zamu' THEN 'xxx' ELSE 'yyy' END
WHERE widget IN ('zamu', 'flabu')
UPDATE tablename
SET widget = CASE
WHEN widget = 'zamu' THEN 'xxx'
ELSE 'yyy'
END
WHERE widget IN ('zamu', 'flabu')

SQL case-when-else statement efficiency

When I use this statement;
UPDATE TABLE
SET FIELD = CASE
WHEN NAME = 'a' THEN (SELECT NO FROM TABLE_TWO WHERE NAME = 'a')
ELSE 'x' END
WHERE FIELD_TWO = 1
if TABLE.NAME != 'a' will the select SQL executed nevertheless?
Moreover, a little extra question, do you think it is proper to have such logic in SQL code for any given product? I think having any logic in SQL makes its coverage very difficult, and hard to maintain, what do you think?
edit: select statement only returns a single value, ignore the case where it can return multiple values, that case is not in the scope of this question.
The Oracle manual claims that it does short-circuit evaluation:
Oracle Database uses short-circuit evaluation. For a simple CASE expression, the database evaluates each comparison_expr value only before comparing it to expr, rather than evaluating all comparison_expr values before comparing any of them with expr
In your case comparison_expr is the WHEN NAME = 'a' part and if the manual is right, the database will not run the select if name has a different value.
I think it would be easier to read and maintain, when you split it into two UPDATE-statements like this:
UPDATE TABLE SET FIELD = (SELECT TOP 1 NO FROM TABLE_TWO WHERE NAME = 'a')
WHERE FIELD_TWO = 1
AND NAME='a'
UPDATE TABLE SET FIELD = 'x'
WHERE FIELD_TWO = 1
AND NAME != 'a'
It lets you add more cases easily and you can generalize the cases if there are more of them, like:
UPDATE TABLE SET FIELD = (SELECT TOP 1 NO FROM TABLE_TWO WHERE NAME = TABLE.FIELD)
WHERE FIELD_TWO = 1
AND NAME IN ('a','b','c')
If I were you, I would use a variable so that case doesn't compute a scalar value everytime. Something like following:
DECLARE #myVar VARCHAR(10);
SELECT TOP 1 #myVar = NO FROM TABLE_TWO WHERE NAME = 'a';
UPDATE TABLE
SET FIELD = CASE
WHEN NAME = 'a' THEN #myVar
ELSE 'x' END
WHERE FIELD_TWO = 1

Can I use delete clause in Case Clause in teradata

I am newbie to teradata.
I need to delete a row once the case condition is satisfied.
Eg: case condition true delete the selected row.
Maybe I am misinterpreting what you are trying to accomplish with the CASE statement, but based on my understanding you can use the WHERE clause to conditionally remove data from a table:
DELETE
FROM MyDB.MyTable
WHERE Col1 = 31
AND "Desc" = 'xxxxxx';
EDIT:
Based on your comment then you need to apply the CASE logic to each column returned in the SELECT statement that you wish to obscure.
SELECT CASE WHEN Col1 = 31 and "DESC" = 'yyyyy'
THEN NULL
ELSE ColA
END AS ColA_,
/* Repeat for each column you wish to "delete" */
FROM MyDB.MyTable;

Populate New Column with Existing Column Based On Criteria

I am selecting into an existing column C from column A if it has a value greater than zero. If not then I want to populate it with column B. It will populate with column A only if column B is zero. If both column A and B have values it always uses B. Any help would be appreciated.
(
CASE
WHEN column A > 0 THEN column A
ELSE column B
END
)
My insert into was correct but my select had the columns reversed which was causing what I thought was an error. Thanks for all of your help. What a noob thing to do.
Your database of choice will be extremely useful, as it looks like you are already correct in most RDBMS. Just stick exactly what you have already inside of a select:
--Insert based on select
--INSERT INTO [TABLETOINSERT] (ColumnToInsert)
SELECT
CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END AS NewColumn
--Or create a table the select
--INTO [NEWTABLENAME]
FROM [TABLENAME]
Or update:
UPDATE [TABLENAME]
SET NewColumn =
CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END
Assuming you want to update the value permanently and not just change it during select, do:
update MyTable
set ColumnC = CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END