Search code examples
nestjstypeorm

How to Perform Group By Operation in TypeORM Using Repository's find Method with MS SQL Server?


I'm working with TypeORM and using the Repository's find method to retrieve data from an MS SQL Server database. I need to perform a GROUP BY operation on a specific column.

const options:FindManyOptions={}
this.dailyProductionRepository.find(options);

Can someone provide an example of how to achieve this with TypeORM and MS SQL Server? I'd appreciate any guidance or code examples. Thank you!


Solution

  • typeorm find function does not support groupBy functionality. If you need to use groupBy, you have to use the QueryBuilder. below is an example of how you might use the QueryBuilder to group results:

    createQueryBuilder("yourEntity(user)").groupBy("user.name").addGroupBy("user.id")