I am trying to add a column, YES/No, based on text involved. If the Value column contains BALL, we mark it as Yes in the column. But if the 'BALL' is attached with any text/string, it should return it as 'NO'. There are some cases which I have attached
How do I form the case statement so that if any text/string is attached to the 'BALL' without a space should be No, and rest all the cases should be 'Yes'. I tried using like %BALL%, but it does not satisfy all the examples in the above screenshot.
Just insist that the previous and next characters are not letters:
(case when value regexp '([^a-zA-Z]|^)BALL([a-zA-Z]|$)'
then 'YES' else 'NO'
end)
Here is a db<>fiddle.
Set #val variable with value need to lookup:
SET #val := 'BALL';
Run query with two type of checking:
Use LOCATE to find #val value in the table; will return numerical position of the first character of the searched value.
Use SUBSTRING to get two sets of value from string_val column; using the numerical position that was obtained using LOCATE:
val1 will return the value of string_val before the searched value of #val (the LOCATE result need to have a deduction of 1).
val2 will return the value of string_val that matches #val and anything after it.
In the outer query, chk1 checks the last character extracted to val1 against alphabets using REGEXP. In this case GOBALL will return true (1) while 9232BALL and 9232BALLV will return false (0). Here we'll look at whatever false.
chk2 checks if val2 matches the searched #val. Therefore, the separated value of 9232BALL which end up getting BALL for val2 will return true (1) while 9232BALLV which end up getting BALLV for val2 will return false (0). Here we'll look at whatever is true.
The last filter is checking the addition of chk1+chk2. The result we're looking for is 1 because chk1 need to be false (0) and chk2 need to be true (1).
SELECT String_val AS 'Value',
CASE WHEN chk1+chk2=1 THEN 'Yes' ELSE 'No' END AS 'Yes/No'
FROM
(SELECT *,
RIGHT(val1,1) REGEXP '[a-zA-Z]' AS chk1,
val2=#val AS chk2
FROM
(SELECT string_val,
SUBSTRING(string_val,1,LOCATE(#val,string_val)-1) val1,
SUBSTRING(string_val,LOCATE(#val,string_val)) val2
FROM mytable) A) B
Alternative option 1:
SELECT string_val,
CASE WHEN
REGEXP_REPLACE(CASE WHEN val1 REGEXP '[a-zA-Z]$' = 1
THEN CONCAT(val1,val2) ELSE val2 END,'[0-9]','')=#Val
THEN 'Yes' ELSE 'No' END AS 'Yes/No'
FROM
(SELECT string_val,
SUBSTRING(string_val,1,LOCATE(#val,string_val)-1) val1,
SUBSTRING(string_val,LOCATE(#val,string_val)) val2
FROM mytable) A;
Alternative option 2:
My further testing shows that it's possible to get the result using REGEXP_SUBSTR with a much shorter query:
SET #val := 'BALL';
SELECT string_val,
REGEXP_SUBSTR(string_val, '[a-zA-Z]+[BALL]+[a-zA-Z]') AS vals,
IF(((SELECT vals))=#val,'YES','NO') AS 'Yes/No'
FROM mytable;
Demo
Related
I have table as data_attributes with a column data_type
SELECT * FROM DATA_ATTRIBUTES;
DATA_TYPE
----------
NAME
MOBILE
ETHINICITY
CC_INFO
BANK_INFO
ADDRESS
Bank_info, CC_info classified as Risk1,
Mobile, Ethinicity classified as Risk2,
Name, Address classified as Risk3
I should get the Risk classification as output,
For eg: If any of the row contains Risk1 type then output should be Risk1,
else if any of the row contains Risk2 type then output should be Risk2,
else if any of the row contains Risk3 type then output should be Risk3
I wrote below query for this
SELECT COALESCE(COL1,COL2,COL3) FROM
(SELECT
CASE WHEN DATATYPE IN ('BANK_INFO','CC_INFO') THEN 'RISK1' ELSE NULL END AS COL1,
CASE WHEN DATATYPE IN ('MOBILE','ETHINICITY') THEN 'RISK2' ELSE NULL END AS COL2,
CASE WHEN DATATYPE IN ('NAME','ADDRESS') THEN 'RISK3' ELSE NULL END AS COL3
FROM DEMO.TPA_CLASS1) A;
The required output is: Risk1 ( Only 1 value )
Please give some idea to achieve this.
You can use conditional aggregation:
SELECT
CASE
WHEN MAX(DATATYPE IN ('BANK_INFO','CC_INFO')) = 1 THEN 'RISK1'
WHEN MAX(DATATYPE IN ('MOBILE','ETHINICITY')) = 1 THEN 'RISK2'
WHEN MAX(DATATYPE IN ('NAME','ADDRESS')) = 1 THEN 'RISK3'
END AS RISK
FROM DEMO.TPA_CLASS
Take a look at the following query. How do you display Column 'Action' as text.
If the result of 'Action' is LEQ 0 then dipslay the text "Crash" and if 'Action'
is GRT 0 display the text "Hold"?
SELECT col1 AS Action
FROM vdk
WHERE t_stamp Between "{StartTime}" AND "{EndTime}"
Refactoring the answer above, since i don't see the necessity to add a query to an alias table. I think this should work the other answer should work too btw, but its a little more complicated query for no given reason.
SELECT (CASE WHEN col1 <= 0 THEN 'Crash' ELSE 'Hold' END) AS Action
FROM vdk
WHERE t_stamp Between "{StartTime}" AND "{EndTime}"
Use CASE WHEN ... ELSE ... END and select from your set (query):
SELECT *, (CASE WHEN Action <= 0 THEN 'Crash' ELSE 'Hold' END) as ActionText
FROM (
SELECT col1 AS Action
FROM vdk
WHERE t_stamp Between "{StartTime}" AND "{EndTime}"
) q
This application is similar to my first question and I thought it might help someone else down the the road.
User can select from a Table's Drop Down List a set of options to enter a value into the Database.
Using Ignition's Power Table Component's Extension Function configureEditor with the following script.
This script sets up the Drop Down List.
if colName == 'Action':
return {options': [(0, 'Null'), (1, 'HOLD'), (2, 'CRASH')]}
Along with the same Power Table's Extension Function onCellEdited script.
This script enters the selection as a value into the database.
#onCellEdited Upadte Query
row = rowIndex
col = colIndex
colName = colName
value = newValue
ndx = self.data.getValueAt(row,0)
query = "UPDATE vdk SET %s = ? WHERE ndx = ?" % colName
system.db.runPrepUpdate(query,[value,ndx],'history')
system.db.refresh(self.data)
According to the docs a if works as follows:
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3 [...].
Here is a random fiddle that represents my problem: http://sqlfiddle.com/#!9/8076e2/1
Basically what I'm trying to do is the following:
SELECT IF(whatever, 1, 2) FROM testing WHERE whatever = 'abc';
Since there is a record there that matches my WHERE clause it basically means that whatever won't be 0 or NULL like specified in the docs. Then why am I getting expression3 as result?
expr1 is intended to be a boolean expression, or at least a numeric value, not a string.
You can use true and false, or 1 and 0, etc. Technically any non-zero numeric value is interpreted as true.
Since you are using the string 'abc' as expr1, MySQL is implicitly converting that to the number 0, which represents false.
In order to return one result for a non-empty string, and another result for empty string or null, you can use a query like this:
SELECT if((whatever is not null and whatever != ''), 1, 2)
FROM testing
WHERE whatever = 'abc';
You can also do the same thing with CASE if you want to follow the SQL standard:
SELECT (case when (whatever is not null and whatever != '') then 1 else 2 end)
FROM testing
WHERE whatever = 'abc';
SELECT IF(COUNT(whatever) > 0, 1, 2) FROM testing WHERE whatever = 'abc';
I am trying to display a field value based on the value of field and then find a external table record.
can I do it?
SELECT
CASE
WHEN (dsp_notes IS NOT NULL) THEN '*'
WHEN (dsp_notes IS NULL) THEN ''
ELSE ''
END,
CASE
WHEN (dsp_priority = '1') THEN [SELECT uvi_value FROM PUB.universalinfo WHERE uvi_key = 'DSP01SHORT']
Is this possible?
Yes. This is called a scalar subquery and it needs to return one column and one row:
(CASE WHEN dsp_priority = '1'
THEN (SELECT ui.uvi_value FROM PUB.universalinfo ui WHERE ui.uvi_key = 'DSP01SHORT')
END) as NewCol
I strongly encourage you to use table aliases on your column references.
Strangely i could not find this on the web,
I need an OR within a when clause in a case statement, something like:
SELECT type,color,brand,CASE type WHEN "foo" OR "BAR" OR "somethingelse" THEN SELECT ... FROM .... WHERE .... > ....;
I just cant find a way to make it work, i have tried enclosing it in parenthesys:
SELECT type,color,brand,CASE type WHEN ("foo" OR "BAR" OR "somethingelse") THEN SELECT ... FROM .... WHERE .... > ....;
Or in square brackets:
SELECT type,color,brand,CASE type WHEN ["foo" OR "BAR" OR "somethingelse"] THEN SELECT ... FROM .... WHERE .... > ....;
The problem is that i have more groups of options to check, and make a single WHEN for each of them, will make my query super long.
Nothing, any advice?
case has two forms Only one can use Or
case x when val1 then result1 when val2 then result2 else defaultResult end
case when x = val1 then result1 When y = val2 then result2 Else DefaultResult end,
or, more generally:
case when [boolExp1] then reslt1 when [boolExp2] then reslt2 else defltReslt end
the second form can use Or, or any other Boolean operator for that matter (except Exists as I recall)
case when x = val1 or y = val2 or z in (val3, val4, val5) then result1
else defaultResult end
In CASE you cannot start a separate SELECT without brackets ... and the OR is possible with the following:
CASE WHEN type IN ("foo","BAR","somethingelse") THEN ... ELSE END
If you want to get the target value from a select call (subquery) then do this in the THEN section
... THEN ( SELECT col1 FROM tble WHERE ... ) ELSE END