问题
i have problem how to ascending the data according the list. For example, i have a table field names mhn.kod_urusan. i want to show the result according the list that i type.The problem is the data not follow what i type in the list. "and mhn.kod_urusan in ('PBPTG','PBMT')"
This is my query:-
select LISTAGG (upper(aa.kod_urusan), ', ') within Group (order by aa.kod_urusan asc) as daerah
from
(select distinct
mhn.kod_urusan,kc.nama nm
from
mohon mhn, kod_urusan ku, kod_caw kc
where
mhn.kod_urusan = ku.kod(+)
and mhn.kod_caw = kc.kod(+)
and (mhn.trh_masuk <= sysdate )
and mhn.kod_urusan in ('PBPTG','PBMT')
and mhn.kod_caw = '01'
order by mhn.kod_urusan asc )aa
This is the result:-
--Daerah--
PBMT, PBPTG
Anyone know what the problem?
回答1:
PBMT, PBPTG is sorted ascending. If you want it reversed, sort descending.
回答2:
PBMT is shorter than PBPTG so result is ok
if you need custom sort order, you have to add some int column to table mohon
, let it be int myordercol
;
for all different values in kod_urusan
you should use queries like:
update mohon set myordercol = 1 where kod_urusan='PBPTG';
update mohon set myordercol = 2 where kod_urusan='PBMT';
.. and so on, so in such way you're establishing custom order for this columns
after this you will need to change your order by clauses to use this column as sorting field
来源:https://stackoverflow.com/questions/19531632/order-by-listagg-is-not-ascending-according-the-list-data