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

  • Home
  • SEARCH
  • 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 868861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:12:49+00:00 2026-05-15T10:12:49+00:00

I have a database table that stores document information (DocID, Name, Type, Owner, etc).

  • 0

I have a database table that stores document information (DocID, Name, Type, Owner, etc). Then I have another table that stores the times the document has been downloaded (DocID, DownloadTime, etc). I’m trying to set up a page for recording the number of hits that different kinds of documents (policies, forms, publications, etc) get in a specific month and year (I have a dropdown for each that the user selects).

I also want to be able to leave Month or Year empty, so the user can get hits for all months in a year, for example. The query below works ok when the user selects the month and year. However, when month or year is left empty the gridview doesn’t show the document types grouped. For example if I want to see hits for January 2010, I get:

Policies : 210 hits
Forms: 98 hits

etc

But when the users chooses all months in 2010, the user gets:

Policies: 210 hits
Policies: 87 hits
Policies: 23 hits
Policies: 87 hits
Forms 

etc

So I’m getting all months in different rows but not grouped as one kind. Where is the query wrong?

SELECT COUNT(Counter.DocID) AS Counter,
       Doc.DocType,
       MONTH(Counter.DownloadDate) AS MonthDownload,
       YEAR(Counter.DownloadDate) AS YearDownload
  FROM Counter
       INNER JOIN Doc ON Counter.DocID = Doc.DocID
 GROUP BY Doc.DocType,
       MONTH(Counter.DownloadDate),
       YEAR(Counter.DownloadDate)
HAVING (MONTH(Counter.DownloadDate) = @Month1 OR @Month1 IS NULL) AND
       (YEAR(Counter.DownloadDate) = @Year1 OR @Year1 IS NULL)
  • 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-05-15T10:12:50+00:00Added an answer on May 15, 2026 at 10:12 am

    The issue is in your GROUP BY clause.

    If you say GROUP BY DocType, Month, Year that means that any time any of those is different from the others, count them separately.

    What you need to do is GROUP BY year only if you want the total for the whole year, and group by month, year if you want the count for each month of each year.

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

Sidebar

Related Questions

I have a database table that stores various type of operators and values that
I have a database that has a table email_eml that stores 3 attributes name_eml,
Ok I have a table in my SQL Server database that stores comments. My
I have a database table that has a Unique Key constraint defined to avoid
I have a database table that is a dictionary of defined terms -- key,
that my problem: I have database table like that: id (AI) market_id 1 6
I have a large database table that I need to display on a Windows
I have a database table TravelRequest that contains, amongst other things, the fields SignOffName
I have a very simple database table that joins two other tables in a
I have some code to update a database table that looks like try {

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.