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

  

原文地址:https://www.cnblogs.com/dongyaotou/p/12350614.html

时间: 2024-08-28 04:15:41

ORACLE数据库--order by----ORA-01785 ORDER BY必须是select-list 表达式的数目的相关文章

PlSql链接Oracle数据库对tnsnames.ora文件的配置

1.打开tnsnames.ora文件的位置 D:\ora_product\11.2.0\dbhome_1\NETWORK\ADMIN 2.文件中写入 ORCL = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) ) 108_XRORCL = (DESCRIPTION = (A

调整oracle数据库编码

新装数据库可能需要调整oracle数据库编码,否则在导入数据是可能会出错. oracle11g更改字符集AL32UTF8为ZHS16GBK Sql代码 Database character set (AL32UTF8) and Client character set (ZHS16GBK) are different. Character set conversion may cause unexpected results. Note: you can set the client chara

In Oracle 11g, how to change the order of the results of a sql without “order by”?(转)

oracle 11g 当sql语句中不加order by的时候,好像是按rowid的顺序返回结果的.我也看过一些相关的文档,oracle的官方意思就是不加order by,就不保证输出的顺序. 那么,问题来了:如果现在我select XXX,一组结果出来,顺序是.......A....B.....那么接下来我做什么操作,再做同样一句select,怎么才能让结果变成....B...A.....Notice1: 操作中请勿修改表的主键,不要用DDL,只用select * from A;得到结果Not

oracle系统包——dbms_random用法及order by 小结(转)

dbms_random是一个可以生成随机数值或者字符串的程序包. 这个包有initialize().seed().terminate().value().normal().random().string()等几个函数,但value()是最常用的,下面依次介绍各个函数的详细用法: 1.dbms_random.value方法 第一种:FUNCTION value RETURN NUMBER; 说明: 这种用法没有参数,会返回一个具有38位精度的数值,范围从0.0到1.0,但不包括1.0. 示例: S

Oracle系列:(7)order by子句

查询员工信息(编号,姓名,月薪,年薪),按月薪升序排序,默认升序,如果月薪相同,按oracle内置的校验规则排序 select empno,ename,sal,sal*12  from emp  order by sal asc; 查询员工信息(编号,姓名,月薪,年薪),按月薪降序排序 select empno,ename,sal,sal*12  from emp  order by sal desc; 查询员工信息,按入职日期降序排序,使用列名 select empno,ename,sal,h

SQL Server数据库--》top关键字,order by排序,distinct去除重复记录,sql聚合函数,模糊查询,通配符,空值处理。。。。

top关键字:写在select后面 字段的前面 比如你要显示查询的前5条记录,如下所示: select top 5 * from Student 一般情况下,top是和order by连用的 order by用在表名的后面 order by后面就填你要按它排序的字段,是升序排序(从小到大排序) percent:百分比 %=percent 比如要显示5%的数据处理,不能写top 5%,而是top 5 percent 提示:如果top 5 percent出来的数是3.1条数据的话,是取四条数据,而不

数据库学习笔记3 基本的查询流 2 select lastname+&#39;,&#39;+firstname as fullname order by lastname+&#39;,&#39;+firstname len() left() stuff() percent , select top(3) with ties

order by子句对查询结果集进行排序 多列和拼接 多列的方式就很简单了 select firstname,lastname from person.person order by lastname,firstname; 这句话表示根据lastname和firstname两列进行排序,并且是先按照lastname进行排序如果有相同的值就按照firstname进行排序. 拼接很有意思,可以写成这个样子 select lastname+','+firstname as fullname from

Oracle 数据库日常巡检

Oracle 数据库日常巡检 阅读目录 1. 检查数据库基本状况 2. 检查Oracle相关资源的使用情况 3. 检查Oracle数据库备份结果 4. 检查Oracle数据库性能 5. 检查数据库cpu.I/O.内存性能 6. 检查数据库安全性 7. 其他检查 回到顶部 1. 检查数据库基本状况 包含:检查Oracle实例状态,检查Oracle服务进程,检查Oracle监听进程,共三个部分. 1.1. 检查Oracle实例状态 select instance_name,host_name,sta

Oracle数据库学习笔记

创建表的同时插入数据:create table zhang3 as select * from zhang1;create table zhang3(id,name) as select * from zhang1; 将查询数据插入到某个表中:insert into zhang3 select * from zhang1;insert into zhang3(id,name) select * from zhang1; 有关完整性约束,表名,列名的两个视图:user_constraints, u