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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:12:20+00:00 2026-05-21T03:12:20+00:00

I have a table with some columns, one holds a timestamp that I use

  • 0

I have a table with some columns, one holds a timestamp that I use to run
currently four single selects to compute a SELECT count() and return information
like “n-rows older than 1 week”, “n-rows older than 2 weeks”, …

How to transform the four queries into one SQL-Statement that is hopefully
running faster?

The statements look like this:

SELECT count(foo_pk) AS oneweek FROM foo WHERE foo_timstamp < DATE_SUB(now(), INTERVAL 1 week)
  • 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-21T03:12:21+00:00Added an answer on May 21, 2026 at 3:12 am

    I’m not a MySql guy, but in SQL Server I would use the CASE statement like so:

    SELECT
        SUM(CASE WHEN foo_timstamp < DATE_SUB(now(), INTERVAL 1 week) THEN 1 ELSE 0 END) as oneweek,
        SUM(CASE WHEN foo_timstamp < DATE_SUB(now(), INTERVAL 2 week) THEN 1 ELSE 0 END) as twoweek,
        SUM(CASE WHEN foo_timstamp < DATE_SUB(now(), INTERVAL 3 week) THEN 1 ELSE 0 END) as threeweek,
        SUM(CASE WHEN foo_timstamp < DATE_SUB(now(), INTERVAL 4 week) THEN 1 ELSE 0 END) as fourweek
    FROM foo
    WHERE 
        foo_timstamp < DATE_SUB(now(), INTERVAL 1 week)
    

    It seems that this statement is available in MySql as well, so while my syntax might be a bit off I think something like the SQL above should work.

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

Sidebar

Related Questions

I have a table and one of the columns holds web addresses like: 'http://...'
I have a script that appends some rows to a table. One of the
I have a table that saves some account limits like users. For most rows
I have a table that contains some blob fields that I don't want to
I have some char() fields in a DBF table that were left encrypted by
I have to delete some rows from a data table. I've heard that it
I have a table TreeStructures wich holds structures of Trees. This table has columns
I have a table var with some rows but only one column of type
I have a database table full of some really ugly and messy data. In
I have a table with say 1640 items. I set bindingSource.Filter = some filter

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.