58

I have a table with total no of 1000 records in it.It has the following structure:

EMP_ID EMP_NAME PHONE_NO   ARR_DATE
1        A        545454 2012/03/12

I want to calculate no of records for every month in year-2012

Is there any way that should solve my issue in a single shot?

I tried:

select count(*) 
from table_emp 
where year(ARR_DATE) = '2012' and month(ARR_DATE) = '01'
4
  • 1
    there doesn't appear to be a question? Commented Mar 27, 2012 at 11:00
  • 1
    see from..... I want to (2nd line) from top for question Commented Mar 27, 2012 at 11:01
  • 1
    BTW Wrapping columns in functions makes the query unsargable. Commented Mar 27, 2012 at 11:01
  • 2
    It would be helpful if you would take some time to state your question clearly and then format it correctly to make it readable. In this case, I think you are asking how to count the number of records in each month of one year, based on the ARR_DATE column but this is a guess. If it's correct, check the documentation for GROUP BY, COUNT(), YEAR() and MONTH().
    – Pondlife
    Commented Mar 27, 2012 at 11:04

4 Answers 4

107
SELECT    COUNT(*) 
FROM      table_emp 
WHERE     YEAR(ARR_DATE) = '2012' 
GROUP BY  MONTH(ARR_DATE)
6
  • why quotes around 2012? YEAR() returns a number AFAIK.
    – iDevlop
    Commented Jun 1, 2016 at 10:37
  • 1
    @iDevlop I just tested it (Microsoft SQL Server 2012 - 11.0.5058.0 (X64))and it works both with and without the quotes.
    – Dan
    Commented Jun 1, 2016 at 11:11
  • 1
    ** eg:- April has not any employee registered.** so that month has 0 counts. how that record adds to the final result. Commented Oct 14, 2017 at 12:33
  • @janaka it would be absent from the result, if you're asking how to force it to include April, 0 I think you should ask it as a new question referencing this one
    – Dan
    Commented Oct 15, 2017 at 13:45
  • @Dan Hi Dan, Can you please tell me If I have to find the record between two years such that I have to show the count from Jan 2019 to Dec 2020 for each month seperately Commented Dec 12, 2020 at 5:32
59

This will give you the count per month for 2012;

SELECT MONTH(ARR_DATE) MONTH, COUNT(*) COUNT
FROM table_emp
WHERE YEAR(arr_date)=2012
GROUP BY MONTH(ARR_DATE);

Demo here.

6
  • 3
    I prefer this one as it gives the month number also
    – AdRock
    Commented Jul 17, 2015 at 11:15
  • Can i get month name instead of number? Example Jan, Feb Commented Jul 24, 2016 at 7:37
  • 3
    use SELECT MONTHNAME(ARR_DATE) MONTH to get "January", "February", etc.
    – chrish
    Commented Sep 7, 2016 at 16:51
  • month number is helpful
    – ahmad ali
    Commented Dec 27, 2017 at 10:11
  • 2
    This code doesn't work because if there is no records for a given month it will not be displayed.
    – Pete
    Commented Aug 10, 2018 at 14:40
22

Try This query:

SELECT 
  SUM(CASE datepart(month,ARR_DATE) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
  SUM(CASE datepart(month,ARR_DATE) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
  SUM(CASE datepart(month,ARR_DATE) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
  SUM(CASE datepart(month,ARR_DATE) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
  SUM(CASE datepart(month,ARR_DATE) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
  SUM(CASE datepart(month,ARR_DATE) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
  SUM(CASE datepart(month,ARR_DATE) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
  SUM(CASE datepart(month,ARR_DATE) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
  SUM(CASE datepart(month,ARR_DATE) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
  SUM(CASE datepart(month,ARR_DATE) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
  SUM(CASE datepart(month,ARR_DATE) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
  SUM(CASE datepart(month,ARR_DATE) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
  SUM(CASE datepart(year,ARR_DATE) WHEN 2012 THEN 1 ELSE 0 END) AS 'TOTAL'
FROM
    sometable
WHERE
  ARR_DATE BETWEEN '2012/01/01' AND '2012/12/31' 
9
  • 1
    +1 for avoiding unsargable predicate but should use unambiguous date formats and the OP should avoid BETWEEN if datetime datatype (OK if date datatype) Commented Mar 27, 2012 at 11:06
  • Assumed it is date type, since there is no "time" in example data
    – cichy
    Commented Mar 27, 2012 at 11:09
  • It will display total number of records from January to December....Monthwise or not..? Commented Mar 27, 2012 at 11:09
  • @manojkumarsingh - It should correctly separate the values by month. I prefer Joachim's query because it is shorter. But both are valid and should produce the same counts, just one in rows and the other in columns.
    – Leigh
    Commented Mar 27, 2012 at 19:20
  • @Leigh small difference between Joachim's and mine queries is that, mine in extremly faster over very large tables, since it doesn't use GROUP BY ;)
    – cichy
    Commented Feb 5, 2017 at 8:25
2
select count(*) 
from table_emp 
 where DATEPART(YEAR, ARR_DATE) = '2012' AND DATEPART(MONTH, ARR_DATE) = '01'
1
  • Your Query and my query output is same.I want to have a way so that i can get data month wise..! Commented Mar 27, 2012 at 11:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.