There is a way to order one SQL union file?

5 pts.
Tags:
RPG
RPG ILE
SQL/400
I have to conect 2 files, the current and the historic. Both has the same structure but the fields names are diferent. This is the complete command but i can not order to put the records in a subfile in the order that i need: C/EXEC SQL C+ DECLARE S1 CURSOR FOR SELECT IQUSER, IQFMOV, IQNUME, IQANO, C+ IQMES, IQITEM, IQDESC , IQCAMO, CMRFCT, CMRORD, CMROCQ, C+ IHISTOR.IHDCRE, IHISTOR.IHCAMO, IHISTOR.IHCDTR FROM C+ ICFILE/IORDCOM LEFT JOIN PMFILE/CMOVRCS ON IQNUME = CMRNMR AND C+ IQITEM = CMRITE AND CMRSTS <> 'C' LEFT join ICFILE/IHISTOR on C+ iqtpit =IHTPIT AND iqitem = IHITEM AND iqnume = DEC(IHCTST) AND C+ DEC(IQSEQ) = (DEC((IHCTST-DEC(IHCTST))*100000000) ) WHERE IQANO = C+ : WAAFIN AND IQMES = : WMMFIN AND IQTPIT = : XQTPIT AND IQUSER = C+ : YUSER UNION SELECT IQHSER, IQHMOV, IQHUME, IQHNO, IQHES, C+ IQHTEM, IQHESC , IQHAMO, CMRFCT, CMRORD, CMROCQ, IHISTOR.IHDCRE, C+ IHISTOR.IHCAMO, IHISTOR.IHCDTR FROM ICFILE/IORDHIS LEFT JOIN C+ PMFILE/CMOVRCS ON IQHUME = CMRNMR AND IQHTEM = CMRITE AND CMRSTS C+ <> 'C' LEFT join ICFILE/IHISTOR on iqHpit =IHTPIT AND iqHtem = C+ IHITEM AND iqHume = DEC(IHCTST) AND DEC(IQHEQ) = C+ (DEC((IHCTST-DEC(IHCTST))*100000000) ) WHERE IQHNO = : WAAFIN AND C+ IQHES = : WMMFIN AND IQHPIT = : XQTPIT AND IQHSER = : YUSER C/END-EXEC

Answer Wiki

Thanks. We'll let you know when a new response is added.

I think you could use the column position to order you result set.
Something like this:

select col_a,col_b,col_c from … where …
UNION
select col_d,col_e,col_f from … where …
ORDER BY 1,3

In this example the result will be ordered by the first column (col_a for the first query and col_d for the second one) and the third column (col_c for the first query and col_f for the second one).

Hope this helps.

Cool, thats much better then my answer. — phil

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following