ORDER BY is not sorting correctly. Help, please!

Database:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64 bit Production
PL/SQL Release 9.2.0.4.0 - Production
CORE Production 9.2.0.3.0

I think that the problem is related to the field of OPERATION.
Order By properly sorts if I excluded all other fields except for POINT and EXPLOITATION,
If I order by OPERATION and all other fields in my Order By clause, then the sorting is done correctly. Instead of DESC, it sorts in the order of the CSA.

FIELD INFO:
TRANSACTION is defined as Varchar2 (4)
select dtls.description
, dtls.item
, dtls.revision
, upper(dtls.serial) as serial
, dtls.wo_num
, dtls.wo_line
, dtls.operation
, '' as priorOp
, dtls.ccn
, dtls.mas_loc
, dtls.ord_qty
, dtls.compl_qty
, dtls.lab_seq
, dtls.outside
, trim(rt.oper_text) as oper_text
from
(
select unique labor.description
, labor.item
, labor.revision
, case when (trim(labor.serial) is null AND trim(cs.c_ss_serial) is not null) then cs.c_ss_serial
when (trim(cs.c_ss_serial) is null AND trim(labor.serial) is not null) then labor.serial
else labor.serial
end as serial
, labor.wo_num
, labor.wo_line
, trim(labor.operation) as operation
, labor.ccn
, labor.mas_loc
, labor.ord_qty
, labor.compl_qty
, labor.lab_seq
, labor.rtg_seq
, labor.bcr_type
, labor.outside
from
(
select trim(head.description) as description
, head.item
, head.revision
, head.serial
, head.wo_num
, head.wo_line
, head.operation
, head.ccn
, head.mas_loc
, head.ord_qty
, l.lab_complete_qty as compl_qty
, l.lab_seq
, '0001' as rtg_seq
, 'CUR' as bcr_type
, '' as outside

from (
select lab.*, sr.serial
from (
select i.description, lb.*, wo.item, wo.revision, wo.ord_qty
from (
select lt.*, d.operation
from
(
select c.wo_num, c.wo_line, max(c.lab_seq) as lab_seq
,c.ccn, c.mas_loc
from lab_tim c
where c.ccn = '1' and c.mas_loc = '1' and c.wo_num not like '%INDIRECT%' and c.lab_tran_date between to_date('3/1/2009', 'mm/dd/yyyy') and to_date('3/10/2009', 'mm/dd/yyyy') --and trim(c.wo_num) = '55302'

group by c.ccn, c.mas_loc, c.wo_num, c.wo_line

) lt,
lab_tim d
WHERE lt.ccn = d.ccn
and lt.mas_loc = d.mas_loc
and lt.wo_num = d.wo_num
and lt.wo_line = d.wo_line
and lt.lab_seq = d.lab_seq
order by lt.wo_num, lt.wo_line, d.operation asc
) lb
,wo , item i
WHERE lb.ccn = wo.ccn
and lb.mas_loc = wo.mas_loc
and lb.wo_num = wo.wo_num
and lb.wo_line = wo.wo_line
AND wo.mfg_close_date is null
AND wo.REL_DATE is not null
AND (wo.ord_qty - wo.scrap_qty - wo.complete_qty) > 0
and wo.STATUS 'C'
and wo.STATUS 'U'
and wo.item = i.item
and wo.revision = i.revision
AND ( i.pc ='2050' ) ) lab, (
select distinct i.description , wo.item,wo.revision, it.serial, wo.wo_num, wo.wo_line, wo.ccn, wo.mas_loc
from itm_wosr it, wo, item i
where it.ccn ='1' and it.mas_loc = '1' and it.wo=wo.wo_num
and wo.wo_line = wo.wo_line
and it.ccn = wo.ccn
and it.mas_loc = wo.mas_loc
and wo.status = 'I'
and i.item = wo.item AND ( i.pc ='2050' ) group by wo.ccn, wo.mas_loc, wo.item, wo.revision, wo.wo_num, wo.wo_line, it.serial,i.description )sr

WHERE lab.ccn = sr.ccn (+)
and lab.mas_loc = sr.mas_loc (+)
and lab.wo_num = sr.wo_num (+)
and lab.wo_line = sr.wo_line (+)
and lab.item = sr.item (+)
and lab.revision = sr.revision (+)
)head
, lab_tim l

WHERE head.ccn = l.ccn (+)
and head.mas_loc = l.mas_loc (+)
and head.wo_num = l.wo_num (+)
and head.wo_line = l.wo_line (+)
and head.operation = l.operation (+)
and head.lab_seq = l.lab_seq (+)
group by head.description
, head.item
, head.revision
, head.wo_num
, head.wo_line
, head.ord_qty
, head.operation
, head.ccn
, head.mas_loc
, head.serial
, l.lab_complete_qty
, l.lab_seq
) labor
,c_ss cs
WHERE labor.ccn = cs.ccn (+)
and labor.mas_loc = cs.mas_loc (+)
and labor.wo_num = cs.wo (+)
and labor.wo_line = cs.wo_line (+)

UNION ALL
-----------------------END JUST LABOR -----
-----------------------BEING OUTSIDE PROCESSING 
-----
select o.*
from
( --these are parts that are still in the realm the shipping/receiving department.

Select distinct WOs. description, WOs.item, WOs.revision, WOs.serial, WOs.wo_num, WOs.wo_line
,WOs.operation, WOs.ccn, WOs.mas_loc, WOs.ord_qty, WOs.compl_qty
, '0000' as lab_seq
, '0001' as rtg_seq
, 'CUR' as bcr_type
, WOS.outside
From
( ---Max labor on these (potentially) outside parts

select shp.description, shp.item, shp.revision,max(lt.operation) as operation
,shp.wo_num, shp.wo_line, shp.serial, shp.outside, shp.ccn, shp.mas_loc
from
( ---Outside Proc Only-----Used to get the Work Orders Of Interest

select i.description, i.item, i.revision, wit.serial, wit.wo_num, wit.wo_line, max(wit.operation) as operation
, wit.ccn, wit.mas_loc, wit.ord_qty, 1 as compl_qty, '0000' as lab_seq, '0001' as rtg_seq, 'CUR' as bcr_type
, wit.outside
from (
select wo.wo_num, wo.wo_line,wo.item, wo.revision, wo.ord_qty, ot.ccn, ot.mas_loc, ot.operation
,ot.outside, ot.serial
from (
select a.*
from
(
select c.ccn, c.mas_loc, c.wo as wo_num, c.wo_line,
case
when trim(cs.operation) is null then '0'
else cs.operation
end as operation
,case
when trim(c.receiver) is null then '(Out)'
else '(Rcvd)'
end as outside
, c.c_ss_serial as serial
from c_ss c, c_shipop cs
where c.c_ss_serial is not null
and c.ccn = cs.pur_ccn
and c.mas_loc = cs.mas_loc
and trim(c.receiver) is null
and c.ccn = '1' and c.mas_loc = '1' -- and cs.dat_shipped > to_date('9/10/2008', 'mm/dd/yyyy')

and c.c_shipop_ship_num = cs.c_shipop_ship_num
and trim(cs.operation) '290.'
-- and trim(c.wo) = '55302'
--604ms
) a

order by a.wo_num, a.wo_line, a.operation
) ot
, wo
WHERE ot.ccn = '1' and ot.ccn = wo.ccn and ot.mas_loc = wo.mas_loc
and ot.wo_num = wo.wo_num
and ot.wo_line = wo.wo_line
AND wo.mfg_close_date is null AND wo.REL_DATE is not null
AND (wo.ord_qty - wo.scrap_qty - wo.complete_qty) > 0
and wo.STATUS = 'I'
order by wo.item, wo.revision, ot.operation ) wit
,item i
WHERE wit.item = i.item
and wit.revision = i.revision
and i.pc 'boogayaagaa'
AND ( i.pc ='2050' ) GROUP BY i.description, i.item, i.revision, wit.serial, wit.wo_num, wit.wo_line
, wit.ccn, wit.mas_loc, wit.ord_qty, wit.outside
order by i.item, i.revision, operation --<--slows query to 13 seconds from 607ms
--607 ms

)shp
, lab_tim lt
where shp.ccn = '1' and shp.mas_loc = '1' and shp.ccn = lt.ccn
and shp.mas_loc = lt.mas_loc
and shp.wo_num = lt.wo_num
and shp.wo_line = lt.wo_line
and shp.lab_seq < lt.lab_seq
and shp.ccn = lt.ccn
and shp.mas_loc = lt.mas_loc
and lt.wo_num not like '%INDIRECT%'
and trim(lt.operation) is not null
and trim(lt.operation) '9000' --2/27/09

and trim(lt.operation) '9050' --2/27/09

group by shp.ccn, shp.mas_loc, shp.wo_num, shp.wo_line, shp.operation, shp.item, shp.revision, shp.description
, shp.outside , shp.serial
)lab,
----*---*---*---*-------*---*---*---*--END OUTSIDE AND LABOR-----*---*---*---*-------*---*---*---*-------
----*---*---*---*-------*---*---*---*-BEING OUTSIDE NO LABOR------*---*---*---*-------*---*---*---*-------

(
--Get largest operation sent out

select i.description, i.item, i.revision, wit.serial, wit.wo_num, wit.wo_line, max(wit.operation) as operation
, wit.ccn, wit.mas_loc, wit.ord_qty, 1 as compl_qty, '0000' as lab_seq, '0001' as rtg_seq, 'CUR' as bcr_type
, wit.outside
from (
select wo.item, wo.wo_num, wo.wo_line, wo.revision, wo.ord_qty, ot.ccn, ot.mas_loc, ot.operation
,ot.outside, ot.serial
from (
select a.*
from
(
select c.ccn, c.mas_loc, c.wo as wo_num, c.wo_line,
case
when trim(cs.operation) is null then '0'
else cs.operation
end as operation
,case
when trim(c.receiver) is null then '(Out)'
else '(Rcvd)'
end as outside
, c.c_ss_serial as serial
from c_ss c, c_shipop cs
where c.c_ss_serial is not null
and c.ccn = cs.pur_ccn
and c.mas_loc = cs.mas_loc
and trim(c.receiver) is null
and c.ccn = '1' and c.mas_loc = '1' -- and cs.dat_shipped > to_date('9/10/2008', 'mm/dd/yyyy')
and c.c_shipop_ship_num = cs.c_shipop_ship_num
and trim(cs.operation) '290.'
--and trim(c.wo) = '55302'
--604ms
) a

order by a.wo_num, a.wo_line, a.operation
) ot
, wo
WHERE ot.ccn = '1' and ot.ccn = wo.ccn and ot.mas_loc = wo.mas_loc
and ot.wo_num = wo.wo_num
and ot.wo_line = wo.wo_line
AND wo.mfg_close_date is null AND wo.REL_DATE is not null
AND (wo.ord_qty - wo.scrap_qty - wo.complete_qty) > 0
and wo.STATUS = 'I' -- in process
order by wo.item, wo.revision, ot.operation ) wit
,item i
WHERE wit.item = i.item
and wit.revision = i.revision
and i.pc 'boogayaagaa'
AND ( i.pc ='2050' ) GROUP BY i.description, i.item, i.revision, wit.serial, wit.wo_num, wit.wo_line
, wit.ccn, wit.mas_loc, wit.ord_qty, wit.outside
order by i.item, i.revision, operation --<--slows query to 13 seconds from 607ms
--607 ms

) WOs -- outside items
WHERE WOs.operation > lab.operation
and WOs.ccn = lab.ccn
and WOs.mas_loc = lab.mas_loc
and WOs.wo_num = lab.wo_num
and WOs.wo_line = lab.wo_line
and WOs.serial = lab.serial
)o

order by item, revision, wo_num, wo_line 


) dtls
,rtg_text rt
where dtls.ccn = rt.ccn (+)
and dtls.item = rt.item (+)
and dtls.revision = rt.revision (+)
and dtls.OPERATION = RT.OPERATION (+)
and dtls.rtg_seq = rt.rtg_seq (+)
and dtls.bcr_type = rt.bcr_type (+)
order by dtls.item, rt.operation,dtls.wo_num desc
Published by: NCR March 10, 2009 08:50

