Salar
Salar

Reputation: 5509

Knex.js count distinct column with alias

I want to generate this SQL code:

Select count(distinct user_id) as ui
from posts

I tried this snippet but it did not work:

knex('posts').countDistinct('user_id').as('ui');

How can I do that?

Upvotes: 5

Views: 6310

Answers (1)

Salar
Salar

Reputation: 5509

I found that both count() and countDistinct can parse raw queries like this:

knex('posts').countDistinct('user_id as ui') ;

knex('posts').count('user_id as ui') ;

Upvotes: 11

Related Questions