Yllirion
5th July 2016, 16:15
Hi to all!

I trying to get some data with this query:
select t3.* |cag_name, cag_num, docdate, leac, deb_sum, cred_sum, descr, cor_leac, ttyp, docnum, btno, cr_user
from
(select t2.NAMA as cag_name, t.BPID as cag_num, t.DCDT as docdate, t.LEAC as leac,
(case t.DBCR when 1 then t.AMTH(1)*t.SIGN when 2 then 0 end) as deb_sum,
(case t.DBCR when 2 then t.AMTH(1)*t.SIGN when 1 then 0 end) as cred_sum,
t.REFR as descr, t.COR_LEAC as cor_leac, t.TTYP as ttyp, t.DOCN as docnum, t.BTNO as btno, t.USER as cr_user
from tfcoa102 t, tccom100 t2
where t.FYER = 2016 and t.FPRD = 6 and (t.LEAC = "161001" or t.LEAC = "161000")
and t.BPID = T2.BPID
union all
select t2.NAMA, t.BPID, t.DCDT, t.COR_LEAC,
(case t.COR_DBCR when 1 then t.AMTH(1)*t.COR_SIGN when 2 then 0 end) as deb_sum,
(case t.COR_DBCR when 2 then t.AMTH(1)*t.COR_SIGN when 1 then 0 end) as cred_sum,
t.REFR, t.LEAC, t.TTYP, t.DOCN, t.BTNO, t.USER
from tfcoa102 t, tccom100 t2
where t.FYER = 2016 and t.FPRD = 6 and (t.COR_LEAC = "161001" or t.COR_LEAC = "161000")
and t.BPID = t2.BPID
union all
select t2.NAMA, t.BPID, t.DCDT, t.LEAC,
(case t.DBCR when 1 then t.AMTH(1)*t.SIGN when 2 then 0 end) as deb_sum,
(case t.DBCR when 2 then t.AMTH(1)*t.SIGN when 1 then 0 end) as cred_sum,
t.REFR, t.COR_LEAC, t.OTYP, t.ODOC, t.OBAT, t.USER
from tfcoa106 t, tccom100 t2
where t.FYER = 2016 and t.FPRD = 6 and (t.LEAC = "161001" or t.LEAC = "161000")
and t.BPID = T2.BPID
union all
select t2.NAMA, t.BPID, t.DCDT, t.COR_LEAC,
(case t.COR_DBCR when 1 then t.AMTH(1)*t.COR_SIGN when 2 then 0 end) as deb_sum,
(case t.COR_DBCR when 2 then t.AMTH(1)*t.COR_SIGN when 1 then 0 end) as cred_sum,
t.REFR, t.LEAC, t.OTYP, t.ODOC, t.OBAT, t.USER
from tfcoa106 t, tccom100 t2
where t.FYER = 2016 and t.FPRD = 6 and (t.COR_LEAC = "161001" or t.COR_LEAC = "161000")
and t.BPID = t2.BPID
) as t3
|order by cag_num
selectdo
----do somethink---
endselect
and having this error by compiler:
Error SQL: SQLState HYL06: select item 1 must be bound explicitly (302)

