3

Table works(emNo, comNo, salary).

I can get distinct comNo using "select distinct comNo from works". Suppose it gives me a column with 5 rows. How do I count "emNo`" for each of those rows?

3
  • Do you like to count or to sum the values? Commented Mar 16, 2013 at 14:03
  • Ok, reading your question carefully I understand that you want to count ;) Commented Mar 16, 2013 at 14:06
  • but i need to get the max in the count column. how do i get that? Commented Mar 16, 2013 at 14:22

1 Answer 1

5

You can use GROUP BY to aggregate per type of comNo.

SELECT
  comNo,
  count(emNo)
FROM
  works
GROUP BY
  comNo

This will return one row per distinct value of comNo along with the count of records per group.

Demo: http://www.sqlfiddle.com/#!2/4f5df/1

Sign up to request clarification or add additional context in comments.

3 Comments

i also need to get the max in the count column. how do i get that?
You can sort by count(emNo), and then take the first row, e.g., .... ORDER BY count(emNo) DESC LIMIT 1: sqlfiddle.com/#!2/4f5df/17
.... ORDER BY count(emNo) DESC this gives me result but when i add .... ORDER BY count(emNo) DESC limit 1 i get error (command not properly ended)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.