Facing problem with case when statement in a single row fetch for oracle sql - sql

I have a single row of data as follows:
dyn1 dyn2 dyn3 chg
1 0 1 768
Now i want to write a case condition like
Case when dyn1 = 1 then 7 When dyn2=1 then 7 When dyn3=1 then 7 End
Now for this above records it is not checking the dyn3 value as it is getting dyn1 value as true.
How to handle this code?

I'll suppose that you need to return those values in a select statement, maybe this could be helpful to you.
SELECT CASE WHEN dyn1 = 1 THEN 7 END,
CASE WHEN dyn2 = 1 THEN 7 END,
CASE WHEN dyn3 = 1 THEN 7 END
FROM (SELECT 1 AS dyn1, 1 AS dyn2, 1 AS dyn3 FROM dual);
Or, maybe you want it in an only column
SELECT CASE WHEN dyn1 = 1 THEN 7 END ||
CASE WHEN dyn2 = 1 THEN 7 END ||
CASE WHEN dyn3 = 1 THEN 7 END
from (select 1 as dyn1, 1 as dyn2, 1 as dyn3 from dual);

Related

How to reset running counts and start from 1 based on the condition and make previous row values as 0 in SQL?

For example there is some table with below data:
No Id Value
1 100 1
2 100 0
3 100 1
4 100 2
1 101 1
2 101 2
1 102 0
2 102 1
I have to write SQL query, which will return row count based on specific condition. If the value matches 0 then need to reset running counts and start from 1 and make previous row values as 0
So the result will be like:
No Id Value Running Count
1 100 1 0
2 100 0 0
3 100 1 1
4 100 1 2
1 101 1 1
2 101 2 2
1 102 1 0
2 102 0 0
Your sample dataset is quite limited so I'm not sure of all edge cases but see if the following works for you. If not it might help get you there.
This gets a running count using a window & case expression and uses lead to check the next value.
If the current value or next value is 0 the count is 0, otherwise it's the running count subtracting 1 if there is a 0 in the Id block indicating the count was reset.
select No, Id, Value,
case when value = 0 or nv = 0
then 0
else
rc - case when Min(value) over(partition by id) = 0 then 1 else 0 end
end Running_Count
from (
select *,
Sum(case when value = 0 then 0 else 1 end) over(partition by id order by no) rc,
Lead(Value) over(partition by Id order by No)nv
from t
)t;

return value when count equals variable in sql server

I have a table to update with other table data. For this I created a trigger. Inside trigger, I must check how many active occurrences of each id. If this number of occurrences is same number than a variable value then return 1 (true) otherwise 0 (false).
I get the variable
DECLARE #num_gerencias numeric(2, 0)
SELECT #num_gerencias = (SELECT COUNT(id_gerencia) FROM FR_GERENCIES)
select #num_gerencias
This works ok... returns 5
Then, I make a count of occurrences of the l_activo variable in other table (variable is a bit):
SELECT id_operacio, SUM(CASE WHEN l_activo = 1 THEN 1 ELSE 0 END)
FROM FR_GERENCIES_OPERACIONS o
GROUP BY o.id_operacio
This query also works nice, returns:
2958 5
2959 0
2960 5
2961 3
2962 5
2963 5
2964 2
2965 4
2966 5
2967 5
All perfect... now i must get same list, but if sum equals to #num_gerencias, then put 1 and 0 otherwise.
Expected result table
2958 1
2959 0
2960 1
2961 0
2962 1
2963 1
2964 0
2965 0
2966 1
2967 1
I've tried with CASE
SELECT DISTINCT id_operacio, CASE WHEN
(
SELECT SUM(CASE WHEN l_activo = 1 THEN 1 ELSE 0 END)
FROM FR_GERENCIES_OPERACIONS o
GROUP BY o.id_operacio
) = #num_gerencias THEN 1 ELSE 0 END
but I'm getting error:
Mens . 512 , Level 16 , State 1, Line 6
The subquery returned more than one value , which is not correct when it goes below = , ! = , <, < = ,>, > = Or when used as an expression .
I also tried with an IF (i guess this option is totally wrong for this case... but I've tried)
SELECT DISTINCT id_operacio,
IF #num_gerencias = (SELECT SUM(CASE WHEN l_activo = 1 THEN 1 ELSE 0 END)
FROM FR_GERENCIES_OPERACIONS o
GROUP BY o.id_operacio)
1
ELSE 0
FROM FR_GERENCIES_OPERACIONS
But I have syntax errors...
Any idea how can i reach expected result table?
You were nearly there, however your grouping and selection must occur outside of your case statement:
SELECT DISTINCT
id_operacio
,CASE
WHEN SUM(CAST(l_activo AS INTEGER)) = #num_gerencias THEN 1
ELSE 0
END
FROM FR_GERENCIES_OPERACIONS o
GROUP BY o.id_operacio

