SQL Server: update five columns with one condition - sql

In a table, I have a column for currency. I have to calculate a value from two columns (ex: a, b) and update one of the five columns (ex: e, f, g, h, i, j) based on the currency.
I tried with CASE like
CASE when currency = 'INR'
then a+b
else 0
end as e
CASE when currency = 'USD'
then a+b
else 0
end as f
CASE when currency = 'CAN'
then a+b
else 0
end as g
CASE when currency = 'EUR'
then a+b
else 0
end as h
CASE when currency = 'AUD'
then a+b
else 0
end as i
Is there any other way to do this?

Update myTable
set e = CASE when currency = 'INR' then a+b ELSE 0 end,
f = CASE when currency = 'USD' then a+b ELSE 0 end,
g = CASE when currency = 'CAN' then a+b ELSE 0 end,
h = CASE when currency = 'EUR' then a+b ELSE 0 end,
i = CASE when currency = 'AUD' then a+b ELSE 0 end;

Pseudo code would be like following.
NULL / 0 values are not covered.
var result = a+b;
var columnName;
CASE
WHEN currency = 'INR' THEN columnName = e,
WHEN currency = 'USD' THEN columnName = f,
.
.
WHEN currency = 'AUD' THEN columnName = j
END
**your update statement goes here with 'columnName' variable..**

Related

How to select twice the same column on join query?

How Can I to count the gender when is M or F, somehting like
SELECT count(N.gender)
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1 and N.gender="M"
SELECT countif(N.gender = 'M') as M, countif(N.gender = 'F') as F
FROM `DATABASE_T` as T
LEFT JOIN `DATABASE_N` as N
ON
T.ENCUESTA = N.ENCUESTA AND
T.P_DEPTO = N.P_DEPTO AND
T.P_MUNIC = N.P_MUNIC AND
T.COD_VEREDA = N.COD_VEREDA AND
T.PAIS = N.PAIS and
T.UC_UO = N.UC_UO
WHERE N.ID_PROD=1
Since you did not describe the structure of your table and use Spanish-looking identifiers, I will use a clearer example with my own schema:
SELECT
SUM( CASE WHEN Sex = 'M' THEN 1 ELSE 0 END ) AS M,
SUM( CASE WHEN Sex = 'F' THEN 1 ELSE 0 END ) AS F
FROM People
WHERE People.Dept = 5

Determine type of triangle

I'm trying the hacker rank type of triangle below where based on the 3 sides it has to be determined if the triangle is equilateral, isosceles, scaelene, or not a triangle.
https://www.hackerrank.com/challenges/what-type-of-triangle/problem
I'm not sure why the code below isn't passing the test case. Unfortunately, I can't download the test case to see why it isn't working.
SELECT CASE WHEN A = B AND B = C AND A = C THEN 'Equilateral'
WHEN (A = B AND B != C AND A != C) OR (B = C AND A != B AND A != C) OR (A = C AND A != B AND B != C) THEN 'Isosceles'
WHEN ((A + B) < C) OR ((B + C) < A) OR ((C + A) < B) THEN 'Not a triangle'
ELSE 'Scalene' END
FROM Triangles
Try something like this:
SELECT
CASE
WHEN A + B > C AND A + C > B AND B + C > A THEN
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene' END
ELSE 'Not A Triangle' END
FROM TRIANGLES
Only test for the type of triangle when it is a triangle.
Try this :
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A THEN
CASE WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
WHEN A != B OR B != C OR A != C THEN 'Scalene'
END
ELSE 'Not A Triangle' END FROM TRIANGLES;
The condition for scalene triangle does not include the following condition in your original query : Side 1 + Side 2 <= Side 3

CASE WHEN THEN Cannot perform an aggregate function on an expression containing an aggregate or a subquery

this is my code:
SELECT SUM
(CASE
WHEN (dbo.EMBARQUE.EmbEst) = 0 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 3 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 6 THEN
0
WHEN (dbo.EMBARQUE.EmbEst) = 7 THEN
CASE
WHEN (SELECT COUNT (dbo.CUMPLIDO.CumpCod) from dbo.CUMPLIDO where dbo.CUMPLIDO.EmbCod = dbo.EMBARQUE.EmbCod and dbo.CUMPLIDO.CumpVol = 0) > 0 THEN
0
ELSE
dbo.EMBARQUE.EmbVol
END
ELSE
dbo.EMBARQUE.EmbVol
END) FROM dbo.EMBARQUE
You have a subquery inside sum(). That isn't allowed. This version uses a subquery to calculate the flag you want and then moves the filtering logic to the WHERE clause:
SELECT SUM(e.EmbVol)
FROM (SELECT e.*,
(CASE WHEN EXISTS (SELECT 1 FROM dbo.CUMPLIDO c WHERE c.EmbCod = e.EmbCod AND c.CumpVol = 0)
THEN 1 ELSE 0
END) as is_CumpVol_0
FROM dbo.EMBARQUE e
WHERE e.EmbEst NOT IN (0, 3, 6)
) e
WHERE e.EmbEst <> 7 OR is_CumpVol_0 = 0;

