Skip to content

Easier way to make SELECT COUNT(DISTINCT()) queries #1186

Closed

Description

@Ionaru

I recently switched from Sequelize to TypeORM because I wanted proper typing and decorator support, but I'm struggling with certain queries that were easy to do in Sequelize.

// Sequelize
const count = await logEntry.aggregate('sender_id', 'count', {distinct: true})
console.log(count); // 4 (As a number)
// TypeORM
const count = await logEntry.getRepository().createQueryBuilder('log_entry').select('COUNT(DISTINCT(`sender_id`))', 'count').getRawOne()
console.log(count); // {count: 4} (As an object)

I'm basically writing the SELECT part of the query by hand, is there a better way to do this in TypeORM?

Activity

  1. pleerock commented on Nov 16, 2017

    @pleerock
    Member

    Not yet. We'll need to make a detailed proposal for this and all similar features and discuss its design first before implementing

  2. added this to the 0.3.0 milestone on Mar 29, 2018
  3. afurculita commented on Sep 16, 2018

    @afurculita
    Contributor

    @Ionaru this can be done now this way:

    const count = await logEntry.getRepository().createQueryBuilder('log_entry').select('DISTINCT(`sender_id`)').getCount();
    console.log(count); // 4 (As a number)
  4. laurentsmohr commented on Apr 10, 2019

    @laurentsmohr

    @afurculita
    your solution did not work for me. .getCount() always returns the number of rows in the table.

  5. removed this from the 0.3.0 milestone on Jun 28, 2021
  6. self-assigned this
    on Jun 28, 2021
  7. shadrech commented on Aug 12, 2021

    @shadrech

    Just to make sure we all on same page, the real bug here is that typeorm is not building the DISTINCT query correctly write? If I did

    logEntry.createQueryBuilder('n')
            .select('DISTINCT(n. sender_id)')
            .where({ something: 'blah' })
            .getCount()
    

    typeorm doesn't attach the DISTINCT command. Only way I was able to do this was via a raw query...

    logEntry.query(`
            SELECT COUNT(DISTINCT("n"."sender_id")) 
            FROM log_entry "n"
            WHERE "n"."something" = $1
          `, ['blah'])
    
  8. angelxmoreno commented on May 13, 2022

    @angelxmoreno

    Funny how 5 years later no solution was posted. I got here via a Google search. Anyways, for those that come here via the interwebs, the correct syntax to get a number value from a count distinct is this:

     const { count } = await DocumentEntity.createQueryBuilder()
                .select('COUNT(DISTINCT(`classification`))', 'count')
                .getRawOne();
    console.log(count); // 4 (As a number)
    
  9. angelxmoreno commented on May 13, 2022

    @angelxmoreno

    @pleerock I believe this can be closed

  10. PRossetti commented on Jun 23, 2022

    @PRossetti

    Funny how 5 years later no solution was posted. I got here via a Google search. Anyways, for those that come here via the interwebs, the correct syntax to get a number value from a count distinct is this:

     const { count } = await DocumentEntity.createQueryBuilder()
                .select('COUNT(DISTINCT(`classification`))', 'count')
                .getRawOne();
    console.log(count); // 4 (As a number)
    

    For me this is the only answer that worked. I guess the one from shadrech might also work but was trying to do this with the lesser plain sql possible.
    Thank you!

  11. peterklingelhofer commented on Jul 13, 2022

    @peterklingelhofer

    Funny how 5 years later no solution was posted. I got here via a Google search. Anyways, for those that come here via the interwebs, the correct syntax to get a number value from a count distinct is this:

     const { count } = await DocumentEntity.createQueryBuilder()
                .select('COUNT(DISTINCT(`classification`))', 'count')
                .getRawOne();
    console.log(count); // 4 (As a number)
    

    Can confirm that this is the only solution that worked for me (outside of probably, as @PRossetti said above, a raw query as @shadrech suggested). However, I'm joining a few tables in my query, so note that if you're doing that you'll have to remove the backticks around the column name, like so: .select('COUNT(DISTINCT(classification))', 'count')

  12. github-actions commented on Sep 18, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

  13. PRossetti commented on Sep 19, 2025

    @PRossetti

    I think this is still a good feature to be added. Something like DocumentEntity.getDistinctCount(:columnName)

  14. reopened this on Sep 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions