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 138455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:16:00+00:00 2026-05-11T07:16:00+00:00

I’m building a MySQL query to determine how many items from each of several

  • 0

I’m building a MySQL query to determine how many items from each of several categories appear in a given date range. My initial attempt looked like this:

select Title,    (select count(*) from entries where CategoryID=1     and Date >= @StartDate and Date <= @EndDate) as Cat1,   (select count(*) from entries where CategoryID=2    and Date >= @StartDate and Date <= @EndDate) as Cat2,   (select count(*) from entries where CategoryID is null    and Date >= @StartDate and Date <= @EndDate) as UnkownCategory from entries   where Date >= @StartDate and Date <= @EndDate 

The table is quite large and I’d like to refactor the query to speed it up, but I’m not sure how – can this be rewritten using GROUP BY/HAVING statements or is there another way I’m missing?

Edit: Sample result set – something like this:

Title | Category 1 Total | Category 2 Total | Unknown Category Total ABC     1                  3                  0 DEF     2                  7                  2 
  • 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. 2026-05-11T07:16:01+00:00Added an answer on May 11, 2026 at 7:16 am
    select Title, SUM(CategoryID=1) as Cat1, SUM(categoryID=2) as Cat2, SUM(categoryID IS NULL) as UnknownCategory FROM entries WHERE Date BETWEEN @StartDate AND @EndDate GROUP BY Title 

    You can stick expressions in sum() functions: truth equals 1, false equals 0. Also I used the BETWEEN operator which is a little faster.

    An alternative that would return a different result layout but is a little conceptually simpler:

    select Title, CategoryID, count(*) from entries WHERE Date BETWEEN @StartDate AND @EndDate group by Title, CategoryID 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was

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.