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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:49:56+00:00 2026-05-13T08:49:56+00:00

I have a table called trends_points , this table has the following columns: id

  • 0

I have a table called trends_points, this table has the following columns:

  • id (the unique id of the row)
  • userId (the id of the user that has entered this in the table)
  • term (a word)
  • time (a unix timestamp)

Now, I’m trying to run a query on this table which will get the rows in a specific time frame ordered by how many times the column term appears in the table during the specific timeframe…So for example if the table has the following rows:

id | userId | term        | time
------------------------------------
1    28       new year      1262231638
2    37       new year      1262231658
3    1        christmas     1262231666
4    34       new year      1262231665
5    12       christmas     1262231667
6    52       twitter       1262231669

I’d like the rows to come out ordered like this:

new year
christmas
twitter

This is because “new year” exists three times in the timeframe, “christmas” exists twice and “twitter” is only in one row.

So far I’ve asummed it’s a simple WHERE for the specific timeframe part of the query and a GROUP BY to stop the same term from coming up twice in the list.

This makes the following query:

SELECT * 
  FROM `trends_points` 
 WHERE ( time >= <time-period_start> 
  AND time <= <time-period_end> ) 
GROUP BY `term`

Does anyone know how I’d do the final part of the query? (Ordering the query’s results by how many rows contain the same “term” column value..).

  • 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-13T08:49:56+00:00Added an answer on May 13, 2026 at 8:49 am

    Use:

      SELECT tp.term,
             COUNT(*) 'term_count'
        FROM TREND_POINTS tp
       WHERE tp.time BETWEEN <time-period_start> AND <time-period_end> 
    GROUP BY tp.term
    ORDER BY term_count DESC, tp.term
    

    See this question about why to use BETWEEN vs using the >=/<= operators.

    Keep in mind there can be ties – the order by defaults to alphabetically shorting by term value when this happens, but there could be other criteria.

    Also, if you want to additionally limit the number of rows/terms coming back you can add the LIMIT clause to the end of the query. For example, this query will return the top five terms:

      SELECT tp.term,
             COUNT(*) 'term_count'
        FROM TREND_POINTS tp
       WHERE tp.time BETWEEN <time-period_start> AND <time-period_end> 
    GROUP BY tp.term
    ORDER BY term_count DESC, tp.term
       LIMIT 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.