In PostgreSQL, we can use SELECT DISTINCT
the and SUBSTRING
functions to perform deduplication queries on a certain field. This article will introduce how to use these two functions to implement deduplication resource_version
queries on fields.
1. SELECT DISTINCT
Statement
SELECT DISTINCT
Statement 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 DISTINCT
statements to select the deduplicated resource_version
field values.
SELECT DISTINCT resource_version
FROM tb_resource;
2. SUBSTRING
Function
SUBSTRING
Function is used to extract substrings from strings. It takes three parameters: original string, starting position, and substring length. In this example, we will use SUBSTRING
a function to intercept the first N characters of each field value, where N is the number of characters after resource_version
the 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 DISTINCT
and SUBSTRING
functions
Sometimes we need to use both the SELECT DISTINCT
and SUBSTRING
function to perform more complex text operations. In this example, we will use these two functions to perform a resource_version
deduplication 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)-1
the 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_version
SELECT DISTINCT