之前,同事在编写视图的过程中遇到这样了这个错误。我把简化后的语句整理如下:

   1: select

   2: '2016' as nf,

   3: qxdm,

   4: round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

   5: from dltb_2016@dblink_td_tdxz m where dlmc='城市'

   6: group by m.qxdm order by m.qxdm

   7:  

   8: union all 

   9:  

  10: select

  11: '2017' as nf,

  12: qxdm,

  13: round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

  14: from dltb_2017@dblink_td_tdxz n where dlmc='城市'

  15: group by n.qxdm order by n.qxdm

主要是查询各个管辖区中2016年和2017年地类图斑数据中城市用地的面积,语句分单块均可以执行成功,但是使用UNION后则出现ora-00933错误。

检查了列的数量、数据格式均保持一致,没有不对应的现象。

追查了一下原因,最终发现是union和order by字句引起的。

最终处理方式参考如下:

1、如果排序没必要,可以直接去掉,或者在union后统一排序

   1: select

   2: '2016' as nf,

   3: qxdm,

   4: round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

   5: from dltb_2016@dblink_td_tdxz m where dlmc='城市'

   6: group by m.qxdm 

   7:  

   8: union all 

   9:  

  10: select

  11: '2017' as nf,

  12: qxdm,

  13: round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

  14: from dltb_2017@dblink_td_tdxz n where dlmc='城市'

  15: group by n.qxdm

或者

   1: select

   2: '2016' as nf,

   3: qxdm,

   4: round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

   5: from dltb_2016@dblink_td_tdxz m where dlmc='城市'

   6: group by m.qxdm 

   7:  

   8: union all 

   9:  

  10: select

  11: '2017' as nf,

  12: qxdm,

  13: round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

  14: from dltb_2017@dblink_td_tdxz n where dlmc='城市'

  15: group by n.qxdm order by qxdm

2、可以再嵌套一层查询

   1: select * from (

   2: select

   3: '2016' as nf,

   4: qxdm,

   5: round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

   6: from dltb_2016@dblink_td_tdxz m where dlmc='城市'

   7: group by m.qxdm order by m.qxdm

   8: )

   9:  

  10: union all 

  11:  

  12: select * from (

  13: select

  14: '2017' as nf,

  15: qxdm,

  16: round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

  17: from dltb_2017@dblink_td_tdxz n where dlmc='城市'

  18: group by n.qxdm order by n.qxdm

  19: )

或者

   1: with

   2:  s1 as (

   3:  select

   4:        '2016' as nf,

   5:        qxdm,

   6:        round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

   7:        from dltb_2016@dblink_td_tdxz m where dlmc='城市'

   8:        group by m.qxdm order by m.qxdm

   9:   ),

  10:   s2 as (

  11:   select

  12:      '2017' as nf,

  13:      qxdm,

  14:      round(sum(tbdlmj)/10000,2) as csydmj--单位转换,2位小数

  15:      from dltb_2017@dblink_td_tdxz n where dlmc='城市'

  16:      group by n.qxdm order by n.qxdm

  17:   )

  18:   select * from s1

  19:   union all

  20:   select * from s2;

同时使用Union和Order by问题(ORA-00933错误)解决的更多相关文章

  1. 让UNION与ORDER BY并存于SQL语句当中

    在SQL语句中,UNION关键字多用来将并列的多组查询结果(表)合并成一个结果(表),简单实例如下: SELECT [Id],[Name],[Comment] FROM [Product1] UNIO ...

  2. mysql 错误 1221 Incorrect usage of union and order by

    今天有个项目出现了问题 问题原因是union和order by 的问题.关于这个问题的解决方案可以猛击下面的链接. http://blog.csdn.net/gtuu0123/article/deta ...

  3. ORA-00933 UNION 与 ORDER BY

    原文:http://blog.csdn.net/lwei_998/article/details/6093807 The UNION operator returns only distinct ro ...

  4. mysql中的union和order by、limit

      我有一个表 CREATE TABLE `test1` (  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(20) N ...

  5. MySQL中union和order by一起使用的方法

    MySQL中union和order by是可以一起使用的,但是在使用中需要注意一些小问题,下面通过例子来说明.首先看下面的t1表. 1.如果直接用如下sql语句是会报错:Incorrect usage ...

  6. MySql union与order by

    [MySql union与order by] 如果您想使用ORDER BY或LIMIT子句来对全部UNION结果进行分类或限制,则应对单个地SELECT语句加圆括号,并把ORDER BY或LIMIT放 ...

  7. MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)

    我在一个业务中采用了按月的分表策略,当查询的条件跨月的时候,使用了union all汇总2个表的数据,并按插入时间倒序排列.查询并不复杂,但是当执行的时候却报错了. SELECT * FROM `ta ...

  8. union 和order by 使用时排序不正确

    静态专题和APP版专题(order by不起作用): [query] sql=(select sp_f13577,sp_f13576 from sp_t113 where url_1 not like ...

  9. mysql Incorrect usage of UNION and ORDER BY 错误备忘

    出现这个错误的语句是酱紫的 select xxx from aaa order by xxx union all select yyy from bbb order by yyy 错误原因居然是,如果 ...

随机推荐

  1. 用Java代码列出一个目录下所有的文件

    1.File类 File类在java.io.File包中,所以要导入这个包. File类中用到的方法: boolean isDirectory()       测试此抽象路径名表示的文件是否是个目录 ...

  2. Jmeter接口测试实例2-获取所有学生信息

    Jmeter实例2:获取所有学生信息 添加http协议—添加IP.路径.方法,添加信息头管理器,察看结果树,运行 如下图所示,响应结果中获取到所有学生信息

  3. 读取html文件,让其中的内容和notepad打开这个html的样子一样。

    然后我写了个python代码,让其读取这个html文件后,内容和这个一样: htmlf=open('13144815898.html','r',encoding="utf-8") ...

  4. Zepto tap 穿透bug、解决移动端点击穿透问题

    当两个层重叠在一起时,或是有个弹窗,使用Zepto的tap事件时,点击上面的一层时会触发下面一层的事件,特别是底层如果是input框时,必“穿 透”,“google”说原因是“tap事件实际上是在冒泡 ...

  5. 转 ef中使用mysql步骤--Entity Framework 6 with MySql

    原文:http://lvasquez.github.io/2014/11/18/EntityFramework-MySql/ For the Entity Framework 6 support we ...

  6. SpringCloud无废话入门03:Feign声明式服务调用

    1.Feign概述 在上一篇的HelloService这个类中,我们有这样一行代码: return restTemplate.getForObject("http://hello-servi ...

  7. 遭遇ASP.NET的Request is not available in this context

    如果ASP.NET程序以IIS集成模式运行,在Global.asax的Application_Start()中,只要访问Context.Request,比如下面的代码 var request = Co ...

  8. jQuery on()方法使用

    jQuery on()方法 基本语法: 语法结构一: $(selector).on(event,function) 语法结构二: $(selector).on(events,[selector],[d ...

  9. codevs 2033 邮票

    洛谷 P2725 邮票 Stamps codevs 2033 邮票 题目链接 http://codevs.cn/problem/2033/ https://www.luogu.org/problemn ...

  10. Effective Java 第三版—— 87. 考虑使用自定义序列化形式

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...