I have two columns and I want to compare the values in each row and create a third column that will tell me true or false (or 0/1) like the below example.
Col1 Col2 Col3
24 24 true
45 45 true
56 54 false
78 98 false
Personally a CASE expression is probably the easiest and simplest way to do this.
SELECT col1,
col2,
CASE
WHEN col1 = col2 THEN 'True'
ELSE 'False'
END AS Is_A_Match
This should work for you:
select col1, col2, case when col1 = col2 then 'true' else 'false' end as col3
If you want to add a column into the table, then you can use a computed column:
alter table t add col3 as (case when col2 = col3 then 1 else 0 end)
If the columns can have NULL values and you want to treat those as equal, then the logic should take this into account.
This column is then available when referring to the table.
If you just want the result in a query, use a case expression as described in several other answers.
Simply use a case expression.
case when col1 = col2 then 'true' else 'false'
or in bit form
case when col1 = col2 then 1 else 0
Related
I am trying to return all Col1 values IF the respective Col2 value = NULL
I have thought of IF Col2 = NULL Return Col2 END IF, But I am unsure of how to format this, because when I run it I get errors saying invalid relational operator. Thank you for your time.
SELECT Col1
FROM mytable
WHERE IF NULL Col2 = 0;
IF Col2 = NULL
RETURN COL1
ELSE
END IF;
================================================================
EDIT
I apologize for the lack of information/Show of research. First post, So I have been searching google for different IF then statements to deal with NULL values. Everything I have found this far has only showed how to Ignore/convert to 0 so that you can easily work with them. Nothing on how to use them to return values from another col1
use COALESCE. it resturns the first non null value
SELECT
COALESCE(col1,col2) AS FINAL_COLUMN
FROM [TABLE NAME]
SELECT Col1
FROM mytable
WHERE Col2 IS NULL
No IF is required. WHERE is the SQL IF for row selection. This returns only the rows where Col2 is null.
If you want to return all the rows, but want to return Col1 when Col2 is null and NULL otherwise, use
SELECT
CASE WHEN Col2 IS NULL THEN Col1 ELSE NULL END as myResultColumn
FROM mytable
I have a dataset similar to this;
col1 col2 col3
1 YES NO
2 NO YES
I am trying to apply case statement,
case when col1 = 1 then col2
when col1 = 2 then col3 end as newcol
Now the newcol will have values as YES/NO in the output. Is it possible to apply another case inside the above case condition, So that I can hardcode YES as Y and NO as N.
I have got the result by adding a case statement in the an outer query. Is there any alternate approach like nested case.
Also can I apply case condition using the column alias newcol?
You can add a case expression around your case, like this:
case (
case
when col1 = 1 then col2
when col1 = 2 then col3
end
) when 'YES' then 'Y' else 'N' end as newcol
SUBSTR would pick the first character of YES or NO without a conditional:
SUBSTR(
case
when col1 = 1 then col2
when col1 = 2 then col3
end
, 1
, 1
) as newcol
Table structure
id col1 col2
1 data1 false
2 data2 true
I tried
select id, case col2 when true then col1 from table
and it is showing an internal server error. I want to select the col1 from table when the corresponding col2 is true.
probably just a simple syntax error in your case statment.
try this..
select
id,
case when col2=1 then col1 else 'some other value' end as computedCaseCol
from table1
see: SQL fiddle
Do you mean that ?
select id,col1 from tabe where col2 = 'true'
or
select id,col1 from tabe where col2 is true
??
My table in Oracle is like this
Col Col1 Col2 Col3 ColR
-- ---- ---- ---- ----
30 73 40 null -10
60 32 null 20 40
90 80 null null 10
80 45 81 30 50
I can also set 0 instead of null in the above column.
I need to find the min value from Col1,Col2,Col3 ignoring null or 0 and populate the ColR by subtracting from Col.
EDIT:
i wrote a CASE statement which doesn't work due to the null values inside my table.
SELECT col,col1,col2,col3,
CASE
WHEN Col1 < Col2 AND Col1 < Col3
THEN Col - Col1
WHEN Col2 < Col1 AND Col2 < Col3
THEN Col - Col2
ELSE Col - Col3
END ColR
FROM
(SELECT col,col1,
CASE
WHEN col22 IS NULL
THEN NULL( i can also SET TO 0 but it will mess WITH my other calculation TO find MIN)
ELSE ROUND( 100* ( (col22) / (col44)))
END col2 ,
CASE
WHEN col33 IS NULL
THEN NULL
ELSE ROUND( 100* ( (col33) / (col44)))
END col3
FROM TABLE
)
I have just included the case statement inside my select query. all the the column values all populated from another query.
It sounds like you want something like
SELECT least( (case when col1 is null or col1 = 0 then 999999999 else col1 end),
(case when col2 is null or col2 = 0 then 999999999 else col2 end),
(case when col3 is null or col3 = 0 then 999999999 else col3 end) )
FROM <<table name>>
where 999999999 is some numeric value that is large enough that it will always be larger than any other valid value. If it is possible that all three columns will have NULL or 0 values, then you'd probably want to add an additional check that if the result of that least function is 999999999 that you return 0 or NULL or whatever else makes sense.
#X-Zero was kind enough to put together a working SQL Fiddle example of this construct. Note that his example is filtering out the rows where all three columns have either NULL or 0 values.
// IF YOU NEED MINIMAL FROM COL1 or COL (ANY COLUMN)
SELECT MIN (COL1) FROM (SELECT * FROM TABLE WHERE COL1 IS NOT NULL)
Can you please elaborate I am not able to help you with this small set of info actually.
Oracle NVL Usage:
nvl(check_expression, replacement_value)
So
nvl(col2,0) ought to take of nulls that mess with your math.
So try:
CASE
WHEN nvl(col1,0) < nvl(col2,0) AND nvl(col1,0) < nvl(col3,0)
THEN Col - nvl(col1,0)
WHEN nvl(col2,0) < nvl(col1,0) AND nvl(col2,0) < nvl(col3,0)
THEN Col - nvl(col2,0)
ELSE Col - nvl(col3,0)
END ColR
EDIT: Taking X-Zero's point which I missed. I think if you replace the NULLS with 9999999 instead of 0, the logic will work, although that may be too specific to this sample data and not a real world solution.
If you want to ignore nulls in a column, you could wrap them with the NVL function. This replaces null values in a column with the value specified, for which you could use some large number. For example:
NVL(col1,99999)
Oracle Database SQL Reference - NVL: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions105.htm
I am using SQL Server and I have to group by a few columns but only if a setting is true.
DECLARE #setting tinyint
SET #setting = 0
SELECT col1 FROM table1
GROUP BY col1,
CASE WHEN #setting = 1 THEN col2 ELSE NULL END
OR should I default to col1 that I always use to group by if the setting is set, so the code would would be
CASE WHEN #test = 1 THEN col2 ELSE col1 END
It does actually work to use the NULL but I can't find an example on the internet to prove that it is correct usage.
SELECT col1 FROM table1
GROUP BY col1,
CASE WHEN #setting = 1 THEN col2 ELSE NULL END
Your usage is correct. You already know it works.
ELSE NULL is implied in CASE, so you could have written it as
GROUP BY col1, CASE WHEN #setting = 1 THEN col2 END
You could try to be coy with variants like
GROUP BY col1, #setting * col2 --for numeric col2
GROUP BY col1, COALESCE(NULLIF(#setting,1), col2)
But the CASE statement actually unrolls to a better, simpler plan.
DECLARE #setting tinyint
SET #setting = 0
SELECT col1 FROM table1
GROUP BY col1,
CASE WHEN #setting = 1 THEN col2 ELSE NULL END
your first example should work.
For further clarification, do you want to group by col1, col2 or group by col1 when #setting = 1?
edit: Your first example is correct.