0

I have table like this:

Year    Month   Type
2013    4   31
2013    3   31
2014    5   40
2014    6   41
2015    5   31
2015    7   40
2013    4   31
2013    3   31
2014    5   40
2014    6   41
2015    5   31
2015    7   40
2013    4   31
2013    3   31

I would like to count number of appearance of each combination. So output should be something like this:

Year    Month   Type    Count_of_appearance
2013    3   31  3
2013    4   31  3
etc..           

I've been using something like:

SELECT Year,
       Month,
       Type,
       (SELECT COUNT (*)
          FROM myTable
         WHERE (...im lost in defining a condition in order to make this work...)
           AS Count_of_appearance
  FROM employee;

I'm lost at defining functional WHERE clause.

Problem is I a have a lot of fields like this and a lot of unique values. Table is 8Gb big.

1 Answer 1

1

You are looking for GROUP BY:

SELECT Year, Month, Type, COUNT(*) AS Count_of_appearance
FROM employee
GROUP BY Year, Month, Type
ORDER BY Year, Month, Type;

To ensure that the results are in the right order, you should include an ORDER BY as well.

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.