• 判断船进去倾倒区次数


    本来想用点连接线来判断跟面相交多少次的,搞了很久搞不定,还是老老实实用点做轨迹判断

    WITH a AS (
        -- 保持不变
        SELECT 0 AS intersect, * 
        FROM shiphistory AS lines
        WHERE mssi = '412044750' AND create_time >= '2023-09-09 00:00' AND create_time <= '2023-09-10 00:00:00'
    ),
    b AS (
        -- 保持不变
        SELECT 1 AS intersect, lines.create_time, ST_AsText(ST_Intersection(lines.location, polygon.geom)) AS segment_geom
        FROM shiphistory AS lines
        JOIN "public".dumpingarea AS polygon
        ON ST_Intersects(lines.location, polygon.geom) 
        WHERE mssi = '412044750' AND create_time >= '2023-09-09 00:00' AND create_time <= '2023-09-10 00:00:00'
        AND polygon.gid = 101
    ),
    c AS (
        SELECT (a.intersect + b.intersect) AS inside, a.* 
        FROM a
        LEFT JOIN b ON a.create_time = b.create_time
    )
    SELECT
        COUNT(*) AS change_count
    FROM (
        SELECT inside, create_time, 
               LAG(inside) OVER (ORDER BY create_time) AS previous_inside
        FROM c
    ) subquery
    WHERE inside IS DISTINCT FROM previous_inside;

    改为函数

    CREATE OR REPLACE FUNCTION "public"."calculate_change_count"("mssi_input" text, "create_time_start_input" varchar, "create_time_end_input" varchar, "workid_input" int4)
      RETURNS "pg_catalog"."int4" AS $BODY$
    DECLARE
        change_count_result integer;
    BEGIN
        WITH a AS (
            SELECT 0 AS xianjiao, * 
            FROM shiphistory AS lines
            WHERE lines.mssi = mssi_input
              AND lines.create_time >= TO_TIMESTAMP(create_time_start_input, 'YYYY-MM-DD HH24:MI:SS')
              AND lines.create_time <= TO_TIMESTAMP(create_time_end_input, 'YYYY-MM-DD HH24:MI:SS')
        ),
        b AS (
            SELECT 1 AS xianjiao, lines.create_time, ST_AsText(ST_Intersection(lines.location, polygon.geom)) AS segment_geom
            FROM shiphistory AS lines
            JOIN "public".dumpingarea AS polygon
            ON ST_Intersects(lines.location, polygon.geom) 
            WHERE lines.mssi = mssi_input
              AND lines.create_time >= TO_TIMESTAMP(create_time_start_input, 'YYYY-MM-DD HH24:MI:SS')
              AND lines.create_time <= TO_TIMESTAMP(create_time_end_input, 'YYYY-MM-DD HH24:MI:SS')
              AND polygon.workid = workid_input
        ),
        c AS (
            SELECT (a.xianjiao + b.xianjiao) AS inside, a.* 
            FROM a
            LEFT JOIN b ON a.create_time = b.create_time
        )
        SELECT ROUND(COUNT(*)/2.0) INTO change_count_result
        FROM (
            SELECT inside, create_time, 
                LAG(inside) OVER (ORDER BY create_time) AS previous_inside
            FROM c
        ) subquery
        WHERE inside IS DISTINCT FROM previous_inside;

        RETURN change_count_result;
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 1000

    调用select * from calculate_change_count(
        '412044750',
        '2023-09-09 00:00',
        '2023-09-10 00:00',
        756) 

    计算证书下所有倾倒量:

    with shipLicense as(
    SELECT UNNEST(carrier) AS carrier,partition_id,TO_CHAR(start_time, 'YYYY-MM-DD HH24:MI:SS') start_time,
    TO_CHAR(end_time,'YYYY-MM-DD HH24:MI:SS') end_time,approve_amount
    FROM license
    WHERE code = 'TEST')
    select sum(calculate_change_count(ship.mssi,shipLicense.start_time,shipLicense.end_time,shipLicense.partition_id)*amount) 
    from ship inner join shipLicense on ship.id = shipLicense.carrier

    函数获取证书下所有倾倒量

    CREATE OR REPLACE FUNCTION "public"."calculate_license_data"("licensecode" text)
      RETURNS TABLE("license_amount" text) AS $BODY$
    BEGIN
        RETURN QUERY
        WITH shipLicense AS (
            SELECT UNNEST(carrier) AS carrier, partition_id,
                TO_CHAR(start_time, 'YYYY-MM-DD HH24:MI:SS') AS start_time,
                TO_CHAR(end_time, 'YYYY-MM-DD HH24:MI:SS') AS end_time,
                approve_amount AS license_approve_amount
            FROM license
            WHERE code = licensecode
        )
        SELECT 
            sum(calculate_change_count(ship.mssi, shipLicense.start_time, shipLicense.end_time, shipLicense.partition_id)*amount)::text 
        FROM ship
        INNER JOIN shipLicense ON ship.id = shipLicense.carrier;
        RETURN;
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100
      ROWS 1000

    函数获取船倾倒统计

    CREATE OR REPLACE FUNCTION "public"."calculate_ship_data"("licensecode" text)
      RETURNS TABLE("mssi" text, "change_count" int4, "amount" text, "license_approve_amount" text) AS $BODY$
    BEGIN
        RETURN QUERY
        WITH shipLicense AS (
            SELECT UNNEST(carrier) AS carrier, partition_id,
                TO_CHAR(start_time, 'YYYY-MM-DD HH24:MI:SS') AS start_time,
                TO_CHAR(end_time, 'YYYY-MM-DD HH24:MI:SS') AS end_time,
                approve_amount AS license_approve_amount
            FROM license
            WHERE code = licensecode
        )
        SELECT ship.mssi::text, 
            calculate_change_count(ship.mssi, shipLicense.start_time, shipLicense.end_time, shipLicense.partition_id)::integer, -- 强制转换为integer
            ship.amount::text,
            shipLicense.license_approve_amount::text
        FROM ship
        INNER JOIN shipLicense ON ship.id = shipLicense.carrier;

        RETURN;
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100
      ROWS 1000

  • 相关阅读:
    优化代码 —— 减少 if - else
    Web前端:Angular有哪些特征?什么时候使用Angular?
    2023年考PMP证书有什么意义?一定要清楚!
    阿里巴巴中国站1688按关键词搜索商品api电商数据接口
    二维树状数组
    【前端面试考点】
    基于IIC协议的4脚OLED模块的单片机驱动控制(含驱动程序)
    mysql主备集群(高可用)
    不再写Python for 循环
    车联网时代,能链车联凭什么成为“关键先生”?
  • 原文地址:https://blog.csdn.net/jsboy123/article/details/133774108