Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Problem with the execution of SQL (SELECT UNION)

Good morning, I have the following problem:
Use the following code

Importi:

LOAD

    FIELD_1,

    FIELD_2,

    FIELD_3,

    FIELD_4,

    FIELD_5;

SQL select sum (Importo) from

(SELECT sum(FIELD_1) as Importo from $(library_path) where FIELD_2= 117030 and FIELD_5= 'D'

     union

     SELECT sum (FIELD_1)*-1 as Importo from $(library_path) where FIELD_2= 117030 and FIELD_5= 'A')

as Residuo from $(library_path);

But when I execute thce code I receive the following message:

SQL##f - SqlState: 37000, ErrorCode: 4294967097, ErrorMsg: [IBM][ODBC driver for the System i Access] [DB2 for i5/OS]SQL0199 - .FROM keyword not provided. Valid Token: FOR SKIP WITH FETCH ORDER UNION EXCEPT OPTIMIZE.

SQL select sum (Importo) from

(SELECT sum(CNRATA) as Importo from ............where CNCLIE = 117030 and CNDAVE = 'D'

     union

     SELECT sum (CNRATA)*-1 as Importo from ............where CNCLIE = 117030 and CNDAVE = 'A')

as Residuo from ............

Does anyone have any idea what the problem is?

Thanks a lot

  Giovanni


11 Replies
giakoum
Partner - Master II
Partner - Master II

if instead of using a variable you hard code the from table, does it work?

Not applicable
Author

The problem is the same. The error is related to the word from.


maxgro
MVP
MVP

try first the SQL query on your database

when it works, use the query in QlikView

also, from what I understand your query should return 1 field and in Qlik (load) you have 5 fields

giakoum
Partner - Master II
Partner - Master II

Good point

maxgro
MVP
MVP

also I don't understand the bold part

SQL

select sum (Importo) from

    (SELECT sum(FIELD_1) as Importo

    from $(library_path) where FIELD_2= 117030 and FIELD_5= 'D'

    union

    SELECT sum (FIELD_1)*-1 as Importo

    from $(library_path) where FIELD_2= 117030 and FIELD_5='A')

    as Residuo from $(library_path);

jonathandienst
Partner - Champion III
Partner - Champion III

Hi

I think that is the problem - one too many 'froms'. As Massimo said, test this and get it working in SQLMS first, then try it in QV with literals, and then when that is working replace the literals with the variables.

HTH

Jonathan

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
Anonymous
Not applicable
Author

Giovanni,

I guess you mean this:

SQL select sum (Importo) as Residuo from

(SELECT sum(FIELD_1) as Importo from $(library_path) where FIELD_2= 117030 and FIELD_5= 'D'

     union

     SELECT sum (FIELD_1)*-1 as Importo from $(library_path) where FIELD_2= 117030 and FIELD_5= 'A')

;

But I have no idea what you preceding load is about (?)

Not applicable
Author

Yes, the error was in the from $(library_path) at the end.


Thanks.

Not applicable
Author

Good morning, the code tha I use is the following:

Importi:

LOAD

    *;

SQL SELECT sum (Importo) from

(    SELECT sum(CNRATA) as Importo from $(library_path) WHERE CNCLIE = '117030' and CNDAVE = 'D'             

     union

    SELECT sum (CNRATA)*-1 as Importo from $(library_path) a where CNCLIE = 117030 and CNDAVE = 'A'

) as Residuo;

I can't understand why the result where the name of the field is 00001 and not Residuo.

Why ?