If I change code:
select cag_name, cag_num, docdate, leac, deb_sum, cred_sum, descr, cor_leac, ttyp, docnum, btno, cr_user
from
(select t2.NAMA as cag_name, t.BPID as cag_num, t.DCDT as docdate, t.LEAC as leac,
(case t.DBCR when 1 then t.AMTH(1)*t.SIGN when 2 then 0 end) as deb_sum,
(case t.DBCR when 2 then t.AMTH(1)*t.SIGN when 1 then 0 end) as cred_sum,
t.REFR as descr, t.COR_LEAC as cor_leac, t.TTYP as ttyp, t.DOCN as docnum, t.BTNO as btno, t.USER as cr_user
from tfcoa102 t, tccom100 t2
where t.FYER = 2016 and t.FPRD = 6 and (t.LEAC = "161001" or t.LEAC = "161000")
and t.BPID = T2.BPID
union all
select t2.NAMA, t.BPID, t.DCDT, t.COR_LEAC,
(case t.COR_DBCR when 1 then t.AMTH(1)*t.COR_SIGN when 2 then 0 end) as deb_sum,
(case t.COR_DBCR when 2 then t.AMTH(1)*t.COR_SIGN when 1 then 0 end) as cred_sum,
t.REFR, t.LEAC, t.TTYP, t.DOCN, t.BTNO, t.USER
from tfcoa102 t, tccom100 t2
where t.FYER = 2016 and t.FPRD = 6 and (t.COR_LEAC = "161001" or t.COR_LEAC = "161000")
and t.BPID = t2.BPID
union all
select t2.NAMA, t.BPID, t.DCDT, t.LEAC,
(case t.DBCR when 1 then t.AMTH(1)*t.SIGN when 2 then 0 end) as deb_sum,
(case t.DBCR when 2 then t.AMTH(1)*t.SIGN when 1 then 0 end) as cred_sum,
t.REFR, t.COR_LEAC, t.OTYP, t.ODOC, t.OBAT, t.USER
from tfcoa106 t, tccom100 t2
where t.FYER = 2016 and t.FPRD = 6 and (t.LEAC = "161001" or t.LEAC = "161000")
and t.BPID = T2.BPID
union all
select t2.NAMA, t.BPID, t.DCDT, t.COR_LEAC,
(case t.COR_DBCR when 1 then t.AMTH(1)*t.COR_SIGN when 2 then 0 end) as deb_sum,
(case t.COR_DBCR when 2 then t.AMTH(1)*t.COR_SIGN when 1 then 0 end) as cred_sum,
t.REFR, t.LEAC, t.OTYP, t.ODOC, t.OBAT, t.USER
from tfcoa106 t, tccom100 t2
where t.FYER = 2016 and t.FPRD = 6 and (t.COR_LEAC = "161001" or t.COR_LEAC = "161000")
and t.BPID = t2.BPID
)
order by cag_num
selectdo
endselect
Error SQL: SQLState 42I00: Unexpected token 'order' (error 302)

