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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:32:27+00:00 2026-05-16T11:32:27+00:00

I have a data table (will be millions of records but I will make

  • 0

I have a data table (will be millions of records but I will make it simple here) that looks like this.

ID   APPROVAL_DT       DAY_DT        TRANS_COUNT     SALE_AMOUNT
1    2010-04-22        2010-04-27    2               260
1    2010-04-22        2010-04-28    1               40
2    2010-03-28        2010-04-02    1               5
2    2010-03-28        2010-04-03    5               10
2    2010-03-28        2010-04-04    1               20
3    2010-04-25        2010-05-01    6               10
3    2010-04-25        2010-05-02    4               10
4    2010-06-01        2010-06-07    1               5

I need to figure out the DAY_DT for each ID where either the sum of all previous and current DAY_DT TRANS_COUNTs >=10 OR sum of all previous and current DAY_DT SALE_AMOUNTs >= 25

So the results of the query applied to the above table would be

ID   APPROVAL_DT  ACTIVATED_DT
1    2010-04-22   2010-04-27
2    2010-03-28   2010-04-04
3    2010-04-25   2010-05-02
4    2010-06-01   NULL

Any thoughts?

  • 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-16T11:32:27+00:00Added an answer on May 16, 2026 at 11:32 am

    I assume you mean that you want to find, within an ID, the first day_dt for which the sum of previous day_dt is trans_count >= 10 or sales_amount >= 25. You call this found day the ‘activated_dt’. You description is quite different from this because it does not specifies that you want only the first day, and it asks for sum of all previous days while your example result shows the sum up to the day.

    I agree with Martin here that a running total would be the best performing one, as it could produce the result in a single scan of the table.

    A result w/o running totals would have to compute the previous days totals for each day_dt and then pick the the first one for each ID:

    with cte1 as (
    select
      t.id,
      t.approval_dt,
      t.day_dt as activated_dt
    from Table t
    cross apply (
      select sum(trans_count) as sum_tc,
         sum(sale_amount) as sum_sa,
         max(day_dt) as max_day_dt
      from table c
      where c.id = t.id
      and c.day_dt <= t.day_dt) as p
    where p.sum_tc >= 10
    or p.sum_sa >=25)
    , cte2 as (
      select id
       , approval_dt
       , activated_dt
       , row_number() over (partition by id order by activated_dt) as rn
      from cte1)
    select *
    from cte2
    where rn = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table storing millions of rows. It looks something like this: Table_Docs
I'm a bit confused on this. I have a data table structured like this:
I have an InnoDB table that lists customer orders - this table records the
I have table with millions of records (rows). And in this table there is
I have created an archive table which will store data for selecting only. Daily
I have data from a table in a database (string) that contain text and
I already have 80 million records inserted into a table, but need to ensure
I have a table of play data that I'm using for a prototype. I'm
I have a table that has 8 million records, with many fields, including lat/long
I have a log table that gets processed every night. Processing will be done

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.