I need a variable expression that calls a sub dataset with a parameter attached.
I have a dataset which is returning a list of grocery purchases made. Something like:
Milk, NJ, $1.23
Chicken, DE, $5.45
Milk, NY, $1.44
I am able to get a sum of the prices using a summation variable on the purchase price. However, I need a total that takes the state tax into account. In the above example, let’s say the NJ sales tax multiplier is 1.06 and NY sales tax is 1.08. Instead of my milk sum being 2.76 (=1.44 + 1.23), I need it to be 2.859 (=1.23*1.06 + 1.44*1.08).
I have a subdataset SALES_TAX that with the query:
SELECT tax_multiplier FROM mydb.sales_tax WHERE state=$P{STATE}
and I essentially need:
Sum($F{PURCHASE_PRICE} * $V{getTax($F{PURCHASE_STATE)})
How can I accomplish this? What will my getTax variable need to look like to query the SALES_TAX sub dataset with the purchase state field from the main dataset?
Marianna,
Could you build a tax field into your query? You could create a case statement to evaluate the state where the purchase was made. Your query could spit out the pre-tax price, the tax value, and the post-tax price. The case statement would look something like (I’m using Oracle, you may need to adjust for your database):
If you wanted to do this in iReport, I would suggest creating a variable to apply the tax rate. The variable expression would be something like:
Then, your iReport detail band should output one text field with the text field expression
$F{PURCHASE_PRICE}, one text field with the text field expression $V{TAX_RATE}, and one text field with the text field expression $F{PURCHASE_PRICE}*$V{TAX_RATE}.
If you want to use a subquery in your SQL query to find the sales tax, you could: