I need to create a materialized query table with records which have an attribute retrieved with a sub query on a second table, in order to get the most recent value of that attribute. This question is very similar to the one discussed here:Retrieving the last record in each group except the following:
- inner sub query must compares second table's validity date to current_date (or CURDATE) not to another date field from primary table
- results are used to create a new materialized query table
Under these assumptions the SQL ends abnormally giving SQ20058 error, which can be eliminated in two ways:
- using a specific date instead of current_date, which makes the statement useless to my purposes
- adding - disable query optimization- to current settings at the end of the statement - data initially deferred refresh deferred maintained by user- which in turn disables query optimization made by iSeries V7R1 operating system. 
To solve the problem, I tried to create a view with all records extracted by inner sub query, then issue a create table statement joining the view, so that to avoid sub query execution, but again materialized query table can't address a view which contains references to current date.
Do you know how can I compare records to current date when creating tables? What am I missing?
Thank you
Example: 1) given sales table defined as:
create table sales (
item_id decimal(3, 0),
sale_date date,         
sale_qty decimal(7, 2)
)
with the following content:
ITEM_ID  SALE_DATE   SALE_QTY 
    1    2016-01-10     10,00 
    1    2016-02-10     10,00
    1    2016-03-10     10,00 
    2    2016-01-10      5,00 
    2    2016-02-10      5,00 
    2    2016-03-10      5,00 
2) and depts table defined as:
create table depts (
item_id decimal(3, 0),
dept_from_date date,         
dept_id character(3)
)
with the following content:
ITEM_ID  DEPT_FROM_DATE  DEPT_ID
    1       2016-01-01     AAA  
    1       2016-03-01     BBB  
    2       2016-01-01     BBB  
    2       2016-02-01     CCC  
    2       2016-05-01     DDD  
    2       2016-12-01     EEE  
3) that is the expected result:
select s.item_id, s.sale_date, s.sale_qty, d.dept_id
from sales s
left join depts d on d.item_id=s.item_id
and d.dept_from_date = (
select max(dept_from_date)
from depts x
where x.item_id = d.item_id and dept_from_date <= current_date)
  ITEM_ID  SALE_DATE   SALE_QTY   DEPT_ID
    1    2016-01-10     10,00     BBB  
    1    2016-02-10     10,00     BBB  
    1    2016-03-10     10,00     BBB  
    2    2016-01-10      5,00     DDD  
    2    2016-02-10      5,00     DDD  
    2    2016-03-10      5,00     DDD  
where department id is flattened to its most recent value for BI purposes
4) when previous select statement is embedded in a create table statement like this:
create table sales2 as (
select s.item_id, s.sale_date, s.sale_qty, d.dept_id
from sales s
left join depts d on d.item_id=s.item_id
and d.dept_from_date = (
select max(dept_from_date)
from depts x
where x.item_id = d.item_id and dept_from_date <= current_date))
data initially deferred
refresh deferred       
maintained by user
it returns SQ20058 error.
 
     
    