0

I have a table that looks like so:

| user_id | client_application_id | invalidated_at
|       1 | 55555 | |
|       1 | 123   | |

I want to be able to do one thing:

  1. Be able to count how many user_id's have more than 250 client_application_id's on their account and order it so that it starts at 250 and goes up.

My current (incorrect)query looks like so:

select  user_id, count(user_id) as cnt from tokens where invalidated_at is null group by user_id having count(user_id) > 250 order by cnt ;

The output looks like so:

  user_id   | cnt 
------------+-----
    1 | 251
    5 | 251

Using this example, I would like the query to count the two users that have 251, so it would look like so:

Count_Of_Users  | Application_Count
2 | 251
3
  • what's the problem .. you have the user_id by the firts query .. explain better your expected resul for the secondo query.. Commented Oct 22, 2016 at 19:37
  • Edit your question and provide the desired output. Commented Oct 22, 2016 at 19:43
  • Updated it now, hopefully it's more clear Commented Oct 22, 2016 at 19:50

2 Answers 2

1

You can use something similar to your query as a subquery and get the output you need:

    select count(user_id) as count_of_users, cnt as application_count from
    (
       select  user_id, count(client_application_id) as cnt 
       from tokens 
       where invalidated_at is null 
       group by user_id having count(client_application_id) > 250 
    ) t
    group by cnt
    order by cnt 
Sign up to request clarification or add additional context in comments.

Comments

0

Is not clear which is your expected result for the secondo query could be somthings like this

  select users.*, t.user_id, t.cnt 
  from users 
  inner join ( 

      select  user_id, count(user_id) as cnt 
      from tokens 
      where invalidated_at is null 
      group by user_id 
      having count(user_id) > 250 
  ) t on users.id = t.user_id 
  order by t.user_id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.