[Database] Using `SELECT DISTINCT` and `SUBSTRING` functions in PostgreSQL to implement deduplication queries

In PostgreSQL, we can use SELECT DISTINCTthe and SUBSTRINGfunctions to perform deduplication queries on a certain field. This article will introduce how to use these two functions to implement deduplication resource_versionqueries on fields.

1. SELECT DISTINCTStatement

SELECT DISTINCTStatement is used to select unique records from the table. If no column names are specified, all columns will be selected. In this example, we will use SELECT DISTINCTstatements to select the deduplicated resource_versionfield values.

SELECT DISTINCT resource_version
FROM tb_resource;

2. SUBSTRINGFunction

SUBSTRINGFunction is used to extract substrings from strings. It takes three parameters: original string, starting position, and substring length. In this example, we will use SUBSTRINGa function to intercept the first N characters of each field value, where N is the number of characters after resource_versionthe 4th ..

SELECT DISTINCT substring(resource_version, 0, position(split_part(resource_version,'.',4) in resource_version)-1)
FROM tb_resource;

3. Combined use of SELECT DISTINCTand SUBSTRINGfunctions

Sometimes we need to use both the SELECT DISTINCTand SUBSTRINGfunction to perform more complex text operations. In this example, we will use these two functions to perform a resource_versiondeduplication query on the first 3 characters of each field value.

SELECT DISTINCT substring(resource_version, 0, position(split_part(resource_version,'.',4) in resource_version)-1)
FROM tb_resource;

In the above code, we first get the number of characters before position(split_part(resource_version,'.',4) in resource_version)-1the 4th , and then use this value as the substring length to intercept the first N characters of each field value. Finally, we use the statement to select the prefix string after deduplication..resource_versionSELECT DISTINCT

Guess you like

Origin blog.csdn.net/luansj/article/details/132063910