数据sql

作者: 季嘉聊跨境 | 来源:发表于2019-03-07 23:56 被阅读0次

    时间戳转换工具:
    https://tool.lu/timestamp/

    通用sql
    类型强制转换:cast(字段名 as 字段类型)
    统计函数:abs、max、min、avg
    hive impala 相关sql
    截取字符串:
    substr(cast(字段名 as string),11,9) 11为开始index,9为截取长度
    获取时间戳各部分时间:
    year(字段名),month(字段名),day(字段名),hour(字段名),minute(字段名),字段名
    对应结果:
    2019 2 20 9 3 2019-02-20 09:03:05
    获取时间戳差值方法:
    (unix_timestamp(字段名)-unix_timestamp(字段名))
    pg 相关sql post_gis 插件 可以支持空间计算
    计算目标跟其他的距离需要三步,
    第一步为数据建立空间参数。
    -- 目标数据表新增空间字段 poi为目标表名,geom为自定义的空间参数,sql语句如下:
    SELECT AddGeometryColumn ('','poi','geom',4326,'POINT',2);
    第二步给新增的空间参数赋值。
    -- a." point _X" 为目标经度,a." point _Y"为目标纬度 将经纬度赋值给空间参数,4326为wgs84对应坐标系编号
    update poi a set geom=ST_SetSRID(ST_MakePoint(CAST(a."point_X" as decimal),CAST(a."point_Y" as decimal)),4326);
    第三步找出距离N米的其他或者目标,以50米为例。Sql语句中设置的数字单位为度,需要将米转换为度,转换公式如下:
    -- 查询语句 点跟点的比较 距离50米 (0.0004499640028797697度)
    计算公式如下:
    1米=1/1852 海里
    1海里= 1/60度
    10001/18521/60=0.008999280057595392
    SELECT a.else_name ,c.poi_name from else a,poi c where ST_Distance(a.geom, c.geom) <0.0004499640028797697

    oracle 相关sql 空间计算需要建立索引
    select * from poi t;

    ---添加空间字段
    alter table poi add column geometry sdo_geoemtry;

    update poi t
    set t.sdo_geometry = mdsys.st_geometry(mdsys.sdo_geometry(2001,
    4326,
    sdo_point_type(t.lon,
    t.lat,
    null),
    null,
    null));

    ---注册空间元数据
    insert into user_sdo_geom_metadata
    values
    ('poi',
    'geometry',
    sdo_dim_array(sdo_dim_element('X', -180, 180, 0.5),
    sdo_dim_element('y', -90, 90, 0.5)),
    4326);

    ---创建空间索引
    create index poi123 on poi(geometry) indextype is mdsys.spatial_index;

    ---最邻近查询
    select a.*
    from poi a
    where sdo_nn(a.geometry,
    sdo_geometry(2001,
    4326,
    sdo_point_type(120, 30, null),
    null,
    null)) = 'TRUE';

    ---以点为中心,指定范围内查询
    select a.,b.
    from poi a,district B
    where SDO_WITHIN_DISTANCE(a.geometry,
    MDSYS.SDO_GEOMETRY(2001,
    4326,
    MDSYS.SDO_POINT_TYPE(b.lat,
    b.lon,
    0),
    NULL,
    NULL),
    'distance=100') = 'TRUE';

    ---以点为中心,指定范围内查询 (若另一张表也建立空间索引,可用这个sql语句)
    select a., b.
    from poi a,district B
    where mdsys.SDO_WITHIN_DISTANCE(a.geometry, B.GEOMETRY, 'distance=100' ) =
    'TRUE'; ORDER BY DIST ;

    select a.,b. from table1 a,table2 b

    where sdo_nn(a.geometry,b.geometry)='TRUE'
    and b.id=1;

    select mdsys.sdo_geom.sdo_distance(
    mdsys.sdo_geometry(2001,4326,mdsys.sdo_point_type(90,0,NULL),NULL,NULL),
    mdsys.sdo_geometry(2001,4326,mdsys.sdo_point_type(100,0,NULL),NULL,NULL),
    0.05)
    As Distance_90_0_to_100_0
    from poi a ;

    相关文章

      网友评论

          本文标题:数据sql

          本文链接:https://www.haomeiwen.com/subject/zkzhpqtx.html