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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:21:02+00:00 2026-06-18T14:21:02+00:00

I am trying to count events (which are rows in the event_table) in the

  • 0

I am trying to count events (which are rows in the event_table) in the year before and the year after a particular target date for each person. For example, say I have a person 100 and target date is 10/01/2012. I would like to count events in 9/30/2011-9/30/2012 and in 10/02/2012-9/30/2013.

My query looks like:

select * 
from (
    select id, target_date
    from subsample_table
    ) as i
left join (
   select id, event_date, count(*) as N
        , case when event_date between target_date-365 and target_date-1 then 0 
               when event_date between target_date+1 and target_date+365 then 1
               else 2 end as after 
   from event_table
   group by id, target_date, period
   ) as h
on   i.id = h.id 
 and i.target_date = h.event_date

The output should look something like:

id  target_date  after  N
100 10/01/2012   0      1000
100 10/01/2012   1      0    

It’s possible that some people do not have any events in the before or after periods (or both), and it would be nice to have zeros in that case. I don’t care about the events outside the 730 days.

Any suggestions would be greatly appreciated.

  • 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-18T14:21:03+00:00Added an answer on June 18, 2026 at 2:21 pm

    I think the following may approach what you are trying to accomplish.

       select id
             , target_date
             , event_date
             , count(*) as N
             , SUM(case when event_date between target_date-365 and target_date-1 
                        then 1
                        else 0
                    end) AS Prior_ 
             , SUM(case when event_date between target_date+1 and target_date+365 
                        then 1
                        else 0
                   end) as After_              
          from subsample_table i
          left join 
               event_table h
            on i.id = h.id 
           and i.target_date = h.event_date
         group by id, target_date, period
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to count the number of rows whose date has not yet
I am trying to count events that happen in a day, which in itself
Trying to count how many elements within the array are not equal to 0,
I am trying to count the number of times a user has clicked on
I am trying to count the occurrences of certain dates in my MySQL table
I'm trying to count the number of times that -- occurs in a string.
I'm trying to count how many distinct value of FLOOR there is but I
I'm trying to count the number of records that have a null DateTime value.
I'm trying to count all the values inside an array, I've tried using count()
I am trying to count characters in comments included in C code using Python

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.