How to change variable value on demand in JasperReports - variables

I am working with iReport. Here is the situation:
I have some parameters named branchID, spType.
I have created one variable named branchName (string) which is by default null.
What I need to do is, to change/ edit the value of the variable branchName depending on the values of branchID and spType
How can I do this with JasperReports using If Else. Is it something with the properties of the variable branchName? Any help/ Suggestion would be highly appreciated.

Expressions Scripting Language in Jasper reports is your friend!
What i can fathom from your query is that you want to compute the value of a variable based on values of two parameters. In short, you want to do something like:-
Simple if-else
If (branchId == 1 && spType == "abc")
branchName = "Xyz1";
else
branchName = "Xyz2";
In Jasper scripting language, the above expression can be written as :-
($P{branchId}==1 &&
$P{spType} != null &&
$P{spType}.equals("abc"))?"Xyz1":"Xyz2"
Nested If-else
If (branchId == 1 && spType == "abc")
branchName = "Xyz1";
else if (branchId = 2 && spType == "def")
branchName = "Xyz2";
else
branchName = "Xyz3";
Jasper expression:
( $P{branchId} ==1 &&
$P{spType} !=null &&
$P{spType}.equals("abc") ) ? "Xyz1" :
(($P{branchId} == 2 &&
$P{spType} != null &&
$P{spType}.equals("def")) ? "Xyz2" : "Xyz3");
For more details, have a look at this tutorial:
http://www.tutorialspoint.com/jasper_reports/jasper_report_expression.htm
Here is a similar stackoverflow question also:
doing comparison if else in JasperReports
For core concept, Page no. 10 of this pdf:
http://jasperreports.sourceforge.net/JasperReports-Ultimate-Guide-3.pdf

iReport support scripting using expressions.
You can right click on a report element and click edit expression to open the expression editor.
Some useful notes in this answer as well.

Related

typeorm: how to properly use IsNotNull/IsNull?

We created a helper function to create wheres easier. It works fine with eq, neq, lt and gt. Now we're trying to add is null/is not null (for a date column, not sure if that matters).
The critical part of the function looks like this:
// This is ran in a loop for every attribute
const query = `${attribute}` ${comparator} :value${index}`;
// if the checked 'value' is NULL then use IsNull(), same for NOT NULL, otherwise simply use value
const params = { [`value${index}`]: value == 'NULL' ? IsNull() : value === 'NOT NULL' ? Not(IsNull()) : value};
// Add this (sub)query to the qb
qb.andWhere(query, params);
Now we get an error saying this:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ’_type = ‘not’, _value = ‘[object Object]‘, _useParameter =
true, `_multipl’ at line 1"
Value is [object Object] - which kind of makes sense if we use IsNotNull(), right?
As far as I understand from this comment, IsNull() and Not(IsNull()) should work like we are trying to.
We use #nestjs/typeorm 7.1.5.
To check for NULL you need
qb.andWhere(`${attribute} IS NULL`)
To check for NOT NULL you need
qb.andWhere(`${attribute} IS NOT NULL`)
(Note: Omit the second argument, parameters, for these cases).
From your code seems you are using string values 'NULL' and 'NOT NULL' as the value arguments and checking these as special cases. Your code will now look like this:
if ((value == 'NULL' && comparator == '=') || (value == 'NOT NULL' && comparator == '<>'))
qb.andWhere(`${attribute} IS NULL`);
if ((value == 'NOT NULL' && comparator == '=') || (value == 'NULL' && comparator == '<>'))
qb.andWhere(`${attribute} IS NOT NULL`);
else
qb.andWhere(`${attribute} ${comparator} :value${index}`, { [`value${index}`]: value});
(In the code above I check for '=' and '<>' which are standard SQL comparison operators. If your SQL dialect uses 'eq' and 'ne' in place of '=' and '<>', which you mention in your question, you will need to change the code above. If so please update your question and add the appropriate tag to say which SQL database you are using).
When you test this, I recommend that you turn on TypeOrm full logging so you can see the actual generated SQL and you be able to quickly solve any problems. See TypeOrm logging.

Spark Sql Parser append additional parameter in UDF call

I use SQL statements as input from users something like "CASE WHEN CALL_UDF("12G", 2) < 0 THEN 4 ELSE 5 END".
I would like to add in this string an additional parameter.
Expected result: "CASE WHEN CALL_UDF("12G", 2, additional_parameter) < 0 THEN 4 ELSE 5 END".
To achieve this goal I am trying to use SparkSqlParser but faced problems during the implementation of this replacement. Probably someone has implemented a similar solution. Thanks.
What I have already tried:
val expression = parser.parseExpression("CASE WHEN CALL_UDF("12G", 2) < 0 THEN 3 ELSE 4 END")
.transformDown {
case expression if expression.isInstanceOf[UnresolvedFunction] && expression.asInstanceOf[UnresolvedFunction].name.funcName == "CALL_UDF" =>
UnresolvedFunction(FunctionIdentifier("CALL_UDF"), expression.children.toList ++ Seq(parser.parseExpression("4")), false)
}

How to parse big string U-SQL Regex

