PostgreSQL COUNT(*) and COUNT(column) Give Different Totals for NULLs
Compute all three counts from the same filtered input. COUNT(*) counts rows. COUNT(device_id) counts rows with a non-NULL ID. COUNT(DISTINCT device_id) counts the distinct non-NULL IDs.
Reconcile a four-row fixture
Take a synthetic input of A, A, B, NULL for device IDs. Those values yield 4 events, 3 events with IDs and 2 identified devices. A missing identifier does not erase the underlying event. Two rows for A represent two observations of the same identified device.
Measure missing values explicitly
SELECT COUNT(*) AS events,
COUNT(device_id) AS with_id,
COUNT(DISTINCT device_id) AS devices,
COUNT(*) - COUNT(device_id) AS missing_id
FROM daily_events;Also inspect the source contract for blank strings. PostgreSQL treats an empty text string as a value, so it can enter both populated and distinct counts. Normalizing blank strings requires a deliberate rule and a test for whitespace-only input.
Use the denominator named in the report
If the metric is identifier coverage, divide identified events by all events. If the metric is events per identified device, define which events qualify for the numerator. Run the counts before joining detail tables when possible, then compare after the join to catch row multiplication. On an empty input the count columns are zero.
댓글
댓글 쓰기
안녕하세요 :)