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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:58:02+00:00 2026-06-16T11:58:02+00:00

My current SQL looks like this – SELECT i.id, i.name, i.description, c.name campaign, count(h.id)

  • 0

My current SQL looks like this –

SELECT i.id, 
       i.name,
       i.description,
       c.name campaign,
       count(h.id) hits
FROM items i 
LEFT JOIN campaigns c ON c.id = i.campaignid
LEFT JOIN links l ON l.itemid = i.id
LEFT JOIN hits h ON h.linkid = l.id
GROUP BY i.id

I would like to modify this so a 6th column is returned with the count() of h.id, but only including “hits” from the last 30 days. So assuming that there were 100 “hits” overall and 50 in the last thirty days, the 5th column would be 100 and the 6th would be 50.

Apologies if my description is quite wordy…

  • 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-16T11:58:02+00:00Added an answer on June 16, 2026 at 11:58 am
    SELECT i.id, 
           i.name,
           i.description,
           c.name campaign,
           count(h.id) hits,
           COUNT( CASE WHEN h.hitdate >= CURDATE() - INTERVAL 29 DAY
                         THEN h.hitdate 
                         ELSE NULL
                  END
                ) AS last_30_days_hits
    FROM ...
    

    You could use the shorter (but not necessarily faster):

           SUM( h.hitdate >= CURDATE() - INTERVAL 29 DAY )
             AS last_30_days_hits
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in SQL Server which looks like this: ID Code Name
I have a query that looks like this: SELECT OrganizationName, OrganizationID, ReceivableStatus, InvoiceFee FROM
I have a MySQL Table that looks like this: The SQL to create the
I've got a sql output into a data.frame which looks like this: dateTime resultMean
If I have a SQL table that looks like this below how can I
My sql query looks like this ALTER TABLE `exercises` ADD COLUMN `creation_dt` timestamp NOT
I have an SQL table called "posts" that looks like this: id | category
Lets say I have a database that looks like this: tblA: ID, Name, Sequence,
the current SQL query works fine locally on MAMP 1.8.4 running MySQL 5.1.37. SELECT
I'm a postgresql user and I'm trying to follow this : http://www.postgresql.org/docs/current/interactive/sql-createtrigger.html CREATE TRIGGER

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.