You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Easier way to make SELECT COUNT(DISTINCT()) queries #1186
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.
// Sequelizeconstcount=awaitlogEntry.aggregate('sender_id','count',{distinct: true})console.log(count);// 4 (As a number)
// TypeORMconstcount=awaitlogEntry.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?
Reacted by Max Yankov, Johann Buscail, zhangxk, Arslan Shahab, Dmitry Neverovski, Yago Tomé, Alex Nikon, Arno Hilke and Kiran Gopalakrishnan
constcount=awaitlogEntry.getRepository().createQueryBuilder('log_entry').select('DISTINCT(`sender_id`)').getCount();console.log(count);// 4 (As a number)
Reacted by Marek Grzybek, Jeroen Akkerman, Mathias Vonende, Valéria Padilha de Vargas and Arslan Shahab
Reacted by Leonardo E. Dominguez, henrik, tjhiggins, Monye David Onoh, PlayBuilder, Nuo Li, Marcelo Estevam, Karey Higuera, Bhavneet Singh, Anna Babicheva and 9 more
@afurculita
your solution did not work for me. .getCount() always returns the number of rows in the table.
Reacted by Gao Sun, Dzmitry Tolpekin, Hunter Weinsheink, Leonardo E. Dominguez, Željko Šević, Ali Zahid, Tri Van Pham, Ace Pace, tjhiggins, Dzianis Ihnatovich and 20 more
Reacted by Tristan, John Ciprian, Cristian Milea, Marcelo Estevam and Joe Souter
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:
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:
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!
Reacted by peterklingelhofer, Antoine Maitre and Angel S. Moreno
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:
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')
Reacted by Pablo Rossetti, lucasgonzalezm, Sid Grover and Angel S. Moreno
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.
Activity
pleerock commentedon Nov 16, 2017
Not yet. We'll need to make a detailed proposal for this and all similar features and discuss its design first before implementing
afurculita commentedon Sep 16, 2018
@Ionaru this can be done now this way:
laurentsmohr commentedon Apr 10, 2019
@afurculita
your solution did not work for me.
.getCount()always returns the number of rows in the table.shadrech commentedon Aug 12, 2021
Just to make sure we all on same page, the real bug here is that typeorm is not building the
DISTINCTquery correctly write? If I didtypeorm doesn't attach the
DISTINCTcommand. Only way I was able to do this was via a raw query...angelxmoreno commentedon May 13, 2022
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:
angelxmoreno commentedon May 13, 2022
@pleerock I believe this can be closed
PRossetti commentedon Jun 23, 2022
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!
peterklingelhofer commentedon Jul 13, 2022
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')github-actions commentedon Sep 18, 2025
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.
PRossetti commentedon Sep 19, 2025
I think this is still a good feature to be added. Something like
DocumentEntity.getDistinctCount(:columnName)