I have got a big CSVs that contain big strings. I wanna parse them in U-SQL.
#t1 =
SELECT
Regex.Match("ID=881cf2f5f474579a:T=1489536183:S=ALNI_MZsMMpA4voGE4kQMYxooceW2AOr0Q", "ID=(?<ID>\\w+):T=(?<T>\\w+):S=(?<S>[\\w\\d_]*)") AS p
FROM
(VALUES(1)) AS fe(n);
#t2 =
SELECT
p.Groups["ID"].Value AS gads_id,
p.Groups["T"].Value AS gads_t,
p.Groups["S"].Value AS gads_s
FROM
#t1;
OUTPUT #t
TO "/inhabit/test.csv"
USING Outputters.Csv();
Severity Code Description Project File Line Suppression State
Error E_CSC_USER_INVALIDCOLUMNTYPE:
'System.Text.RegularExpressions.Match' cannot be used as column type.
I know how to do it in a SQL way with EXPLODE/CROSS APPLY/GROUP BY. But may be it is possible to do without these dances?
One more update
#t1 =
SELECT
Regex.Match("ID=881cf2f5f474579a:T=1489536183:S=ALNI_MZsMMpA4voGE4kQMYxooceW2AOr0Q", "ID=(?<ID>\\w+):T=(?<T>\\w+):S=(?<S>[\\w\\d_]*)").Groups["ID"].Value AS id,
Regex.Match("ID=881cf2f5f474579a:T=1489536183:S=ALNI_MZsMMpA4voGE4kQMYxooceW2AOr0Q", "ID=(?<ID>\\w+):T=(?<T>\\w+):S=(?<S>[\\w\\d_]*)").Groups["T"].Value AS t,
Regex.Match("ID=881cf2f5f474579a:T=1489536183:S=ALNI_MZsMMpA4voGE4kQMYxooceW2AOr0Q", "ID=(?<ID>\\w+):T=(?<T>\\w+):S=(?<S>[\\w\\d_]*)").Groups["S"].Value AS s
FROM
(VALUES(1)) AS fe(n);
OUTPUT #t1
TO "/inhabit/test.csv"
USING Outputters.Csv();
This wariant works fine. But there is a question. Will the regex evauated 3 times per row? Does exists any chance to hint U-SQL engine - the function Regex.Match is deterministic.
You should probably be using something more efficient than Regex.Match. But to answer your original question:
System.Text.RegularExpressions.Match is not part of the built-in U-SQL types.
Thus you would need to convert it into a built-in type, such as string or SqlArray<string> or wrap it into a udt that provides an IFormatter to make it a user-defined type.
Looks like it is better to use something like this to parse the simple strings. Regexes are slow for the task and if i will use simple string expressions (instead of CLR calls) they probably will be translated into c++ code at codegen phase... and .net interop will be eliminated (i'm not sure).
#t1 =
SELECT
pv.cust_gads != null ? new SQL.ARRAY<string>(pv.cust_gads.Split(':')) : null AS p
FROM
dwh.raw_page_view_data AS pv
WHERE
pv.year == "2017" AND
pv.month == "04";
#t3 =
SELECT
p != null && p.Count == 3 ? p[0].Split('=')[1] : null AS id,
p != null && p.Count == 3 ? p[1].Split('=')[1] : null AS t,
p != null && p.Count == 3 ? p[2].Split('=')[1] : null AS s
FROM
#t1 AS t1;
OUTPUT #t3
TO "/tmp/test.csv"
USING Outputters.Csv();

SSIS Split Condition

I need to implement SCD Type 2
this is my condition in my split condition before updating my RecordEndDate and eventually adding it in the database. But even though it does not satisfy the condition it still keeps on adding it in the database
((PlateNo == Stage_PlateNo)) && (([Car Name] != [Stage_Model]) ||
([Manufacturer] != [Stage_Manufacturer]) ||
[Year Model] != Stage_Year ||
[Car Body Type] != Stage_BodyType ||
Transmission != Stage_Transmission ||
[Daily Rate (in Peso)] != Stage_DailyRate
)
IMO your requirements are far too complex to attempt in an SSIS Expression. I recommend you recode this logic in a Script transformation. I would pre-create a new Column e.g. Include_Row and in the Script set it's value to yes or no.
This approach will give you much more code flexibility and better debugging possibilities.

Having problems converting conditional where clause in LINQ back over to SQL

I've got myself in a bit of a pickle!
I've done a snazzy LINQ statement that does the job in my web app, but now I'd like to use this in a stored procedure:
var r = (from p in getautocompleteweightsproducts.tblWeights
where p.MemberId == memberid &&
p.LocationId == locationid
select p);
if (level != "0")
r = r.Where(p => p.MaterialLevel == level);
if (column == "UnitUserField1")
r = r.Where(p => p.UnitUserField1 == acitem);
if (column == "UnitUserField2")
r = r.Where(p => p.UnitUserField2 == acitem);
return r.OrderBy(p => p.LevelNo).ToList();
However, I can't for the life of me get the conditional where clause to work!!
If someone can point me in the right direction, I'd be most grateful.
Kind regards
Maybe something like this?
SELECT *
FROM dbo.weights
WHERE member_id = #memberid
AND location_id = #locationid
AND material_level = CASE WHEN #level = '0' THEN material_level
ELSE #level END
AND #acitem = CASE #column WHEN 'UnitUserField1' THEN unit_user_field_1
WHEN 'UnitUserField2' THEN unit_user_field_2
ELSE #acitem END
ORDER BY level_no
Have you tried LinqPAD, I'm pretty sure last time I played with that you could enter "LINQ to SQL" code and see the resulting SQL that produced. Failing that, place a SQL trace/profiler on your code running the LinqTOSQL and find the query being executed in the trace.
LukeH's answer will give you the correct rows, but there is something lost when you try to replace a query-generating-machine with a single query. There are parts of that query that are opaque to the optimizer.
If you need the original queries as-would-have-been-generated-by-linq, there are two options.
Generate every possible query and control which one runs by IF ELSE.
Use Dynamic sql to construct each query (although this trades away many of the benefits of using a stored procedure).
If you do decide to use dynamic sql, you should be aware of the curse and blessings of it.