This is what I am trying to do. Basically I have some columns deliberately left blank in table Staging_X and to be updated later. I would like to update those columns using the case conditions below. I want to implement this in a stored procedure.
UPDATE Staging_X
SET Staging_X.[NoMaterial]
(SELECT (case
when ((([Up]+[Test])+[Mon])+[Down_percentage])*(1.68)=(0)
then (168) else [Lost]*(1.68)
end)
FROM Staging_X)
UPDATE Staging_X
SET [NoMaterial] =
case when [Up]+[Test]+[Mon]+[Down_percentage]=0
then 168 else [Lost]*1.68 end
WHERE [NoMaterial] is null
If I understand you correctly, you dont need a selected like that as the values are all in the same row.
So try something like
UPDATE Staging_X
SET Staging_X.[NoMaterial] =
case
when ((([Up]+[Test])+[Mon])+[Down_percentage])*(1.68)=(0)
then (168)
else [Lost]*(1.68)
end
more simply
UPDATE [Staging_X]
SET [NoMaterial] =
CASE [Up]+[Test]+[Mon]+[Down_percentage]
WHEN 0 THEN 168
ELSE [Lost] * 1.68
END
Related
Is this the most efficient way to update one single column from three different columns?
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT = ISNULL(PRINCIPAL,0)
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT = ISNULL(INTEREST,0)
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT = ISNULL(RAW_DATA_AMOUNT,0)
I am using SQL Server. Thanks!
Looks like COALESCE() would help
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT = COALESCE(ROW_DATA_AMOUNT, INTEREST, PRINCIPAL, 0)
REPORTABLE_AMOUNT will then be updated with the first value in the COALESCE() that is not null.
Please try SQL : Case - When - End if you are checking Null for update fields.
Egs:
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET REPORTABLE_AMOUNT =
case when ISNULL(PRINCIPAL,0) <> 0 then PRINCIPAL
case when ISNULL(INTEREST,0) <> 0 then INTEREST
case when ISNULL(RAW_DATA_AMOUNT,0) <> then RAW_DATA_AMOUNT
else 0 --default value
End
Regards
Abdul
IF(
[dbo.tblx.Category] = 'WS' OR [dbo.tblx.Category] = 'SEM',
0,
[dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
)
this returns the entire text and not the "calculation"
can someone help me...what am I doing wrong?
i tried
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000
END AS Metal
Do perhaps insert something before "CASE WHEN"? it is not running
As I understood, you are trying to create a computed column, using value from other table.
I think in this case you have to use a User Defined function in the formula for computed column. If you give more information I can try to write some code for you.
Instead, if you just want to add a column in a query you can use:
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblPTx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000 END AS NAME_OF_YOUR_NEW_COLUMN
Update
DECLARE #myvar INT;
SET #myvar = CASE WHEN ([dbo.tblx.Category] = WS OR [dbo.tblPTx.Category] = SEM) THEN 0
ELSE [dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
END
Is it possible to refer to another case statement name in another case statement within SQL query?
Example: I have 3 case statements. The first 2 case statements are returning values based off coded fields. My 3rd case statement I would like to refer to the ending case name to return a sum of quantity.
However, I cannot figure how to get the case statement to refer to the previous case names I created. I hope I am explaining this correctly.
Any assistance would be greatly appreciated. Please see attached image for more detail.
SELECT CI_ITEM.ITEMCODE
, CI_ITEM.ITEMCODEDESC
, CASE WHEN DATEDIFF("M",CI_ITEM.DATECREATED,GETDATE()) <60 THEN DATEDIFF("M",CI_ITEM.DATECREATED,GETDATE())
ELSE 60 END AS NO_OF_MONTHS
, CASE WHEN DATEDIFF("M",IM_ITEMTRANSACTIONHISTORY.TRANSACTIONDATE,GETDATE()) <=60
AND IM_ITEMTRANSACTIONHISTORY.TRANSACTIONCODE IN ('BI','II','SO','WI')
THEN IM_ITEMTRANSACTIONHISTORY.TRANSACTIONQTY *-1 ELSE '0' END AS QTY_CONSUMED_60_MONTHS
, CASE WHEN NO_OF_MONTHS = 0 THEN 0 ELSE SUM([QTY_CONSUMED_60_MONTHS])/ [NO_OF_MONTHS] END AS MONTHLY_AVE_ON_60MONTHS_DATA
FROM CI_ITEM
INNER JOIN IM_ITEMTRANSACTIONHISTORY ON CI_ITEM.ITEMCODE = IM_ITEMTRANSACTIONHISTORY.ITEMCODE
Simply wrap your dependent cases within a sub query and reference them as fields of the sub query result.
SELECT
*,
CASE WHEN NO_OF_MONTHS = 0 THEN 0 ELSE SUM([QTY_CONSUMED_60_MONTHS])/ [NO_OF_MONTHS] END AS MONTHLY_AVE_ON_60MONTHS_DATA
FROM
(
SELECT CI_ITEM.ITEMCODE
, CI_ITEM.ITEMCODEDESC
, CASE WHEN DATEDIFF("M",CI_ITEM.DATECREATED,GETDATE()) <60 THEN DATEDIFF("M",CI_ITEM.DATECREATED,GETDATE())
ELSE 60 END AS NO_OF_MONTHS
, CASE WHEN DATEDIFF("M",IM_ITEMTRANSACTIONHISTORY.TRANSACTIONDATE,GETDATE()) <=60
AND IM_ITEMTRANSACTIONHISTORY.TRANSACTIONCODE IN ('BI','II','SO','WI')
THEN IM_ITEMTRANSACTIONHISTORY.TRANSACTIONQTY *-1 ELSE '0' END AS QTY_CONSUMED_60_MONTHS
FROM CI_ITEM
INNER JOIN IM_ITEMTRANSACTIONHISTORY ON CI_ITEM.ITEMCODE = IM_ITEMTRANSACTIONHISTORY.ITEMCODE
)AS X
I tried searching around but couldn't find anything that would help me out.
I'm trying to do this in SQL:
DECLARE #onlyMM INT
SET #onlyMM = 1
SELECT *
FROM cdn.magnag
WHERE CASE #onlyMM
WHEN 1
THEN man_zrdtyp NOT IN (
1616
,2001
)
ELSE - 1
END
I have a problem with:
where case #onlyMM when 1 then man_zrdtyp not in (1616,2001) else -1 end
how to properly make a case for the operator not in?
I think you need to formulate the WHERE clause in a different way. You could use OR instead of case statement. Like this:
WHERE
(#onlyMM=1 AND man_zrdtyp not in (1616,2001))
OR #onlyMM<>1
You don't want to do this?
where (case when #onlyMM = 1 then man_zrdtyp else -1 end) not in (1616,2001)
I have a pretty simple question me thinks. I've been looking on the internet, but haven't been able to find anything. I am trying to add an IF statement basically to my Oracle sql.
UPDATE PS_Z_TREND_NOW_TBL a
SET STATUS = (
SELECT COUNT(SEC.IS_AW_AUTH_NAME)
FROM PS_IS_AW_SECURITY sec
WHERE sec.IS_AW_AUTH_NAME LIKE '%Manager%'
I want to update STATUS so that if COUNT(SEC.IS_AW_AUTH_NAME) is greater than 0 it will insert 'M'. How would I write this?
With Case statements.
UPDATE PS_Z_TREND_NOW_TBL a
SET STATUS = ( CASE WHEN COUNT(SEC.IS_AW_AUTH_NAME) > 0 then 'M'
ELSE null END )
FROM PS_IS_AW_SECURITY sec
WHERE sec.IS_AW_AUTH_NAME LIKE '%Manager%'