# Why count(distinct patient\_id) is larger than count(patient\_id)?

**URL:** <https://discuss.elastic.co/t/why-count-distinct-patient-id-is-larger-than-count-patient-id/337250>\
**Category:** Elasticsearch\
**Created:** [June 30, 2023, 1:24am UTC](https://discuss.elastic.co/t/why-count-distinct-patient-id-is-larger-than-count-patient-id/337250 "2023-06-30T01:24:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DongPoJuShi\_Dj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dongpojushi_dj/32/122863_2.png) [@DongPoJuShi\_Dj](https://discuss.elastic.co/u/DongPoJuShi_Dj)\
**Post date:** [June 30, 2023, 1:24am UTC](https://discuss.elastic.co/t/why-count-distinct-patient-id-is-larger-than-count-patient-id/337250/1 "2023-06-30T01:24:29Z")

</div>

There is an index alias that includes two indexes, and the index structure is as follows:

```auto
{
  "scientific_data_group1": {
    "aliases": {
      "scientific_data_group": {}
    },
    "mappings": {
      "properties": {
        "category_id": {
          "type": "keyword"
        },
        "category_name": {
          "type": "keyword"
        },
        "id": {
          "type": "keyword"
        },
        "patient_id": {
          "type": "keyword"
        }
      }
    }
  },
  "scientific_data_group_nested_20230525144327776": {
    "aliases": {
      "scientific_data_group": {}
    },
    "mappings": {
      "properties": {
        "category_id": {
          "type": "keyword"
        },
        "category_name": {
          "type": "keyword"
        },
        "id": {
          "type": "keyword"
        }
        "patient_id": {
          "type": "keyword"
        }
      }
    }
  }
}

```

Now, when using SQL syntax to query, the result of count(distinct patient\_id) is larger than count(patient\_id). What could be the possible reason for this?

```auto
POST _sql?format=json
{
  "query": """
SELECT count(patient_id),count(distinct patient_id),category_id FROM scientific_data_group
group by category_id
"""
}

```

 ![无标题](https://us1.discourse-cdn.com/elastic/original/3X/6/9/691a3f451494f2a5a1c9cbc5b8be7374e1dad376.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 28, 2023, 1:24am UTC](https://discuss.elastic.co/t/why-count-distinct-patient-id-is-larger-than-count-patient-id/337250/2 "2023-07-28T01:24:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
