2

For example I have a data here

ID    Doctor    Status
1     Wooka     Approved
2     Jamba     Approved
3     Jamba     Approved
4     Wooka     Approved
5     Wooka     Approved
6     Wooka     Approved
7     Wooka     Approved
.    .          .
.    .          .
.    .          .
99    

For the first 7 rows (demonstration purposes, not limited to 7) I want it to output like this

Doctor    Count
Wooka     5
Jamba     2

Although I am not sure how to do that in COUNT()

SELECT COUNT(*) FROM table WHERE status='Approved'

I really don't have any idea how to make it flexible as to what I've asked.

1
  • 1
    Look up how to GROUP BY Commented Jul 20, 2016 at 16:36

2 Answers 2

2
SELECT
  t.doctor,
  COUNT(*) AS c
FROM
  table t
WHERE
  t.status = 'Approved'
GROUP BY
  t.doctor
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't handle the "first 7 rows" requirement.
0

You could use a limit clause in a subquery to limit the number of rows returned, and then wrap this query with an aggregate query that performs a count:

SELECT   doctor, COUNT(*)
FROM     (SELECT   doctor
          FROM     mytable
          WHERE    status = 'Approved'
          ORDER BY id ASC
          LIMIT    7 -- or some other number
         ) t
GROUP BY doctor

4 Comments

@Noobster yes. That's what the group by clause is there for
Hello, yes this works. But im wondering how if to map all the status? Like Approved, Pending, Denied, ...the list goes on and on?
@Noobster If you want a count per status, drop the inner where clause and add the status to the group by clause.
@Noobster yes, indeed.

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.