6

Using QGIS 2.16.2 I would like to export the unique values in a shapefile string attribute field to a new database table (which would contain a single field listing the unique values). The Vector > Analysis Tools > List Unique Values tool correctly selects the unique values, but the only output option is an .html file. Is there some way to output to a .dbf, or .csv format?

The shapefile is a line format, so the Dissolve tool does not work (it requires a polygon input), as described in Getting list of distinct values from shapefile field using QGIS?

Stu Smith
  • 8,252
  • 8
  • 35
  • 84
  • Did my answer solve your question? If so, please mark it as accepted to let others know this post is officially solved (see http://gis.stackexchange.com/tour). – Germán Carrillo Nov 06 '16 at 00:04

2 Answers2

6

Go to DB Manager -> Virtual Layers -> QGIS layers, click on SQL Window button (or press F2), and write the next SQL expression:

SELECT DISTINCT your_field FROM your_layer ORDER BY your_field;

Once you get the result table, click on Load as new layer, uncheck Geometry column, and click on Load now!.

enter image description here

Now you have a table layer in QGIS, which you can export with right click -> Save As... to a CSV file, for example.

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • @ German Carrillo Thank you. Following your advice, I use this SQL expression: SELECT DISTINCT junk FROM 2015_08_01 ORDER BY junk; This error results: Query execution error on CREATE TEMP VIEW _tview AS SELECT DISTINCT junk FROM 2015_08_01 ORDER BY junk;: 1 - unrecognized token: "2015_08_01" FYI, 2015_08_01 is the shapefile name and junk is the string attribute field. What am I doing wrong? – Stu Smith Sep 29 '16 at 18:16
  • Your table name seems to be special (only numbers and underscores), so you need to use quotation marks. Try with "2015_08_01" in the SQL sentence. – Germán Carrillo Sep 29 '16 at 18:44
  • Note that there is also a button called "Add VIrtual Layer" in QGIS. You could use it to avoid opening the DB Manager. – Germán Carrillo Sep 29 '16 at 18:46
1

2023 you can do this by 3 different ways

Step by step walk through each method click here

https://transparentgov.net/cleargov1/1180/how-to-find-unique-value-part-5-qgis

I prefer SQL method,

enter image description here

enter image description here

enter image description here

hoogw
  • 1,712
  • 1
  • 18
  • 23