Hello

Wow, 441 lines unformatted SQL.

Why do you have several ORDER BY?

Why do you use DISTINCT and GROUP BY at the same time?

BTW,

order by dtls.item, rt.operation, dtls.wo_num desc

Simply means that dtls.wo_num is DESCENDING, rest is GROWING. That means that it is the same as

order by dtls.item ASC, rt.operation ASC, dtls.wo_num DESC

So maybe ORDER BY is sort properly after all? -Just a guess

Concerning

Peter

Published by: Peter on March 10, 2009 08:56
-Added a note on ASC/DESC

Tags: Database

Similar Questions

Maybe you are looking for

  • Satellite A80-154 / 3-4-3 beep problem

    Hello When I try to turn on the laptop, he made a series of beeps, 3-4-3 (short beeps, anyway) and the laptop is blocking down, but the Start button remains light, I need to hold down 5 seconds to turn off the laptop... In the past, I had this proble

  • PlayStation Network sucks!

    I'm Livid angry. My games doesn't work on the PSN anymore and PSN decided to try to violate my constitutional right to civil action through class action. These freaks sent a 3000 + terms of service for my son 13 years and his daughter as if they woul

  • Do not open a guest account of LabVIEW

    LabVIEW SP11 cannot be opened in the guest account. Tried to change settings in the system registry, but in vain.

  • Get the error code 0 X 00000015 when you try to scan

    Original title: Scan problem Tried to scan HP 2100 connected directly to the USB port of the laptop. OK but previewed such access has been denied at the point of import in the process. error code 0 X 00000015 - is this a problem of security settings?

  • Strange glitch video HELP!

    OK, so I was watching a video on my ROCKET and I turned it off in the middle of the clip. When I went to turn it back on, he had an image of the scene where I left off in what looked like a MUSIC - reading the display (photo Album, chronology, title