postgresql相關sql集錦

一、相似於oracle的listagg->string_agghtml

SELECT
  area_county,','||string_agg(to_char(is_end,'9'),',,')||',' is_ends,count(1)total
FROM
  project_info
GROUP BY area_county

注意:sql

string_agg的第一個參數必定是字符類型,第二個參數是分隔符。
to_char函數是將數字類型的數據轉換爲字符類型,第一個參數是要轉換的數據,第二個參數是轉換的格式。
(對於to_char相關說明能夠參考:https://www.postgresql.org/docs/10/functions-formatting.html

結果:


二、統計某個字符在指定字符串中出現的次數。
select array_length(regexp_split_to_array(',-1,,-1,,-1,,-1,,-1,,-1,',',-1,'),1)-1 total

注意:oracle

regexp_split_to_array函數的第一個參數是源字符串,第二個參數是要統計的字符。

結果:函數

相關文章
相關標籤/搜索