Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Conditional Filters using IN clauses
morganm00
First of all, I'm using BIRT 4.2.2.
I'm trying to find a way to have a conditional filter using in-clause style logic.
Here's the scenario:
A view returns a bunch of student records with department ID and affiliated department IDs. Normally, the data set returns all students that are members of a set of department ID's passed as a query parameter. However, if a checkbox is selected, then it needs to return all students with a department ID or an affiliated department ID from the set of selected department IDs.
So, the query is either:
SELECT * FROM STUDENTS WHERE DEPT_ID IN param["deptlist"]
or, if the checkbox is selected:
SELECT * FROM STUDENTS WHERE DEPT_ID IN param["deptlist"] OR AFFILIATED_DEPT_ID IN param["deptlist"]
I've been trying to play with filters and the expression editor for hours trying to figure out a way to implement that seemingly simple query, but I keep hit walls either trying to get the "OR" implemented since filters are ANDed or the "IN" implemented since that doesn't seem possible to test the expression editor.
Does anyone have any ideas?
Thanks in advance.
-mike
Find more posts tagged with
Comments
kclark
Are you getting errors when trying to use that query? With the classicmodels DB I was able to use this query.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
select distinct *
from customers
where customernumber in ( 103, 112 ) or customername in ( 'Mini Wheels Co.', 'Blauer See Auto, Co.' )</pre>
<br />
Which is close to what you're doing without the parameters. If you're parameters are only passing a single value then you could try something like this in your script.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var paramValue = params["deptlist"];
this.queryText = "SELECT * FROM STUDENTS WHERE DEPT_ID IN (" + paramValue + ") OR AFFILIATED_DEPT_ID IN (" + paramValue + ")"</pre>
Tubal
The first thing, you need parentheses around your IN values. Adding to what kclark said, you could create the query in the beforeOpen event of the dataset and use the correct query based on your affiliated checkbox value:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
paramValue = params["deptlist"];
// Or if your department list is a multiselect parameter
// paramValue = params["deptlist"].join(',');
if(params["affiliated"]==true){
this.queryText = "SELECT * FROM STUDENTS WHERE DEPT_ID IN (" + paramValue + ") OR AFFILIATED_DEPT_ID IN (" + paramValue + ")";
} else {
this.queryText = "SELECT * FROM STUDENTS WHERE DEPT_ID IN (" + paramValue + ")";
}</pre>
Yaytay
<blockquote class='ipsBlockquote' data-author="'morganm00'" data-cid="116860" data-time="1368735898" data-date="16 May 2013 - 01:24 PM"><p>
I'm trying to find a way to have a conditional filter using in-clause style logic.<br /></p></blockquote>
Try this: <a class='bbc_url' href='
https://code.google.com/a/eclipselabs.org/p/birt-functions-lib/wiki/BindParameters'>https://code.google.com/a/eclipselabs.org/p/birt-functions-lib/wiki/BindParameters</a><br
/>
<br />
I've used it in BIRT 3.7 and 4.2.<br />
<br />
It's a little inconvenient because you have to write a tiny amount of script, but it works really well.<br />
<br />
Jim
morganm00
<blockquote class='ipsBlockquote' data-author="'Tubal'" data-cid="116867" data-time="1368748076" data-date="16 May 2013 - 04:47 PM"><p>
The first thing, you need parentheses around your IN values. Adding to what kclark said, you could create the query in the beforeOpen event of the dataset and use the correct query based on your affiliated checkbox value:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
paramValue = params["deptlist"];
// Or if your department list is a multiselect parameter
// paramValue = params["deptlist"].join(',');
if(params["affiliated"]==true){
this.queryText = "SELECT * FROM STUDENTS WHERE DEPT_ID IN (" + paramValue + ") OR AFFILIATED_DEPT_ID IN (" + paramValue + ")";
} else {
this.queryText = "SELECT * FROM STUDENTS WHERE DEPT_ID IN (" + paramValue + ")";
}</pre></p></blockquote>
<br />
Aha! The "join" function is really the missing link for me. The department list is a multi-select so I would need to use that. I hadn't considered rewriting the entire query. I had hoped to be able to do something with the filters expression UI or the other normal UI pieces. I have 15-20 reports that need to make use of similar logic like this and I was kind of hoping that I'd be able to do it within the normal functional confines of the BIRT UI rather than having to skip it all and use scripting to bypass everything. If there was some sort of BIRTFunction.In(string,collection of strings) then I could do it in a filter expression, although rewriting the SQL clause will probably overall serve to be more efficient.<br />
<br />
But hey, whatever works ...<br />
<br />
Thanks!