So I have been facing this weird situation in mysql where somehow null values are inserted in my table.
I'm talking about null value not NULL value.
I have attached image for better understating
as you can see name column have null and mobile_no have NULL
So after using this query
select Case when t1.name IS NULL then 'NA'
when t1.name= 'NA' or 'null' or NULL then 'NA'
else t1.name end as 'Name',
Case when t1.mobile_no IS NULL then 'NA'
when t1.mobile_no= 'NA' or 'null' or NULL then 'NA'
else t1.mobile_noend as 'Mobile no' from student;
after this I'm getting this result
|Name|Mobile no|
----------------
|null|NA |
but I want below result
|Name|Mobile no|
----------------
|NA |NA |
To compare a column with multiple values use col IN (x, y, z), not col = x OR y OR z.
You also can't compare with NULL using = or IN, so that has to be a separate check.
select
Case
when t1.name IS NULL OR t1.name IN ('NA' or 'null') then 'NA'
else t1.name
end as 'Name'
Can you try use IN statement? That should work.
select Case WHEN (t1.name IN ('NA', 'null') OR t1.name IS NULL) THEN 'NA' else t1.name end as 'Name',
Case WHEN (t1.mobile IN ('NA', 'null') OR t1.mobile IS NULL) THEN 'NA' else t1.mobile_no end as 'Mobile no' from student;
I am running a simple Pivot query in Presto via "ExecuteSqlRecord" processor but the moment I run the processor it gives me the error: Unable to execute SQL select.
Query is:
with map_date as
(
SELECT
vin,
epoch,
timestamp,
date,
map_agg(signalName, value) as map_values
from hive.vehicle_signals.vehicle_signals_flat
where date(date) = date('2020-02-28')
and vin = '000015'
and signalName in ('timestamp','epoch','msgId','usec','vlan','vin','msgName','A','B','C','D','E','F','G','H',
'J','K','L','M','N','O','P','R','S','T','U','V','W','X',
'Y','Z','A1','A5','A2','A3','A4','A6','A8','A9','A10','A11','A12')
GROUP BY vin, epoch, timestamp, date
order by timestamp asc
)
SELECT
epoch
, timestamp
, CASE WHEN element_at(map_values, 'A') IS NOT NULL THEN map_values['A'] ELSE NULL END AS A
, CASE WHEN element_at(map_values, 'B') IS NOT NULL THEN map_values['B'] ELSE NULL END AS B
, CASE WHEN element_at(map_values, 'C') IS NOT NULL THEN map_values['C'] ELSE NULL END AS C
, CASE WHEN element_at(map_values, 'D') IS NOT NULL THEN map_values['D'] ELSE NULL END AS D
, CASE WHEN element_at(map_values, 'E') IS NOT NULL THEN map_values['E'] ELSE NULL END AS E
, CASE WHEN element_at(map_values, 'F') IS NOT NULL THEN map_values['F'] ELSE NULL END AS F
, CASE WHEN element_at(map_values, 'G') IS NOT NULL THEN map_values['G'] ELSE NULL END AS G
, CASE WHEN element_at(map_values, 'H') IS NOT NULL THEN map_values['H'] ELSE NULL END AS H
, CASE WHEN element_at(map_values, 'J') IS NOT NULL THEN map_values['J'] ELSE NULL END AS J
, CASE WHEN element_at(map_values, 'K') IS NOT NULL THEN map_values['K'] ELSE NULL END AS K
, CASE WHEN element_at(map_values, 'L') IS NOT NULL THEN map_values['L'] ELSE NULL END AS L
, CASE WHEN element_at(map_values, 'M') IS NOT NULL THEN map_values['M'] ELSE NULL END AS M
, CASE WHEN element_at(map_values, 'N') IS NOT NULL THEN map_values['N'] ELSE NULL END AS N
, CASE WHEN element_at(map_values, 'O') IS NOT NULL THEN map_values['O'] ELSE NULL END AS O
, CASE WHEN element_at(map_values, 'P') IS NOT NULL THEN map_values['P'] ELSE NULL END AS P
, CASE WHEN element_at(map_values, 'R') IS NOT NULL THEN map_values['R'] ELSE NULL END AS R
, CASE WHEN element_at(map_values, 'S') IS NOT NULL THEN map_values['S'] ELSE NULL END AS S
, CASE WHEN element_at(map_values, 'T') IS NOT NULL THEN map_values['T'] ELSE NULL END AS T
, CASE WHEN element_at(map_values, 'U') IS NOT NULL THEN map_values['U'] ELSE NULL END AS U
, CASE WHEN element_at(map_values, 'V') IS NOT NULL THEN map_values['V'] ELSE NULL END AS V
, CASE WHEN element_at(map_values, 'W') IS NOT NULL THEN map_values['W'] ELSE NULL END AS W
, CASE WHEN element_at(map_values, 'X') IS NOT NULL THEN map_values['X'] ELSE NULL END AS X
, CASE WHEN element_at(map_values, 'Y') IS NOT NULL THEN map_values['Y'] ELSE NULL END AS Y
, CASE WHEN element_at(map_values, 'Z') IS NOT NULL THEN map_values['Z'] ELSE NULL END AS Z
, CASE WHEN element_at(map_values, 'A1') IS NOT NULL THEN map_values['A1'] ELSE NULL END AS A1
, CASE WHEN element_at(map_values, 'A2') IS NOT NULL THEN map_values['A2'] ELSE NULL END AS A2
, CASE WHEN element_at(map_values, 'A3') IS NOT NULL THEN map_values['A3'] ELSE NULL END AS A3
, CASE WHEN element_at(map_values, 'A4') IS NOT NULL THEN map_values['A4'] ELSE NULL END AS A4
, CASE WHEN element_at(map_values, 'A5') IS NOT NULL THEN map_values['A5'] ELSE NULL END AS A5
, CASE WHEN element_at(map_values, 'A6') IS NOT NULL THEN map_values['A6'] ELSE NULL END AS A6
, CASE WHEN element_at(map_values, 'A8') IS NOT NULL THEN map_values['A8'] ELSE NULL END AS A8
, CASE WHEN element_at(map_values, 'A9') IS NOT NULL THEN map_values['A9'] ELSE NULL END AS A9
, CASE WHEN element_at(map_values, 'A10') IS NOT NULL THEN map_values['A10'] ELSE NULL END AS A10
, CASE WHEN element_at(map_values, 'A11') IS NOT NULL THEN map_values['A11'] ELSE NULL END AS A11
, CASE WHEN element_at(map_values, 'A12') IS NOT NULL THEN map_values['A12'] ELSE NULL END AS A12
, vin
, date
from map_date
I can run the ABOVE query in ExecuteSqlRecord perfectly fine if I pivot only ~20 fields but when pivoting ~37 fields then the ExecuteSqlRecord processor immediately Fails.
It is interesting to see that I can run the same pivot query with ~37 fields in Presto CLI and QueryDatabaseTable successfully! Please help me achieve this in ExecuteSQLRecord processor as it has the added benefit of writing the output flowfile in any desired format (for my case, I am writing the output of the SQL query as CSV)
Here is the full stack trace of the error,
java.sql.SQLException: Error executing query. No FlowFile to route to failure: java.sql.SQLException: Error executing query
java.sql.SQLException: Error executing query
at com.facebook.presto.jdbc.PrestoStatement.internalExecute(PrestoStatement.java:279)
at com.facebook.presto.jdbc.PrestoStatement.execute(PrestoStatement.java:228)
at com.facebook.presto.jdbc.PrestoPreparedStatement.<init>(PrestoPreparedStatement.java:80)
at com.facebook.presto.jdbc.PrestoConnection.prepareStatement(PrestoConnection.java:129)
at org.apache.commons.dbcp2.DelegatingConnection.prepareStatement(DelegatingConnection.java:303)
at org.apache.commons.dbcp2.DelegatingConnection.prepareStatement(DelegatingConnection.java:303)
at org.apache.nifi.processors.standard.AbstractExecuteSQL.onTrigger(AbstractExecuteSQL.java:237)
at org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:27)
at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1176)
at org.apache.nifi.controller.tasks.ConnectableTask.invoke(ConnectableTask.java:213)
at org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:117)
at org.apache.nifi.engine.FlowEngine$2.run(FlowEngine.java:110)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.RuntimeException: Error fetching next at http://presto.testk8s.content.mywebspace.com:8889/b7/journal/20245300229_0354554_08r940_as8y4/2?slug=78234778257265 returned an invalid response: JsonResponse{statusCode=500, statusMessage=Server Error, headers={connection=[close]}, hasValue=false} [Error: ]
at com.facebook.presto.jdbc.internal.client.StatementClientV1.requestFailedException(StatementClientV1.java:446)
at com.facebook.presto.jdbc.internal.client.StatementClientV1.advance(StatementClientV1.java:386)
at com.facebook.presto.jdbc.PrestoResultSet.getColumns(PrestoResultSet.java:1742)
at com.facebook.presto.jdbc.PrestoResultSet.<init>(PrestoResultSet.java:119)
at com.facebook.presto.jdbc.PrestoStatement.internalExecute(PrestoStatement.java:250)
... 17 common frames omitted
I have the following scenario
SELECT
...
...
(
#var1 = MAX(IF(table1.name='Mod', table1.value, NULL))
#var2 = MAX(IF(table1.name='Man', table1.value, NULL))
CASE
WHEN #var1 IS NOT NULL && #var2 IS NOT NULL THEN #var1+"/"+#var2
WHEN #var1 IS NULL && #var2 IS NOT NULL THEN #var2
WHEN #var1 IS NOT NULL && #var2 IS NULL THEN #var1
ELSE NULL
END) AS "col44",
This is throwing me an exception in the mysql console.
Can't we do a variable assignment in the general mysql select query?
What about this?
SELECT ...
, #var1 = MAX(IF(table1.name='Mod', table1.value, NULL))
, #var2 = MAX(IF(table1.name='Man', table1.value, NULL))
, CASE
WHEN #var1 IS NOT NULL && #var2 IS NOT NULL THEN #var1+"/"+#var2
WHEN #var1 IS NULL && #var2 IS NOT NULL THEN #var2
WHEN #var1 IS NOT NULL && #var2 IS NULL THEN #var1
ELSE NULL
END AS "col44"
Note the addition of commas, and removal of parenthesis.
Alternatively, you could just use a subquery. (In my experience, variables in queries can become unpredictable when used in conjunction with aggregation; this avoids the variables altogether).
SELECT ...
, CASE
WHEN modMax IS NOT NULL && manMax IS NOT NULL THEN modMax+"/"+manMax
WHEN modMax IS NULL && manMax IS NOT NULL THEN manMax
WHEN modMax IS NOT NULL && manMax IS NULL THEN modMax
ELSE NULL
END AS "col44"
FROM (
SELECT ...
, MAX(IF(table1.name='Mod', table1.value, NULL)) AS modMax
, MAX(IF(table1.name='Man', table1.value, NULL)) AS manMax
...
) AS subQ
Additional note: Watch out for modMax+"/"+manMax; it may end up giving you an integer calculated from modMax+0+manMax, instead of a string.
Have some ( difficult ) MySQL query
SELECT
*,
(
SELECT
MIN(tbl.id)
FROM
stat tbl
WHERE
tbl.id > stat.id
and tbl.user_id=stat.user_id
) next_session_id,
(
Select
(case
when next_session_id IS NULL then '9999-12-31 23:59:59' else sub.date
end)
from
stat sub
where
sub.id=next_session_id
) next_date
FROM
stat
WHERE
user_id=15973
and date >'2014-06-01'
Problem in next, for last row next_session_id are NULL(check in MySQL Workbench) and it must return datetime '9999-12-31 23:59:59', but return NULL. Check IS NULL not work i think or incorrect. Why it's happen?
UPDATE
Example of result set
You should add OR next_session_id IS NULL to the WHERE clause of your subselect
SELECT
(CASE
WHEN next_session_id IS NULL THEN '9999-12-31 23:59:59' ELSE sub.date
END)
FROM
stat sub
WHERE
sub.id=next_session_id OR next_session_id IS NULL
because without that the where condition will not be true, if next_session is null.
Try changing this part
Select
(case
when next_session_id IS NULL then '9999-12-31 23:59:59' else sub.date
end)
from
stat sub
where
sub.id=next_session_id
to
case
when next_session_id IS NULL then '9999-12-31 23:59:59' else (
Select sub.date
from
stat sub
where
sub.id=next_session_id)
end
Because in MySQL, NULL = NULL doesn't evaluate to true.
In database NULL and '' (empty string) are different. So ensure you have NULL value instead of '' (empty string) in your table.
Check this query with your table
SELECT * FROM TABLE WHERE next_session_id=''
I think IS NULL is not working because of some reason, so you can use = '',
for example x is null you can use x = '' instead.
I'm working on an 'audit_history' table that stores updates performed on my 'myTable' table. My code works with just the single IF statement that handles password updates. But as soon as I add another, I get the #1064 SQL Error. So I'm guessing it's something to do with my delimiters(?), but I just can't get it to work.
Any and all help would be great. Thanks.
IF (NEW.password <> OLD.password)
OR (NEW.password IS NOT NULL AND OLD.password IS NULL)
OR (NEW.password IS NULL AND OLD.password IS NOT NULL)
THEN
INSERT INTO myTable.audit_history
VALUES (
'update'
, NULL
, NOW()
, #user_id
, 'user'
, 'password'
, OLD.password
, NEW.password);
END IF;
IF (NEW.landline <> OLD.landline)
OR (NEW.landline IS NOT NULL AND OLD.landline IS NULL)
OR (NEW.landline IS NULL AND OLD.landline IS NOT NULL)
THEN
INSERT INTO myTable.audit_history
VALUES (
'update'
, NULL
, NOW()
, #user_id
, 'user'
, 'landline'
, OLD.landline
, NEW.landline);
END IF;
I get this error message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to you MySQL server version for the right syntax to use near ‘IF (NEW.landline <> OLD.landline) OR (NEW.landline IS NOT NULL AND OLD.landline’ at line 17
You need BEGIN and END keywords for multiple statements
BEGIN
IF (NEW.password <> OLD.password)
OR (NEW.password IS NOT NULL AND OLD.password IS NULL)
OR (NEW.password IS NULL AND OLD.password IS NOT NULL)
THEN
INSERT INTO myTable.audit_history
VALUES (
'update'
, NULL
, NOW()
, #user_id
, 'user'
, 'password'
, OLD.password
, NEW.password);
END IF;
IF (NEW.landline <> OLD.landline)
OR (NEW.landline IS NOT NULL AND OLD.landline IS NULL)
OR (NEW.landline IS NULL AND OLD.landline IS NOT NULL)
THEN
INSERT INTO myTable.audit_history
VALUES (
'update'
, NULL
, NOW()
, #user_id
, 'user'
, 'landline'
, OLD.landline
, NEW.landline);
END IF;
END;