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%'