Split_part() cutting function of Postgresql

I. Introduction

The database table field has a format: 1|2|3. It is divided by vertical lines. Now I want to take the second one. pg provides such a function. The record is as follows. The following is my SQL.

Two, the code

SELECT COUNT
                   (*)                              AS "count",
               split_part(tree_t.node_path, '|', 2) AS "id",
               name_t.node_name                     AS "type"
        FROM datsvc_data_standard_t sd_t
                 INNER JOIN datsvc_tree_t tree_t ON tree_t."id" = sd_t.tree_category
                 INNER JOIN datsvc_tree_t name_t ON name_t."id" :: VARCHAR = split_part(tree_t.node_path, '|', 2)
        WHERE tree_t."type" = 2
        GROUP BY split_part(tree_t.node_path, '|', 2),
                 name_t.node_name

Three, analysis

split_part(string text, delimiter text2, field int)

The text field to be cut; in what form does text2 cut the int interception position

ps:
text="abc" split_part(text,'.',1) Result: a
text="abc" split_part(text,'.',2) Result: b
text="abc" split_part(text,'.' ,3) Result: c

Guess you like

Origin blog.csdn.net/BeiisBei/article/details/108449986