How to add a column in a sql query with selection of the result

I have a sql statment like this:
SELECT a.link_id, a.Speed_Limit_1, a.Speed_Limit_2, a.speed_limit_source, g.autos, g.trucks, g.motorcycles, 'vehicleType' as vehicleType
FROM g, f, d, a
WHERE f.access_id = g.access_id
AND d.condition_id = f.condition_id
AND g.access_id = a.access_id
result:
link_id......autos...trucks...motorcycles.........vehicleTypes
1..........|.......Y....|....Y......|.....N.................|.............?
2..........|.......Y....|....Y......|.....Y.................|.............?
3..........|.......Y....|....Y......|.....N.................|.............?
4..........|.......Y....|....N......|.....Y.................|.............?
I will add the column "vehicleTypes. Inside this column should be:
0 if autos, trucks and motorcycles are allowed (e.g. row 2)
1 if only cars are allowed
2 if only trucks are allowed
3 if only motorcycles are allowed
How can I do this?
This should do it.
I added an ELSE -1 to indicate where it didnt match any of the criteria.
SELECT
a.link_id,
a.Speed_Limit_1,
a.Speed_Limit_2,
a.speed_limit_source,
g.autos, g.trucks,
g.motorcycles,
CASE
WHEN g.autos = 'Y' AND g.trucks = 'Y' AND g.motorcycles = 'Y' THEN 0
WHEN g.autos = 'Y' AND g.trucks = 'N' AND g.motorcycles = 'N' THEN 1
WHEN g.autos = 'N' AND g.trucks = 'Y' AND g.motorcycles = 'N' THEN 2
WHEN g.autos = 'N' AND g.trucks = 'N' AND g.motorcycles = 'Y' THEN 3
ELSE -1
END as vehicleTypes
FROM g, f, d, a
WHERE f.access_id = g.access_id
AND d.condition_id = f.condition_id
AND g.access_id = a.access_id
You should use CASE WHEN block
for example in Oracle:
https://www.techonthenet.com/oracle/functions/case.php

Where Clause Case Statement; ORA-00907: missing parenthesis

I have a query and when I run it I get the error message ORA-00907: missing parenthesis. When I replace the CASE statement with either x = g and or y = g and it runs as expected.
SELECT *
FROM
table1,
table2,
table3,
table4,
table5,
table6,
table7,
table8
WHERE
a = b and
c = d and
e = d and
CASE strfldvar
WHEN 'BROKEN_ARROW' THEN (x = g)
WHEN 'BROKEN_BOX' THEN (y = g)
ELSE -1
end
and
f = h and
i = j
What am I doing wrong here?
case is an expression, not a predicate (i.e condition) : it 'returns' a typed value and can not contain predicate as result (in the then parts). In your case (assuming else -1 means 'no match') :
AND g = CASE strfldvar
WHEN 'BROKEN_ARROW' THEN x
WHEN 'BROKEN_BOX' THEN y
ELSE NULL -- never match, even if g is null
END
although I think it would be simpler to replace it with :
AND (
(strfldvar = 'BROKEN_ARROW' AND x = g)
OR (strfldvar = 'BROKEN_BOX' AND y = g)
)
Instead of
CASE strfldvar
WHEN 'BROKEN_ARROW' THEN (x = g)
WHEN 'BROKEN_BOX' THEN (y = g)
ELSE -1
have this:
x=CASE strfldvar WHEN 'BROKEN_ARROW' THEN g ELSE x END
y=CASE strfldvar WHEN 'BROKEN_ARROW' THEN g ELSE y END
I would replace
CASE strfldvar
WHEN 'BROKEN_ARROW' THEN (x = g)
WHEN 'BROKEN_BOX' THEN (y = g)
ELSE -1
by
(CASE WHEN strfldvar = 'BROKEN_ARROW' and x = g then 1
WHEN strfldvar = 'BROKEN_BOX' and y = g then 1
ELSE -1
END) = 1