I am at a loss of how to sort the data in the second select statement of the following union query.
The desired results would be:
C null null null null null
I xxxx xxxx xxxx 1 xxxx
I xxxx xxxx xxxx 1 xxxx
I xxxx xxxx xxxx null xxxx
I xxxx xxxx xxxx null xxxx
Here is the code:
select 'C' [Identifier]
,null [Acct/Invoice #]
,null [Check #/Pay Amount]
,null [Check Amount/Error Code]
,null [Error Flag]
,null [Trx Account #]
union all
select 'I' [Indentifer]
,final_inv_number [Invoice #]
,final_final_pay_amount [Pay Amount]
,final_trans_code [Error Code]
,case when final_is_reconciled = 1 then 1 end [Error Flag]
,case when final_parse_type = 'ERROR CREATED' or final_parse_type = 'PREV PD'
then final_account_number else null end [Trx Account #]
from #final
where final_net_pay_amount <> 0
--order by [Error Flag] ?
You are sorting the entire result, not just the records from the second select.
You can use the [Identifier] field to keep the records from the first select first: