I'm getting: Subquery returned more than 1 value, but I don't have a subquery - sql

UPDATE P
SET NRI_1 = (WI_1 * .8)
FROM AC_Property P
WHERE COALESCE(WI_1,0) <> 0 and RSV_CAT = 'PDNP'

Try with this:
UPDATE AC_Property
SET NRI_1 = (WI_1 * .8)
WHERE COALESCE(WI_1,0) <> 0 and RSV_CAT = 'PDNP'
You don't need to FROM because you are using the same table.
Also if you have a Trigger it will cause the error, because you update more than one (1) row.

Why not just use ?
UPDATE AC_Property
SET NRI_1 = (WI_1 * .8)
WHERE COALESCE(WI_1,0) <> 0 and RSV_CAT = 'PDNP'

Related

SQL update to make a toggle

I want to make a toggle request as defined there T-SQL: Using a CASE in an UPDATE statement to update certain columns depending on a condition
I did this :
Update capteur
join smartparking_reference on(smartparking_reference.id_capteur = capteur.id_capteur)
set (CASE WHEN capteur.valeur != 0 then capteur.valeur = 0 and last_value_date = now() END)
where smartparking_reference.id_ref = 3;
But always a syntax error. So what did i miss ?
Apparently you want to update capteur.valeur only when it is nonzero and set it to zero. Try this simpler statement:
MySql style
Update capteur
join smartparking_reference on(smartparking_reference.id_capteur = capteur.id_capteur)
set valeur = case when valeur = 0 then 1 else 0 end, last_value_date = now()
where smartparking_reference.id_ref = 3;
Sql Server style
Update capteur
set valeur = case when valeur = 0 then 1 else 0 end, last_value_date = getdate()
from smartparking_reference
where smartparking_reference.id_capteur = capteur.id_capteur
and smartparking_reference.id_ref = 3;
I also guess your update statement has the wrong syntax
UPDATE c
SET valeur = 0, last_value_date = now()
FROM capteur c
INNER JOIN smartparking_reference AS sr ON
sr.id_capteur = c.id_capteur
WHERE sr.id_ref = 3 and c.valeur != 0
UPDATE capteur
SET capteur.valeur=(
CASE
WHEN capteur.valeur != 0 THEN
0 ELSE 'another value'
END
),last_value_date = now ()
FROM capteur
INNER JOIN smartparking_reference ON
smartparking_reference.id_capteur = capteur.id_capteur
WHERE
smartparking_reference.id_ref = 3;

Rewrite subquery with calculation in SQL to CASE statement

