Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
How can I get a user's activity count for today and this month in a single SELECT query

In my table I have:

Activity : Date
---------------
doSomething1 : June 1, 2020
doSomething2 : June 14, 2020

I want to be able to make a query so that I can get the following result (assuming today is June 1, 2020):

Today : ThisMonth
1 : 2

I looked at group by but I wasn't sure how to do that without a lot of additional code and I think there's very likely a much simpler solution that I'm missing. Something that will just return a single row with two results. Is this possible and if so how?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Is this what you want?

select array_agg(activity) filter (where date = current_date) as today,
       array_agg(activity) filter (where date <> current_date) as rest_of_month
from t
where date_trunc('month', date) = current_date;

This uses arrays so it can handle more than one activity in either category.

over 4 years ago · Santiago Trujillo Report

0

you can write subqueries to get data in single row,

Select today , month 
from
(
( query to get today's count ) as today,
( query to get month's count ) as month
) t;

yes, u can do group by on dates to get todays nd months count. Hope this will give u some perception to go on.

over 4 years ago · Santiago Trujillo Report

0

Assume you want to query based on a particular date -

select count(case when d.date = :p_query_date then 0 end) day_count
      ,count(0) month_count
  from d -- your table name
 where d.date between date_trunc('month', :p_query_date) 
                  and date_trunc('month', :p_query_date + interval '1 month') - interval '1 day' 

The above query assumes you have index defined on d.date column. If you have index defined on date_trunc('month', date), the query condition can be simplified to:

date_trunc('month', d.date) = date_trunc('month', :p_query_date)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!