Set a variable inside case statement in mysql - mysql

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.

Related

How to handle null and NULL value in sql

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;

CASE statement after WHERE then one of several values can be NULL?

I have such select:
SELECT
*
FROM
Table Tb
WHERE
Tb.LastModificationDate BETWEEN #StartDate And #EndDate
AND Tb.var1 = #var1
AND Tb.var2 = #var2
And its possible that #var1 and #var2 can be null (or only one of them) so for now I have 3 selects for each case (real select is more complicated so I am providing example for simplify things) I am try to merge them into one select with CASE but have no luck so far - as cant understand how to include AND clause in case if one of vars are not null.
I believe it should be something like that:
SELECT
*
FROM
Table Tb
WHERE
Tb.LastModificationDate BETWEEN #StartDate And #EndDate
(CASE WHEN #var1 IS NOT NULL THEN (AND Tb.var1 = #var1)
WHEN #var2 IS NOT NULL THEN (AND Tb.var2 = #var2)
END);
any advice on that?
To use the CASE statement, you would need to remove the AND clause from the THEN portion and move it to outside of CASE so that the result is evaluated. This is because CASE can not append the criteria to the WHERE clause (THEN AND).
SELECT *
FROM Table Tb
WHERE Tb.LastModificationDate BETWEEN #StartDate And #EndDate
AND (CASE
WHEN #var1 IS NOT NULL THEN Tb.var1 = #var1
WHEN #var2 IS NOT NULL THEN Tb.var2 = #var2
END);
However, the above approach will be limited to the first "CASE #var* IS NOT NULL" that evaluates as true. So if both #var1 and #var2 are supplied, the CASE will only return Tb.var1 = #var1, and will not compare Tb.var2 = #var2.To retrieve a result when both #var1 and #var2 are NULL, ELSE true would need to added to the CASE statement.
(CASE
WHEN #var1 IS NOT NULL THEN Tb.var1 = #var1
WHEN #var2 IS NOT NULL THEN Tb.var2 = #var2
ELSE true
END);
Another approach that circumvents the first case limitation, would be to apply the criteria directly to the WHERE clause by using AND (IS NULL OR ...).
This will allow for the criteria to match under the following 4 conditions:
Not checked if both #var1 and #var2 are NULL
#var1 if supplied (Tb.var1 = #var1)
#var2 if supplied (Tb.var2 = #var2)
#var1 and #var2 if both are supplied (Tb.var1 = #var1 AND Tb.var2 = #var2)
SELECT *
FROM Table Tb
WHERE Tb.LastModificationDate BETWEEN #StartDate And #EndDate
AND (#var1 IS NULL OR Tb.var1 = #var1)
AND (#var2 IS NULL OR Tb.var2 = #var2);
Working Examples: DB-Fiddle
SELECT
*
FROM
Table Tb
WHERE
Tb.LastModificationDate BETWEEN #StartDate And #EndDate
AND ((#var1 IS NOT NULL AND Tb.var1 = #var1)
OR
(#var2 IS NOT NULL AND Tb.var2 = #var2));
I would have done this way.
Check for both #var1 and #var2 if they are NULL or equal to the value of a column:
WHERE Tb.LastModificationDate BETWEEN #StartDate And #EndDate
AND (#var1 IS NULL OR Tb.var1 = #var1)
AND (#var2 IS NULL OR Tb.var2 = #var2)

NiFi ExecuteSqlRecord not processing SQL query(Presto)

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

MySQL query case statement in where optimization

I am writing a query in mysql i am not familer with the recursive query to much.
how and what i need to do to optimization of the below query as the conditions i put not looks good ,there must be a easier way to do same.
select
b.entity_id
from
entity_hierarchies a,
entity_hierarchies b
where
a.entity_id = 25
and a.entity_type = 'user'
and b.entity_type = 'idea'
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
and COALESCE(a.Geography_Country, '') =
case
when
a.Geography_Country is null
then
COALESCE(a.Geography_Country, '')
else
b.Geography_Country
end
and COALESCE(a.Geography_DistrictOrCounty, '') =
case
when
a.Geography_DistrictOrCounty is null
then
COALESCE(a.Geography_DistrictOrCounty, '')
else
b.Geography_DistrictOrCounty
end
and COALESCE(a.Geography_State, '') =
case
when
a.Geography_State is null
then
COALESCE(a.Geography_State, '')
else
b.Geography_State
end
and COALESCE(a.Geography_City, '') =
case
when
a.Geography_City is null
then
COALESCE(a.Geography_City, '')
else
b.Geography_City
end
Intro
I noticed you could rewrite some of these statements in a much simpler form.
So for example:
and a.Geography_Geography =
case
when
a.Geography_Geography is null
then
a.Geography_Geography
else
b.Geography_Geography
end
can simply become:
AND ( a.Geography_Geography is null or a.Geography_Geography = b.Geography_Geography )
Final Solution
select
b.entity_id
from
entity_hierarchies a,
entity_hierarchies b
where
a.entity_id = 25
and a.entity_type = 'user'
and b.entity_type = 'idea'
AND ( a.Geography_Geography is null OR a.Geography_Geography = b.Geography_Geography )
AND ( a.Geography_Country is null OR a.Geography_Geography = b.Geography_Country )
AND ( a.Geography_DistrictOrCounty is null OR a.Geography_DistrictOrCounty = b.Geography_DistrictOrCounty )
AND ( a.Geography_State is null OR a.Geography_State = b.Geography_State )
AND ( a.Geography_City is null OR a.Geography_City = b.Geography_City );

Use multiple variables in select

I have a question about using variables.
I need to get different values from the same condition.
Is the following example correct?
Many thanks!!!!
DECLARE #var1 varchar(50)
DECLARE #var2 varchar(50)
SELECT #var1, #var2
FROM table
WHERE IF condicion1 IN (1, 2, 3)
BEGIN
SET #var1='Value1'
SET #var2='Value2'
END
You can assign values to variables in SELECT statement.
DECLARE #var1 varchar(50)
DECLARE #var2 varchar(50)
SELECT #var1 = CASE WHEN condicion1 = 1 THEN 'Value1'
WHEN condicion1 = 2 THEN 'Value1'
WHEN condicion1 = 3 THEN 'Value1' END,
#var2 = CASE WHEN condicion1 = 1 THEN 'Value2'
WHEN condicion1 = 2 THEN 'Value2'
WHEN condicion1 = 3 THEN 'Value2' END
FROM table