I have, as a part of a bigger query, some subqueries that I would like to convert to CASE statements instead.
The subquery looks like this (and works):
(SELECT (((SUM(DAm)-(SUM(StcCst)*-1))*100)/NULLIF(SUM(DAm),0)) AS 'DG' FROM [F0001].[dbo].[ProdTr] WHERE AcYrPr = '201601' AND ProdTr.TrTp = 1 AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1
However, I don't seem to find any logical way to put this into a CASE-statement.
Any help would be appreciated!
(
SELECT CASE
WHEN SUM(t1.DAm) <> 0
THEN (SUM(t1.DAm) + SUM(t1.StcCst)) * 100 / SUM(t1.DAm)
ELSE 0 /* or whatever you want to have in this case */
END AS 'DG'
FROM [F0001].[dbo].[ProdTr] t1
WHERE t1.AcYrPr = '201601' AND
t1.TrTp = 1 AND
t1.CustNo = '12773'
) AS dg_period_1
I also removed some unneeded parentheses and simplified an operation (x - (y * -1) = x + y)
You could use the following statement with CASE provided you want to return a null when SUM(DAm) is null or 0.
(SELECT CASE
WHEN SUM(DAm) IS NOT NULL and SUM(DAm) <> 0 THEN (((SUM(DAm) - (SUM(StcCst) * -1)) * 100) /SUM(DAm))
ELSE NULL
END AS 'DG'
FROM [F0001].[dbo].[ProdTr]
WHERE AcYrPr = '201601'
AND ProdTr.TrTp = 1
AND [F0001].[dbo].[ProdTr].CustNo = '12773') AS dg_period_1

update not working

UPDATE dwh.product_in_offer AS t
SET (t.PRODUCT_BUCKET_TYPE_CODE,t.PRODUCT_BUCKET_TYPE_DESC,t.LAST_EDIT_TYPE,t.CREATE_ID,t.UPDATE_ID) =
(SELECT s.PRODUCT_BUCKET_TYPE_CODE, s.PRODUCT_BUCKET_TYPE_DESC, s.LAST_EDIT_TYPE, s.CREATE_ID, s.UPDATE_ID
FROM dwh.product_in_offer_vw AS s
WHERE create_id = 0
AND t.PRODUCT_KEY = s.PRODUCT_KEY
AND t.OFFER_KEY=s.OFFER_KEY)
INNER JOIN dwh.product_in_offer_vw p on t.PRODUCT_KEY=p.PRODUCT_KEY and t.OFFER_KEY=p.OFFER_KEY
WHERE create_id = 0;
ERROR:Syntax error near INNER, offset 386 "..ND t.OFFER_KEY=s.OFFER_KEY) -->INNER<--"
Any idea ??
The INNER JOIN clause has to go before the SET clause.
UPDATE dwh.product_in_offer AS t
INNER JOIN dwh.product_in_offer_vw p on t.PRODUCT_KEY=p.PRODUCT_KEY and t.OFFER_KEY=p.OFFER_KEY
SET (t.PRODUCT_BUCKET_TYPE_CODE,t.PRODUCT_BUCKET_TYPE_DESC,t.LAST_EDIT_TYPE,t.CREATE_ID,t.UPDATE_ID) =
(SELECT s.PRODUCT_BUCKET_TYPE_CODE, s.PRODUCT_BUCKET_TYPE_DESC, s.LAST_EDIT_TYPE, s.CREATE_ID, s.UPDATE_ID
FROM dwh.product_in_offer_vw AS s
WHERE create_id = 0
AND t.PRODUCT_KEY = s.PRODUCT_KEY
AND t.OFFER_KEY=s.OFFER_KEY)
WHERE create_id = 0;

SQL Server : IF Condition in WHERE clause

I have a bit parameter
#IsInRetry
If it is false I have to set the where condition to
attempts = 0
else if it is true I have to set the where condition to
attempts > 0
How could this be done?
Try this way:
( (#IsInRetry = 0 and attempts = 0) or (#IsInRetry = 1 and attempts > 0) )
Try this:
WHERE (attempts = #IsInRetry or (#IsInRetry = 1 and attempts > 0))
Try this, should work also for negative attempts :-)
#IsInRetry = (attempts > 0)
Greetings.
This:
if #IsInRetry = 0x0
BEGIN
SELECT * FROM dbo.tbl
WHERE attempts = 0
END
ELSE
BEGIN
SELECT * FROM dbo.tbl
WHERE attempts = 1
END

Conditional operator in Transact-sql

Is there a way to do this shorter, for instance using some sort of conditional operator in Transact-sql?
IF #ParentBinaryAssetStructureId = -1
BEGIN
SET #ParentBinaryAssetStructureId = NULL
END
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId = #ParentBinaryAssetStructureId
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
USE NULLIF()
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId = NULLIF(#ParentBinaryAssetStructureId,-1)
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
The ternary (conditional) operator in c like languages:
x = doSomething ? 5 : 7
would be written like this in SQL:
SELECT #x = CASE WHEN #doSomething = 1 THEN 5 ELSE 0 END
There can be multiple cases (when clauses):
SELECT #x = CASE WHEN #doSomething = 1 THEN 5 WHEN #somethingElse = 1 THEN 20 ELSE 0 END
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId =
CASE ParentBinaryAssetStructureId
WHEN -1 THEN NULL
ELSE ParentBinaryAssetStructureId
END
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
Give that a whirl