Nested Case in SQL 2000

I have the following SQL statement, when executing it on SQL 2008 it works perfectly with no problem. But when excuted in SQL 2000 it says:
Internal SQL Server error
I think the problem comes because of using nested case statement in SQL 2000. Here is the SQL statement:
WHEN 14 THEN
Case When (select COUNT(*) from [dbo].[mnrFnBI_Fixed](#CurrencyID) f where f.BAccCustID!=0x0 AND f.biMatID = bi.biMatID)>0 THEN
CASE (select top 1 PriceType from (select top 1 f.BDate,f.BNumber , tc.PriceType From [dbo].[mnrFnBI_Fixed](#CurrencyID) f inner join #tcust tc on tc.AccID = f.BAccCustID AND tc.PriceType!=0 Where f.biMatID =bi.biMatID AND f.BAccCustID != 0x0 order by f.BDate desc,f.BNumber desc ) as k) -- Price From customer Card
WHEN 1 THEN
CASE #UseUnit WHEN 1 THEN mtEndUser1
WHEN 2 THEN mtEndUser2
WHEN 3 THEN mtEndUser3
WHEN 4 THEN mtEndUser4
WHEN 5 THEN mtEndUser5
WHEN 0 THEN CASE [mtDefUnit] WHEN 1 THEN mtEndUser1
WHEN 2 THEN mtEndUser2
WHEN 3 THEN mtEndUser3
WHEN 4 THEN mtEndUser4
WHEN 5 THEN mtEndUser5
END
END
---*******************---
WHEN 2 THEN
CASE #UseUnit WHEN 1 THEN mtWhole1
WHEN 2 THEN mtWhole2
WHEN 3 THEN mtWhole3
WHEN 4 THEN mtWhole4
WHEN 5 THEN mtWhole5
WHEN 0 THEN CASE [mtDefUnit] WHEN 1 THEN mtWhole1
WHEN 2 THEN mtWhole2
WHEN 3 THEN mtWhole3
WHEN 4 THEN mtWhole4
WHEN 5 THEN mtWhole5
END
END
---*******************---
END
END
When I remove the part between the '*****' it works perfectly !
Please tell me how case statement works with SQL 2000 ?!
Update :
when change the above code to :
WHEN 14 THEN
Case
WHEN (select COUNT(*) from [dbo].[mnrFnBI_Fixed](#CurrencyID) f where f.BAccCustID!=0x0 AND f.biMatID = bi.biMatID)>0 THEN
case
when (select top 1 PriceType from (select top 1 f.BDate,f.BNumber , tc.PriceType From [dbo].[mnrFnBI_Fixed](#CurrencyID) f inner join #tcust tc on tc.AccID = f.BAccCustID AND tc.PriceType!=0 Where f.biMatID =bi.biMatID AND f.BAccCustID != 0x0 order by f.BDate desc,f.BNumber desc ) as k) =1 THEN
CASE #UseUnit WHEN 1 THEN mtEndUser1
WHEN 2 THEN mtEndUser2
WHEN 3 THEN mtEndUser3
WHEN 4 THEN mtEndUser4
WHEN 5 THEN mtEndUser5
WHEN 0 THEN CASE [mtDefUnit] WHEN 1 THEN mtEndUser1
WHEN 2 THEN mtEndUser2
WHEN 3 THEN mtEndUser3
WHEN 4 THEN mtEndUser4
WHEN 5 THEN mtEndUser5
END
END
when(select top 1 PriceType from (select top 1 f.BDate,f.BNumber , tc.PriceType From [dbo].[mnrFnBI_Fixed](#CurrencyID) f inner join #tcust tc on tc.AccID = f.BAccCustID AND tc.PriceType!=0 Where f.biMatID =bi.biMatID AND f.BAccCustID != 0x0 order by f.BDate desc,f.BNumber desc ) as k) = 2 THEN
CASE #UseUnit WHEN 1 THEN mtWhole1
WHEN 2 THEN mtWhole2
WHEN 3 THEN mtWhole3
WHEN 4 THEN mtWhole4
WHEN 5 THEN mtWhole5
WHEN 0 THEN CASE [mtDefUnit] WHEN 1 THEN mtWhole1
WHEN 2 THEN mtWhole2
WHEN 3 THEN mtWhole3
WHEN 4 THEN mtWhole4
WHEN 5 THEN mtWhole5
END
END
END
END
it works , so please tell what's wrong with the first Sql Commands in sql 2000 ?!
This example can Help You.
UPDATE table_name
SET column_name=CASE
WHEN column_name in ('value1', 'value2',.....)
THEN 'update_value'
WHEN column_name in ('value1', 'value2',.....)
THEN 'update_value'
END

Counting similar values on same row, different columns

I want to write a query that will count how many times a certain value appears on each row - on the full table I have more columns and I need to count how many times "9" (for example) appears in all the columns,
In this case the answer would be 3:
PlayerName Nation Kills Deaths SoloKills PartyKills
666 1 9 0 9 9
select PlayerName
, sum(case when Nation = 9 then 1 else 0 end +
case when Kills = 9 then 1 else 0 end +
case when Deaths = 9 then 1 else 0 end +
...
) as SumOfNines
from YourTable
group by
PlayerName

Retrieve rows which satisfy at least four of given seven conditions

I am having a table called as 'Alphabets' with columns named from 'A to G'.
Table Name: Alphabets
Column Names: A | B | C | D | E | F | G
Now, I need a SQL query to retrieve all rows from the table which satisfy at least four criteria from the following list:
A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
G = 7
I am using Oracle 10g database.
SELECT * FROM Alphabets where
CASE WHEN A=1 THEN 1 ELSE 0 END +
CASE WHEN B=2 THEN 1 ELSE 0 END +
CASE WHEN C=3 THEN 1 ELSE 0 END +
CASE WHEN D=4 THEN 1 ELSE 0 END +
CASE WHEN E=5 THEN 1 ELSE 0 END +
CASE WHEN F=6 THEN 1 ELSE 0 END +
CASE WHEN G=7 THEN 1 ELSE 0 END >= 4
But something about this table and query seem suspect - It feels like it should really be a table of two columns, Letter and Value say.
One ugly solution I could think of (I'm sure there must be better solutions, but this one should work, though I'm not conviced it is optimal in any sense...)
SELECT *
FROM Alphabets
WHERE
CASE WHEN A=1 THEN 1 ELSE 0 END +
CASE WHEN B=2 THEN 1 ELSE 0 END +
CASE WHEN C=3 THEN 1 ELSE 0 END +
CASE WHEN D=4 THEN 1 ELSE 0 END +
CASE WHEN E=5 THEN 1 ELSE 0 END +
CASE WHEN F=6 THEN 1 ELSE 0 END +
CASE WHEN G=7 THEN 1 ELSE 0 END BETWEEN 4 AND 7
This has problems:
doesn't scale well - new constraint - new line in query...
performance wise it is not optimal
also just plain horribly ugly