Based on one column within my query results (Value), I am trying to write an if/else statement based on the value held which will display the result the in an additional row.
For example, if I have a record of 2 within the value field, but I want to check whether it is above < 5. If the value is less than 5 I basically want the additional column to display a hardcoded value of 5, else display actual value.
Any help would be appreciated.
Use a case statement
select a.*,
case
when a.TheField < 5 then 5
else a.TheField
end as NewField
from MyTable a
You can use a case
select value, case when value < 5
then 5
else value
end as calculated_column
from your_table
Related
I'm creating a new table and carrying over several columns from a previous table. One of the new fields that I need to create is a flag that will have values 0 or 1 and value needs to be determined based on 6 previous fields in the table.
The 6 previous columns have preexisting values of 0 or 1 stored for each one. This new field needs to check whether any of the 6 columns have 1 and if so set the flag to 0. If there is 0 in all 6 fields then set itself to 1.
Hopefully this makes sense. How can I get this done in oracle? I assume a case statement and some sort of forloop?
You can use greatest() function: GREATEST
create table t_new
as
select
case when greatest(c1,c2,c3,c4,c5,c6)=1 -- at least one of them contains 1
then 0
else 1
end c_new
from t_old;
Or even shorter:
create table t_new
as
select
1-greatest(c1,c2,c3,c4,c5,c6) as c_new
from t_old;
In case of greatest = 1, (1-1)=0, otherwise (1-0)=1
You can use a virtual column with a case expression; something like:
flag number generated always as (
case when val_1 + val_2 + val_3 + val_4 + val_5 + val_6 = 0 then 1 else 0 end
) virtual
db<>fiddle
or the same thing with greatest() as #Sayan suggested.
Using a virtual column means the flag will be right for newly-inserted rows, and if any of the other values are updated; you won't have to recalculate or update the flag column manually.
I've assumed the other six columns can't be null and are constrained to only be 0 or 1, as the question suggests. If they can be null you can add nvl() or coalesce() to each term in the calculation.
I have the following table in Postgresql. The first 4 records are the base data and the others were generated with the ROLLUP function.
I want to add a column "grp_1" that will display the first non-null value of the columns grp1_l1, grp2_l2 and grp2_l3
I can get to the desired result by nesting 3 "case" functions using the SQL below, but my real table has 4 groups with each 8 to 10 columns (so a lot of nested "case" function).
sql:
SELECT grp1_l1, grp1_l2, grp1_l3, case when grp1_l1 is not null then grp1_l1 else case when grp1_l2 is not null then grp1_l2 else case when grp1_l3 is not null then grp1_l3 else null end end end as grp1, value
FROM public.query_test;
Is there a better and more scalable to handle this requirement ? Any suggestions are welcome.
The id will not always have 3 digits, that is just the case in my example here
Use coalesce() it's defined as "returns the first of its arguments that is not null" - which is exactly what you want.
coalesce(grp1_l1, grp1_l2, grp1_l3)
I am trying to sort based on below condition where I have hidden one numeric value with '-' i want to sort in a manner that however I sort (desc,asc) '-' should come on top or at the bottom but it does not affect my sorting, below goes an example
and - can be a negative or positive value
before sorting
1
2
3
-
5
6
7
after sorting
-
1
2
3
5
6
7
You can set the sort order expression to be something like
=Val(Fields!myColumn.Value)
This will return zero for the - row and the numeric value for the other rows.
If this does not help, edit the question (rather than add more comments) and show what the data looks like that comes from your dataset query in the report.
It is quite unclear what the data type of the column is, because - is not a numeric value.
However, regardless of the type, this does what you want:
order by col
If the column is a number and - is NULL, this works because an ascending sort puts NULL values first.
If the column is a string and '-' is the value, this works because '-' sorts before numbers.
Here is a db<>fiddle.
Sort using a CASE expression:
SELECT *
FROM yourTable
ORDER BY
CASE WHEN col = '-' THEN 0 ELSE 1 END,
col;
Actually, it appears that you are mixing character and numeric data in the same column. In order to bear the value -, it must be text, so we should probably do a bit extra work to make sure the text numbers sort properly:
SELECT *
FROM yourTable
ORDER BY
CASE WHEN col = '-' THEN 0 ELSE 1 END,
TRY_CONVERT(NUMERIC(10), col);
Using Microsoft SQL Server, if you use COUNT(column name) it returns the number of rows in that column which have a non-null value (i.e., it counts the rows, ignoring nulls).
Is there any way to do something similar, but allowing you to tell it which values to ignore? For example, if I wanted to count all the rows in a table which have a value which is NOT 1, I could do something like COUNTNOT(column name,1). That would count all the rows in the specified column which have a value NOT 1.
You may use conditional aggregation:
SELECT COUNT(CASE WHEN some_val <> 1 THEN 1 END) AS cnt
FROM yourTable;
The above logic is that COUNT will count one whenever some value is not equal to 1. Otherwise, it falls on the ELSE conditional, which if not present defaults to the value NULL. Since NULL is not counted, any value other than 1 would contribute zero to the count.
Why not put what you want to exclude in a WHERE clause?
SELECT COUNT(some_val) AS cnt
FROM yourTable
WHERE some_val <> 1
You need to be careful about NULL values. I would recommend:
select sum(case when column in (<values to ignore>) then 0 else 1 end)
This will count NULL values as not in the list (even if NULL is in the list). To ignore NULL values (as well), switch the logic to:
select sum(case when column not in (<values to ignore>) then 1 else 0 end)
and be sure NULL is not in the list.
I have this table
id value
1 OK
2 xminimum
3 NO
4 YES
I want to sort this table by value where minimum is always first then the rest according to alphabetic order of value column
Meaning:
xminimum
NO
OK
YES
I wrote this query:
Select *
from table_a
order by case when value='xminimum' then 1 else ????? end
I don't know what to put in the else... conceptually it should be else value end so it means alphabetic order.. but I can not combine integer with text.
How do I fix it?
As requested, copied from my comment:
Select *
from table_a
order by case when value='xminimum' then 1 else 2 end, value
Another solution:
SELECT *
FROM table_a
ORDER BY value <> 'xminimum', value;
Do it like you have and add the value column as second column to sort by:
SELECT *
FROM table_a
ORDER BY CASE WHEN value='xminimum' THEN 1 ELSE 2 END, value