0

I want to select distinct count(id) as activeusers from a table where useractive=1, but at the same time I want to select all the users example distinct Count(id) as totalusers regardless of activestate. How to achieve this in a SQL query?

2
  • Hello, you want to query twice the same table, so you need to use a joined query. Select count(table1.id) as totalusers, count(table2.id) from mytable table1, mytable table2 where table2.useractive = 1 There are other prettier solutions Commented Jun 18, 2024 at 7:26
  • 2
    Please tag the RDBMS you use Commented Jun 18, 2024 at 7:30

1 Answer 1

2

You could call count over a case expression:

SELECT COUNT(DISTINCT CASE useractive WHEN 1 THEN id END) AS active_users,
       COUNT(DISTINCT id) AS total_users
FROM   mytable
Sign up to request clarification or add additional context in comments.

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.