当前位置:Gxlcms > 数据库问题 > PostgreSQL 给数组排序

PostgreSQL 给数组排序

时间:2021-07-01 10:21:17 帮助过:9人阅读

PostgreSQL 支持数组,可是没有对数据内部元素进行排序的一个函数。 ?今天我分别用PLPGSQL和PLPYTHONU写了一个。
演示样例表结构:
  1. <code class="language-sql">t_girl=# \d test_array;
  2. Table "ytt.test_array"
  3. Column | Type | Modifiers
  4. --------+-----------+---------------------------------------------------------
  5. id | integer | not null default nextval(‘test_array_id_seq‘::regclass)
  6. str1 | integer[] |
  7. Indexes:
  8. "test_array_pkey" PRIMARY KEY, btree (id)
  9. </code>



演示样例数据:
  1. <code class="language-sql">t_girl=# select * from test_array;
  2. id | str1
  3. ----+---------------------------
  4. 1 | {100,200,300,5,10,20,100}
  5. 2 | {200,100,2,30,0,5}
  6. 3 | {2000,101,2,30,0,10}
  7. (3 rows)
  8. Time: 1.513 ms</code>




plsql存储函数array_sort运行结果:
升序
  1. <code class="language-sql">t_girl=# select id,array_sort(str1,‘asc‘) from test_array;
  2. id | array_sort
  3. ----+---------------------------
  4. 1 | {5,10,20,100,100,200,300}
  5. 2 | {0,2,5,30,100,200}
  6. 3 | {0,2,10,30,101,2000}
  7. (3 rows)
  8. Time: 2.377 ms</code>


降序
  1. <code class="language-sql">t_girl=# select id,array_sort(str1,‘desc‘) from test_array;
  2. id | array_sort
  3. ----+---------------------------
  4. 1 | {300,200,100,100,20,10,5}
  5. 2 | {200,100,30,5,2,0}
  6. 3 | {2000,101,30,10,2,0}
  7. (3 rows)
  8. Time: 3.318 ms
  9. t_girl=# </code>




python 存储函数array_sort_python 运行结果:
降序:
  1. <code class="language-sql">t_girl=# select id,array_sort_python(str1,‘desc‘) from test_array;
  2. id | array_sort_python
  3. ----+---------------------------
  4. 1 | {300,200,100,100,20,10,5}
  5. 2 | {200,100,30,5,2,0}
  6. 3 | {2000,101,30,10,2,0}
  7. (3 rows)
  8. </code>



  1. <code class="language-sql">Time: 2.797 ms</code>


升序:
  1. <code class="language-sql">t_girl=# select id,array_sort_python(str1,‘asc‘) from test_array;
  2. id | array_sort_python
  3. ----+---------------------------
  4. 1 | {5,10,20,100,100,200,300}
  5. 2 | {0,2,5,30,100,200}
  6. 3 | {0,2,10,30,101,2000}
  7. (3 rows)
  8. Time: 1.856 ms
  9. t_girl=# </code>




附: array_sort_python 代码:
  1. <code class="language-python">CREATE or replace FUNCTION array_sort_python(c1 text [],f_order text) RETURNS text [] AS $$
  2. result = []
  3. if f_order.lower() == ‘asc‘:
  4. c1.sort()
  5. result = c1
  6. elif f_order.lower() == ‘desc‘:
  7. c1.sort(reverse=True)
  8. result = c1
  9. else:
  10. pass
  11. return result
  12. $$ LANGUAGE plpythonu;
  13. </code>





array_sort 代码:


  1. <code class="language-sql">create or replace function array_sort(anyarray,f_order text) returns anyarray
  2. as
  3. $ytt$
  4. declare array1 alias for $1;
  5. tmp int;
  6. result text [];
  7. begin
  8. if lower(f_order) = ‘desc‘ then
  9. for tmp in select unnest(array1) as a order by a desc
  10. loop
  11. result := array_append(result,tmp::text);
  12. end loop;
  13. return result;
  14. elsif lower(f_order) = ‘asc‘ then
  15. for tmp in select unnest(array1) as a order by a asc
  16. loop
  17. result := array_append(result,tmp::text);
  18. end loop;
  19. return result;
  20. else
  21. return array[‘f_order must be asc or desc!‘];
  22. end if;
  23. end;
  24. $ytt$ language plpgsql;</code>




PostgreSQL 给数组排序

标签:track   not   temp   ase   数据   pos   result   desc   2.7   

人气教程排行