暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 格式化负值以正确排序,但保留格式

askTom 2017-11-09
499

问题描述

我有一个旧的和新的查询。我需要新的帮助。旧的查询工作正常。对于新的,我似乎找不到一种方法来格式化两列 (纬度和经度,我需要小数点后的6位数字),因为它们排序正确。

是正确工作的旧查询,顺序中的 “to_number” 函数在这个SQL中工作,但我在新的SQL中尝试它时出错,可能是因为联合:

选择一个.platformid,
修剪 (to_char(a.latitude,'90.0000000')) 作为纬度,
修剪 (to_char(a.经度,'990.0000000')) 作为经度,
一个月,
修剪 (to_char(a.watertemperature,'990.00')) 作为水温,
修剪 (to_char(a.Water盐度,“990.000”)) 作为水盐度,
修剪 (to_char(a.currentspeed,'9990.00')) 为currentspeed,
a.防水深度
来自海洋a
内连接表面b
在a.platformid = b.platformid
和纬度 = 纬度
和a.经度 = b.经度
其中b.插入时间> = 到日期 (“20171107100000”,“yyyymmddhh24miss”)
和b.插入时间 <= 到日期 (“20171108100000”,“yyyymmddhh24miss”)
和b.distributionCd = 'A'
按a.platformid、to_number(a.纬度) 、to_number(a.经度) 、a.waterobsdepth排序

这是产生正确数据的新查询,但是负值的纬度和经度排序不正确,因为它们是字符。新查询的原因是包括质量表中添加的原始日期之后的数据。我尝试使用to_number,但得到各种错误。我尝试在子查询中没有任何格式,而只在顶部选择中设置格式,但是由于不匹配联合而出现错误。我尝试使用to_number来格式化数字,但它不保留小数点后的6位数字。有什么想法吗?经度和纬度列是表中的数据类型 = 数字 (12,7)。
如果我不使用to_char进行格式化,则无法获得小数点后的6位数字。

选择tb1.platformid,
Tb1.纬度,
Tb1.经度,
Tb1.月,
Tb1。水温,
Tb1。水盐度,
tb1.currentspeed,
tb1.waterobsdepth
来自
(选择一个.platformid,
修剪 (to_char(a.latitude,'90.0000000')) 作为纬度,
修剪 (to_char(a.经度,'990.0000000')) 作为经度,
一个月,
修剪 (to_char(a.watertemperature,'990.00')) 作为水温,
修剪 (to_char(a.Water盐度,“990.000”)) 作为水盐度,
修剪 (to_char(a.currentspeed,'9990.00')) 为currentspeed,
a.防水深度
来自海洋a, SURFACE b
其中b.插入时间> = 到日期 (“20171107100000”,“yyyymmddhh24miss”)
和b.插入时间 <= 到日期 (“20171108100000”,“yyyymmddhh24miss”)
和a.platformid = b.platformid
和纬度 = 纬度
和a.经度 = b.经度
和b.distributionCd = 'A') tb1
联盟
(选择c.platformid,
修剪 (to_char(c.latitude,'90.0000000')) 作为纬度,
修剪 (to_char(c.经度,'990.0000000')) 作为经度,
C.m.nth,
修剪 (to_char(c.watertemperature,'990.00')) 作为水温,
修剪 (to_char(c.Watersalcing,'990.000')) 作为水盐度,
修剪 (to_char(c.currentspeed,'9990.00')) 为currentspeed,
c.防水深度
来自 OCEAN c, QUALITY d where d.changedate >= to_date('20171107100000','yyyymmddhh24miss')
和d.changedate <= 到日期 (“20171108100000”,“yyyymmddhh24miss”)
和d.观察时间> (到日期 ('20171107100000','yyyymmddhh24miss')-30)
和c.platformid = d.platformid
和纬度 = 纬度
和c.经度 = d.经度
和d.distributionCd = 'A')
按平台、纬度、经度、水深排序;

专家解答

您需要做的就是将格式推迟到最终查询,例如

SQL> create table t ( x number );

Table created.

SQL> insert into t values (1.123);

1 row created.

SQL> insert into t values (2.456);

1 row created.

SQL> insert into t values (3.123);

1 row created.

SQL>
SQL>
SQL> create table t1 ( x number );

Table created.

SQL> insert into t1 values (10.234);

1 row created.

SQL> insert into t1 values (20.324);

1 row created.

SQL> insert into t1 values (30.321);

1 row created.

-- base data sorts fine

SQL>
SQL> select *
  2  from
  3   ( select x from t
  4     union
  5     select x from t1
  6   )
  7  order by 1;

         X
----------
     1.123
     2.456
     3.123
    10.234
    20.324
    30.321

6 rows selected.

-- strings do not

SQL>
SQL>
SQL> select *
  2  from
  3   ( select to_char(x,'fm99.99') char_version from t
  4     union
  5     select to_char(x,'fm99.99') char_version from t1
  6   )
  7  order by 1;

CHAR_V
------
1.12
10.23
2.46
20.32
3.12
30.32

6 rows selected.

-- so do string conversion last

SQL>
SQL>
SQL> select to_char(x,'fm99.99') char_version
  2  from
  3   ( select x from t
  4     union
  5     select x from t1
  6   )
  7  order by x;

CHAR_V
------
1.12
2.46
3.12
10.23
20.32
30.32

6 rows selected.

SQL>
复制


现在,如果您的联合中有可能发生重复,例如,如果您在源数据中

20.321
20.322

然后由于格式化而显示为dups,您可以使用ROUND/CEIL/TRUNC/etc函数来获取内部查询中的数据。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论