Count number of times matching string appears in another column (similar to Excel COUNTIF)

First solution using virtual column and field calculator

Open the attribute table of your layer and click on field calculator icon. Enter the following expression:

count("your_column", "your_column")

enter image description here

If you close and reopen the table the values will be refreshed.

Second solution to generate a temporary table with unique values and count.

In table manager open virtual layers (if your data are not in PostGIS/SptiaLite or GeoPackage) and insert an SQL query on the query tab:

select your_column, count(*) from your_layer
group by your_column
order by your_column

enter image description here

You will get a new layer which can be joined to the original layer by your_column


You can use this expression in field calculator:

  array_count( array_agg( "fieldnamecontainingstrings",group_by:="fieldnamecontainingstrings"),"fieldnamecontainingstrings")

Tags:

Sql

String

Qgis