问题
I am trying to create a report in iReport based on an oracle SQL query which deals with transactions. The actual tables involved are very lengthy and are not really relevant to the issue. I am trying to create a report that will return results when the transaction amounts meet 1 of the 3 following criteria:
- between 1000.00 and 2499.99
- between 2500.00 and 9999.99
- >= 10000.00
I am trying to create a single select list parameter where the person running the report can choose between one of the above specifications. Before I actually create the parameter I have been trying to test the query in SQL Developer. The line of the query where I am trying to implement this is:
alias.transaction_amount :amountRange
and my test input is "between 1000 and 9999". When that is hardcoded in the query, it works fine, but when I try and pass it in through this input I get an "Invalid Relational Operator" error.
Is there another way of doing this?
回答1:
Try to use like this in query:
select ... from table1 where transaction_amount $P!{paramReport}
From JasperReports Ultimate Guide:
$P{paramName} Syntax
The parameters are used like normal java.sql.PreparedStatement parameters, using
the following syntax:
<queryString>
<![CDATA[
SELECT * FROM Orders WHERE OrderID <= $P{MaxOrderID} ORDER BY
ShipCountry
]]>
</queryString>
$P!{paramName} Syntax
Sometimes it is useful to use parameters to dynamically modify portions of the SQL
query or to pass the entire SQL query as a parameter to the report-filling routines. In
such cases, the syntax differs a little, as shown in the following example. Notice the !
character:
<queryString>
<![CDATA[
SELECT * FROM $P!{MyTable} ORDER BY $P!{OrderByClause}
]]>
</queryString>
回答2:
After talking with some colleagues, we discovered a better solution to this problem, while I don't know if the method I was looking for is actually possible, this solution is much more intuitive.
alias.transaction_amount between (CASE WHEN $P{Range} = 0
THEN 1000.00
WHEN $P{Range} = 1
THEN 2500.00
ELSE 10000.00
END)
and
(CASE WHEN $P{Range} = 0
THEN 2499.99
WHEN $P{Range} = 1
THEN 9999.99
ELSE 9999999.99
END)
Using case statements with a switch parameter works perfectly, granted that if there's ever a transaction which is 1 billion dollars or greater they will be excluded but the maximum number can be adjusted as necessary.
来源:https://stackoverflow.com/questions/15145575/passing-sql-operators-to-query-through-ireport-parameter