• oracle分组合并数值带顺序


    比如:有如下一张设备电子围栏位置坐标的表(tb_equ_point)。

    equ_name:设备电子围栏名称

    point_id:点位坐标id

    point_x:点位x坐标

    point_y:点位y坐标。

    附数据:

    1. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('轻烃二列回收装置', '1707192429914726402', '84.2097884178748', '41.45319253343837');
    2. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('轻烃二列回收装置', '1707192429918920706', '84.20991864872965', '41.45267977449036');
    3. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('轻烃二列回收装置', '1707192429927309314', '84.21122698760112', '41.45286915256579');
    4. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('轻烃二列回收装置', '1707192429931503618', '84.21116556273135', '41.453349887040574');
    5. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('乙烷天然气增压站', '1707192429960863746', '84.20783836821487', '41.45526140938881');
    6. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('乙烷天然气增压站', '1707192429969252354', '84.20791184166993', '41.45484777039682');
    7. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('乙烷天然气增压站', '1707192429973446657', '84.20917631597212', '41.45499000780251');
    8. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('乙烷天然气增压站', '1707192429977640961', '84.20908976877685', '41.45538944455889');
    9. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('液化气罐区', '1707192430002806786', '84.20723072875117', '41.45346426655951');
    10. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('液化气罐区', '1707192430007001090', '84.20740838206585', '41.45246187545852');
    11. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('液化气罐区', '1707192430015389698', '84.20841913655941', '41.45256453477293');
    12. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('液化气罐区', '1707192430019584001', '84.20822649186182', '41.45357496335363');
    13. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('轻烃罐区', '1707192430044749826', '84.20872921576651', '41.45318604610804');
    14. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('轻烃罐区', '1707192430048944130', '84.20884409341907', '41.45257962408824');
    15. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('轻烃罐区', '1707192430220910594', '84.2092407921853', '41.45263007114594');
    16. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('轻烃罐区', '1707192430225104898', '84.20911756856574', '41.453243652526155');
    17. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('丙烷罐区', '1707192430250270722', '84.20896730497799', '41.451822676291805');
    18. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('丙烷罐区', '1707192430258659329', '84.2090131474932', '41.451590037400294');
    19. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('丙烷罐区', '1707192430262853633', '84.2095576735967', '41.45164557396578');
    20. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('丙烷罐区', '1707192430267047938', '84.20950934658842', '41.451900631780624');
    21. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('装车站危废库房区域', '1707192430292213762', '84.20630670236255', '41.45587361982766');
    22. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('装车站危废库房区域', '1707192430296408066', '84.20666273263066', '41.45394183801273');
    23. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('装车站危废库房区域', '1707192430304796674', '84.20785692954897', '41.45406762412216');
    24. INSERT INTO "tb_equ_point" ("EQU_NAME", "POINT_ID", "POINT_X", "POINT_Y") VALUES ('装车站危废库房区域', '1707192430308990978', '84.20749154021959', '41.45595024021673');

     

    现在想把数据按名称合并,一组x坐标和y坐标放在一起,用 “,” 隔开,x,y的顺序要对应

    效果如下图:

     sql如下:

    1. SELECT
    2. equ_name,
    3. LISTAGG ( POINT_X, ',' ) WITHIN GROUP ( ORDER BY point_id ) AS POINT_X,
    4. LISTAGG ( POINT_Y, ',' ) WITHIN GROUP ( ORDER BY point_id ) AS POINT_Y
    5. FROM
    6. tb_equ_point
    7. GROUP BY
    8. equ_name

     解析:

    Oracle的LISTAGG函数将多个结果合并到一起,示例如下:

    SELECT name, LISTAGG(x, ',') WITHIN GROUP (ORDER BY x) AS x_list, LISTAGG(y, ',') WITHIN GROUP (ORDER BY y) AS y_list
    FROM table_name
    GROUP BY name

    其中,table_name是你的表名,x_list和y_list分别是按name分组后合并的x和y结果。如果需要在结果中添加分隔符,可以在LISTAGG函数的第一个参数中指定。例如,如果想在结果中使用“|”作为分隔符,可以将函数改为:

    LISTAGG(x, '|') WITHIN GROUP (ORDER BY x) AS x_list

    同样的,也可以使用其他聚合函数,如MAX、MIN、AVG等。

    注:合并的字段,x,y要顺序对应的话,要有一个统一的标识point_id。 

  • 相关阅读:
    Linux - 配置固定的ip地址
    Lnmp架构
    opencv-python图像处理:阈值,滤波,腐蚀,膨胀,梯度
    Vue内置组件TransitionGroup详细介绍
    从 12K 到 60K, 这 2023Java 研发必问高级面试题,过关斩将拿 offer
    正向代理(流量代理)
    LeetCode //2619. Array Prototype Last (Day 23 of LC JavaScript Challenge)
    一文深入理解Linux驱动整理
    前端面试html自检(先看问题 自己自述一遍 不会再看答案 )
    笔试强训48天——day14
  • 原文地址:https://blog.csdn.net/supersolon/article/details/133378008