Original Oracle query works fine:
select cag_name, cag_num, docdate, leac, deb_sum, cred_sum, descr, cor_leac, ttyp, docnum, btno, cr_user
from
(select t2.T$NAMA as cag_name, t.T$BPID as cag_num, t.T$DCDT as docdate, t.T$LEAC as leac,
case t.T$DBCR when 1 then t.T$AMTH$1*t.T$SIGN when 2 then 0 end as deb_sum,
case t.T$DBCR when 2 then t.T$AMTH$1*t.T$SIGN when 1 then 0 end as cred_sum,
t.T$REFR as descr, t.T$COR_LEAC as cor_leac, t.T$TTYP as ttyp, t.T$DOCN as docnum, t.T$BTNO as btno, t.T$USER as cr_user
from baandb.ttfcoa102552 t, baandb.ttccom100552 t2
where t.T$FYER = 2016 and t.T$FPRD = 6 and (t.T$LEAC = '161001' or t.T$LEAC = '161000')
and t.T$BPID = T2.T$BPID
union all
select t2.T$NAMA, t.T$BPID, t.T$DCDT, t.T$COR_LEAC,
case t.T$COR_DBCR when 1 then t.T$AMTH$1*t.T$COR_SIGN when 2 then 0 end as deb_sum,
case t.T$COR_DBCR when 2 then t.T$AMTH$1*t.T$COR_SIGN when 1 then 0 end as cred_sum,
t.T$REFR, t.T$LEAC, t.T$TTYP, t.T$DOCN, t.T$BTNO, t.T$USER
from baandb.ttfcoa102552 t, baandb.ttccom100552 t2
where t.T$FYER = 2016 and t.T$FPRD = 6 and (t.T$COR_LEAC = '161001' or t.T$COR_LEAC = '161000')
and t.T$BPID = t2.T$BPID
union all
select t2.T$NAMA, t.T$BPID, t.T$DCDT, t.T$LEAC,
case t.T$DBCR when 1 then t.T$AMTH$1*t.T$SIGN when 2 then 0 end as deb_sum,
case t.T$DBCR when 2 then t.T$AMTH$1*t.T$SIGN when 1 then 0 end as cred_sum,
t.T$REFR, t.T$COR_LEAC, t.T$OTYP, t.T$ODOC, t.T$OBAT, t.T$USER
from baandb.ttfcoa106552 t, baandb.ttccom100552 t2
where t.T$FYER = 2016 and t.T$FPRD = 6 and (t.T$LEAC = '161001' or t.T$LEAC = '161000')
and t.T$BPID = T2.T$BPID
union all
select t2.T$NAMA, t.T$BPID, t.T$DCDT, t.T$COR_LEAC,
case t.T$COR_DBCR when 1 then t.T$AMTH$1*t.T$COR_SIGN when 2 then 0 end as deb_sum,
case t.T$COR_DBCR when 2 then t.T$AMTH$1*t.T$COR_SIGN when 1 then 0 end as cred_sum,
t.T$REFR, t.T$LEAC, t.T$OTYP, t.T$ODOC, t.T$OBAT, t.T$USER
from baandb.ttfcoa106552 t, baandb.ttccom100552 t2
where t.T$FYER = 2016 and t.T$FPRD = 6 and (t.T$COR_LEAC = '161001' or t.T$COR_LEAC = '161000')
and t.T$BPID = t2.T$BPID
)
order by cag_num
selectdo
----do somethink---
endselect


How i can solve this problem?

NPRao
5th July 2016, 18:35
From the Tools programmer's manual -

Implicitely derived select targets
If an output column of a SQL statement is not explicitly bound to a select target, then the query processor uses the derived select target. If there is no derived select target then the following error is given:

SQLState HYL06: select item <number> must be bound explicitly

The query processor defines a derived select target for an output column, if it is a column name. The name of the select target is the qualified column name of the column in the related table.

Example: The following query defines "dbtst120.empno" as the select target for the first column, and "dbtst120.firstnme" for the second column. It does not define " alias.empno" as a select target.

SELECT alias.empno, firstnme FROM dbtst120 alias
For all other output columns, such as aggregate functions, no select target is defined.

Example: The following query defines no select target for the second column.

SELECT edlevel, avg(salary) FROM dbtst120 alias
In case of a UNION, the SELECT in the first branch of the UNION defines the name of the select target.

Example: The following query defines "dbtst120.empno" as the select target for the first column, and "dbtst120.bonus" for the second column.

SELECT empno, bonus
FROM dbtst120 alias

UNION ALL

SELECT edlevel, avg(salary)
FROM dbtst120
Example: The following query defines "dbtst120.empno" as the select target. Note that "dbtst180.empno" is not a select target.

SELECT empno
FROM dbtst120

UNION ALL

SELECT empno
FROM dbtst180
A subquery does not define any select targets.

Example: The following query defines "dbtst120.empno" as the select target. Note that "dbtst180.empno" is not a select target.

SELECT empno
FROM dbtst120 a
WHERE EXISTS (
SELECT projno
FROM dbtst180 s
WHERE s.empno = a.empno
)
Note
A table field can be a program variable as well as a query variable. Note that the following query:

SELECT tccom010.*
WHERE tccom010.cuno = tccom010.cuno
has a different result from:

SELECT tccom010.*
WHERE tccom010.cuno = :tccom010.cuno
The latter selects one record; the former selects all records from the table tccom010, as tccom010.cuno by definition equals tccom010.cuno for each row.