ORACLE數據庫--order by----ORA-01785 ORDER BY必須是select-list 表達式的數目


sql語句真是博大精深啊!

下面是報錯的sql語句:

select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       lb.sid,
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where '1582443083000' = '1582443083000'
   and cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and lb.ruleid <> '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date'2020-02-23'
   and to_date(lb.create_time) <= date'2024-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'
union
select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       max(lb.sid),
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and exists (select 1
          from lpedoritem
         where contno = lb.contno
           and edorstate = '0'
           and edortype = 'PT')
   and lb.ruleid = '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date'2020-02-23'
   and to_date(lb.create_time) <= date'2024-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'
   and rownum=1
   group by  lb.contno,
       cont.conttype, cont.appntno, cont.insuredno,
      cont.appntname, cont.insuredname,
       lb.create_time,
       lb.sum_total,
       lb.sum_price,
       lb.sum_tax,       
       lb.managecom
   order by  max(lb.sid) desc
union
select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       lb.sid,
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and not exists (select 1
          from lpedoritem
         where contno = lb.contno
           and edorstate = '0'
           and edortype = 'PT')
   and lb.ruleid = '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date'2020-02-23'
   and to_date(lb.create_time) <= date'2024-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'

  運行之后:ORA-01785  ORDER BY必須是select-list 表達式的數目

我理解的是多個union連接的時候是不能夠使用order by進行排序的,因為他不可能按照每一塊給你去單獨的進行排序。因為有很多主的查詢語句。當然如果只有一個主的查詢語句是可以的

接下來,我進行了改造,發現還是不行:報ora-00907缺失右括號

select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       lb.sid,
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where '1582443083000' = '1582443083000'
   and cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and lb.ruleid <> '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date'2020-02-23'
   and to_date(lb.create_time) <= date'2024-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'
union
select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       lb.sid,
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and exists (select 1
          from lpedoritem
         where contno = lb.contno
           and edorstate = '0'
           and edortype = 'PT')
   and lb.ruleid = '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date'2020-02-23'
   and to_date(lb.create_time) <= date'2024-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'
   and lb.sid=(select sid from lis_busi_transactions where contno=lb.contno and rownum=1 order by sid)
union
select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       lb.sid,
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and not exists (select 1
          from lpedoritem
         where contno = lb.contno
           and edorstate = '0'
           and edortype = 'PT')
   and lb.ruleid = '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date'2020-02-23'
   and to_date(lb.create_time) <= date'2024-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'

  原因還是,

and lb.sid=(select sid from lis_busi_transactions where contno=lb.contno and rownum=1 order by sid)  這樣寫還是不能夠按照你想象中的場景進行排序的。(具體原因自己也不是很清楚)
下面這種寫法是可以的:
select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       lb.sid,
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where '1582439926000' = '1582439926000'
   and cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and lb.ruleid <> '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date
 '2020-02-23'
   and to_date(lb.create_time) <= date '2023-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'

union
select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       lb.sid,
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and exists (select 1
          from lpedoritem
         where contno = lb.contno
           and edorstate = '0'
           and edortype = 'PT')
   and lb.ruleid = '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date
 '2020-02-23'
   and to_date(lb.create_time) <= date
 '2023-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'
     
   and lb.sid = (select max(sid) from lis_busi_transactions where contno=lb.contno and ruleid='16'   )
 
/* group by lb.contno,
          lb.managecom,
          cont.conttype,
          cont.appntno,
          cont.insuredno,
          cont.appntname,
          cont.insuredname,
          lb.create_time,
          lb.sum_total,
          lb.sum_price,
          lb.sum_tax*/

union
select lb.contno,
       decode(cont.conttype, '1', cont.appntno, '2', cont.insuredno),
       decode(cont.conttype, '1', cont.appntname, '2', cont.insuredname),
       to_date(lb.create_time),
       to_char(lb.sum_total, '9999999990.99'),
       to_char(lb.sum_price, '9999999990.99'),
       to_char(lb.sum_tax, '9999999990.99'),
       lb.sid,
       lb.managecom
  from LIS_BUSI_TRANSACTIONS lb, lccont cont
 where cont.contno = lb.contno
   and lb.invoiceflag in ('00')
   and lb.successflag = '1'
   and not exists
 (select 1 from lcgrpcont c where c.grpcontno = lb.contno)
   and lb.sum_total > 0
   and (cont.currency = '01' or cont.currency is null)
   and not exists (select 1
          from ljagetendorse a, LIS_BUSI_TRANSACTIONS b
         where a.actugetno = b.sourceid
           and a.getflag = '1'
           and b.ruleid in ('3', '6', '10', '12')
           and a.actugetno = lb.sourceid
           and b.sid = lb.sid)
   and exists (SELECT 1 FROM ljapay d where d.otherno = cont.contno)
   and not exists (select 1
          from lpedoritem
         where contno = lb.contno
           and edorstate = '0'
           and edortype = 'PT')
   and lb.ruleid = '16'
   and lb.contno = 'P442011830004'
   and to_date(lb.create_time) >= date'2020-02-23'
   and to_date(lb.create_time) <= date'2023-02-23'
   and lb.managecom like '86%'
   and lb.managecom like '86%'

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM