question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GroupBy and Aggregate over date ranges

See original GitHub issue

Problem

Need to be able to group and aggregate over ranges of dates (ie. every month, every 2 months, every year, etc)

Suggested solution

Add datetime operators to the groupBy key. Maybe like:

{ groupBy: { dateCol: { 'year': 1 } } to group by year or { groupBy: { dateCol: { 'month': 2 } } to group by 2-month intervals

Prisma generate could emit these groupBy interfaces for any column that is a datetime.

Additional context

This was commented as a suggestion last year on the original groupBy issue: https://github.com/prisma/prisma-client-js/issues/1#issuecomment-716578358

But it seems it was ignored and the original issue is now closed 😞

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:135
  • Comments:25

github_iconTop GitHub Comments

36reactions
melankecommented, Oct 29, 2021

This is extremely important. Another usecase: Timeline Chart

18reactions
bmcilw1commented, Sep 9, 2021

I would appreciate this feature too. To make this less abstract I’ll provide a stretch example that’s a good bit beyond what we can do so far. Here’s an example query that I’m currently doing

    return this.prisma.$queryRaw`
      SELECT accountId, COUNT(id) as loginOrResumeCount, DATE(CONVERT_TZ(timestamp,'+00:00','-5:00')) as summaryDate 
      FROM ActivityLog WHERE typeOid = ${ACTIVITY_TYPE.login_or_resume} 
      AND timestamp >= ${startDate} AND timestamp < ${endDate} 
      GROUP BY accountId, summaryDate;`;

To fully support this query, Prisma would need

  • Group by support
  • Count by column
  • Group by DateTime column (this issue)
  • Casting of DateTime to Date UTC (I believe should also be supported when dates are added)
  • Casting of DateTime to Date taking into account timezone information stored in the DateTime field (only if MySQL/SQL etc. allows you to store a DateTime in a non-UTC format, I’m not sure if it does)?
  • Casting of DateTime to the Date boundaries of a passed-in timezone

To really add support for grouping by dates, we probably need to handle each one of these tasks or we’re still going to need to do them with raw queries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Grouping by date range with pandas - python - Stack Overflow
I'd convert this to a datetime column and then use pd.TimeGrouper : dates = pd.to_datetime(df.date, format='%m-%d-%y') print(dates) 0 ...
Read more >
How to group by date time range in pandas? - EasyTweaks.com
Step #2: Aggregate by datetime /timestamp. Aggregating by our date range is relatively simple: performance.groupby ([dates.dt.year]).agg(overall_sales= ...
Read more >
How to Group Pandas DataFrame By Date and Time
In this article, we will discuss how to group by a dataframe on the basis of date and time in Pandas. We will...
Read more >
Pandas: Groupby to find first dates for each group - w3resource
Pandas Grouping and Aggregating Exercises, Practice and Solution: Write a Pandas program to split the following dataset using group by on ...
Read more >
Date range aggregation | Elasticsearch Guide [8.5] | Elastic
A range aggregation that is dedicated for date values. The main difference between this aggregation and the normal range aggregation is that the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found