PostgreSQL中尋找最大連續性欄位

來源:互聯網
上載者:User

標籤:

一、建表

lihao=#create table tb (id int,pid int,name varchar);

lihao=#INSERT INTO tb VALUES (1, 0, ‘廣東省‘);

lihao=#INSERT INTO tb VALUES (2, 0, ‘浙江省‘);

lihao=#INSERT INTO tb VALUES (3, 2, ‘衢州市‘);

lihao=#INSERT INTO tb VALUES (4, 2, ‘杭州市‘);

lihao=#INSERT INTO tb VALUES (5, 2, ‘湖州市‘);

lihao=#INSERT INTO tb VALUES (6, 2, ‘嘉興市‘);

lihao=#INSERT INTO tb VALUES (7, 2, ‘寧波市‘);

lihao=#INSERT INTO tb VALUES (8, 2, ‘紹興市‘);

lihao=#INSERT INTO tb VALUES (9, 2, ‘台州市‘);

lihao=#INSERT INTO tb VALUES (10, 2, ‘溫州市‘);

lihao=#INSERT INTO tb VALUES (11, 2, ‘麗水市‘);

lihao=#INSERT INTO tb VALUES (12, 2, ‘金華市‘);

lihao=#INSERT INTO tb VALUES (13, 2, ‘舟山市‘);

lihao=#INSERT INTO tb VALUES (14, 4, ‘上城區‘);

lihao=#INSERT INTO tb VALUES (15, 4, ‘下城區‘);

lihao=#INSERT INTO tb VALUES (16, 4, ‘拱墅區‘);

lihao=#INSERT INTO tb VALUES (17, 4, ‘餘杭區‘);

lihao=#INSERT INTO tb VALUES (18, 11, ‘金東區‘);

lihao=#INSERT INTO tb VALUES (19, 1, ‘廣州市‘);

lihao=#INSERT INTO tb VALUES (20, 1, ‘深圳市‘);

lihao=#INSERT INTO tb VALUES (111, 0, ‘111‘);

lihao=#INSERT INTO tb VALUES (222, 1, ‘222‘);

lihao=#INSERT INTO tb VALUES (333, 2, ‘333‘);

lihao=#INSERT INTO tb VALUES (444, 4, ‘444‘);

lihao=#INSERT INTO tb VALUES (555, 11, ‘555‘);

二、SQL語句

lihao=# select distinct(a.pid),array((select b.id from tb b where a.pid = b.pid and exists (select * from tb c where b.id = c.id + 1) order by b.id)) from tb a order by a.pid;
 pid |            array            
-----+-----------------------------
   0 | {2}
   1 | {19,20}
   2 | {3,4,5,6,7,8,9,10,11,12,13}
   4 | {14,15,16,17}
  11 | {18}

lihao=# select distinct(a.pid),array((select b.id from tb b where a.pid = b.pid and not exists (select * from tb c where b.id = c.id + 1) order by b.id)) from tb a order by a.pid;
 pid |  array  
-----+---------
   0 | {1,111}
   1 | {222}
   2 | {333}
   4 | {444}
  11 | {555}

三、shell指令碼

#!/bin/bash
counter=`psql -c "select count(*) from (select distinct(pid) from tb) a" | sed -n ‘3p‘`
num=$[$counter + 2]
pid=(`psql -c "select distinct(pid) from tb order by pid" | sed -n ‘3,‘‘‘$num‘‘‘p‘`)
echo "==========="
echo "PID:{ID}"
echo "==========="


for pid in ${pid[*]}
do
  m=`psql -c "select count(*) from tb where pid= $pid" | sed -n ‘3p‘`
  n=$[$m + 2]
  id=(`psql -c "select id from tb where pid=$pid order by id" | sed -n ‘3,‘‘‘$n‘‘‘p‘`)

  for ((a=0;a<$m;a++))
  do
     id1=${id[$a]}
     id2=${id[$[$a+1]]}

     diff=$[$id2 - $id1]     
     if [ $diff != 1 ];then
        id_new=${id[@] /$id1/}
     fi
  done

  echo $pid" : {"${id_new[*]}"}"
done

PostgreSQL中尋找最大連續性欄位

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.