Distinctness Depends on Selected Columns

SELECT DISTINCT removes duplicate rows of the chosen result values. Changing which columns are selected can change which rows count as duplicates.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace three records
  2. Match the report's purpose
  3. Predict what disappears when one column is removed
  4. Decide whether duplicate removal hides a join defect

Trace three records

Titles Tea, Tea, and Rice give two distinct title results. Selecting title and a distinct ID yields three pairs. Record equality explains the related question of which fields define sameness.

Match the report's purpose

If the goal counts authors, select identities appropriate to authors before deduplicating. Use row granularity to decide whether similar complete rows are independent observations.[1]

Predict what disappears when one column is removed

Rows (north, 7) and (north, 9) differ by identifier. Selecting distinct region returns north once. Selecting distinct region and ID preserves both rows. The selected tuple defines duplicate equality in the result. See sets and duplicates.

Input: (north,7), (north,9), (west,12)
Distinct region: north, west
Distinct region and id: all three pairs

Decide whether duplicate removal hides a join defect

A distinct query can be intentional for unique labels. If a join is unexpectedly many-to-many, suppressing duplicate display values may disguise multiplicity that still affects totals. Test the intended grain with associated IDs.

References

  1. PostgreSQL Global Development Group, Queries ↩