Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:21:11+00:00 2026-06-01T08:21:11+00:00

I have the following set of data: ID | CREATED | USER ————————– 1

  • 0

I have the following set of data:

ID  |  CREATED     |  USER
--------------------------
1   |  2012-01-14  |   XYZ
2   |  2012-03-14  |   XYZ
3   |  2012-03-15  |   XYZ
4   |  2012-03-24  |   ABC
5   |  2012-04-10  |   XYZ
6   |  2012-04-11  |   ABC

And I need a report that shows the COUNT for a given user but also with a 0 for months where there are no records.

MTH  |  COUNT
-------------
JAN  |   1
FEB  |   0
MAR  |   2
APR  |   1

I managed to get it to work but without the 0 for months with no records. So far, I have this syntax which is throwing an error..

SELECT Month(CREATED), COUNT(SELECT * FROM SEARCHES WHERE USER = 'XYZ')
FROM SEARCHES
GROUP BY Month(CREATED)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:21:13+00:00Added an answer on June 1, 2026 at 8:21 am

    Here is general query for grouping by date range. @startDate and @endDate define the range; you might set min(created) and max(created) for user, or some fixed range.

    CTE monthlyRange generates table of months – first of current month, first of next month. Searches is later left-joined to monthlyRanges by created. Note that user is filtered in join as this is left join and filtering in where clause would effectively turn it into inner join.

    declare @startDate datetime
    declare @endDate datetime
    set @startDate = '2012-01-01'
    set @endDate = '2013-01-01'
    
    ; with monthlyRange (startMonth, startNextMonth) as (
      select dateadd (m, datediff (m, 0, @startDate), 0),
             dateadd (m, datediff (m, 0, @startDate) + 1, 0)
      union all
      select dateadd (m, 1, startMonth),
             dateadd (m, 1, startNextMonth)
        from monthlyRange
      where startNextMonth <= dateadd (m, datediff (m, 0, @endDate), 0)
    )
    SELECT Year(monthlyRange.startMonth) Year, 
           Month(monthlyRange.startMonth) Month, 
           COUNT(searches.Created) Count
      FROM monthlyRange 
      left join SEARCHES
        on monthlyRange.startMonth <= Searches.Created
       and monthlyRange.startNextMonth > Searches.Created
       and [USER] = 'XYZ'
    GROUP BY Year(monthlyRange.startMonth), Month(monthlyRange.startMonth)
    order by 1, 2
    

    Here is Sql Fiddle for testing

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data set that I am trying to plot with ggplot2,
I have a data set that is organized in the following manner: Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 ...
I have the swiss data set provided by R, which has the following form:
I have the following data structure: [DataServiceEntity] public class User { public string RowKey
I have the following scenario: My page has a dropdown list that allows user
I am trying to set twoway binding on a UserControl that I have created.
I'm trying to map a Dictionary containing Lists. I have the following set of
I'm using log4Net for my logging. I also have the following set... <log4net debug=true>
I have the following relationships set up: A HABTM B B belongsTo C C
I have the following code set up in my Startup IDictionary<string, string> properties =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.