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

  • Home
  • SEARCH
  • 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 4256418
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:21:03+00:00 2026-05-21T05:21:03+00:00

I got a table with a list of transactions. for the example, lets say

  • 0

I got a table with a list of transactions.
for the example, lets say it has 4 fields:
ID, UserID, DateAddedd, Amount

I would like to run a query that checks if there was a time, that in 30 days, a user made transactions in the sum of 100 or more

I saw lots of samples of grouping by month or a day but the problem is that if for example
a user made a 50$ transaction on the 20/4 and on the 5/5 he made another 50$ transaction, the query should show it. (its 100$ or more in a period of 30 days)

  • 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-21T05:21:04+00:00Added an answer on May 21, 2026 at 5:21 am

    I think that this should work (I’m assuming that transactions have a date component, and that a user can have multiple transactions on a single day):

    ;with DailyTransactions as (
        select UserID,DATEADD(day,DATEDIFF(day,0,DateAdded),0) as DateOnly,SUM(Amount) as Amount
        from Transactions group by UserID,DATEADD(day,DATEDIFF(day,0,DateAdded),0)
    ), Numbers as (
        select ROW_NUMBER() OVER (ORDER BY object_id) as n from sys.objects
    ), DayRange as (
        select n from Numbers where n between 1 and 29
    )
    select
        dt.UserID,dt.DateOnly as StartDate,MAX(ot.DateOnly) as EndDate, dt.Amount + COALESCE(SUM(ot.Amount),0) as TotalSpend
    from
        DailyTransactions dt
            cross join
        DayRange dr
            left join
        DailyTransactions ot
            on
                dt.UserID = ot.UserID and
                DATEADD(day,dr.n,dt.DateOnly) = ot.DateOnly
    group by dt.UserID,dt.DateOnly,dt.Amount
    having dt.Amount + COALESCE(SUM(ot.Amount),0) >= 100.00
    

    Okay, I’m using 3 common table expressions. The first (DailyTransactions) is reducing the transactions table to a single transaction per user per day (this isn’t necessary if the DateAdded is a date only, and each user has a single transaction per day). The second and third (Numbers and DayRange) are a bit of a cheat – I wanted to have the numbers 1-29 available to me (for use in a DATEADD). There are a variety of ways of creating either a permanent or (as in this case) temporary Numbers table. I just picked one, and then in DayRange, I filter it down to the numbers I need.

    Now that we have those available to us, we write the main query. We’re querying for rows from the DailyTransactions table, but we want to find later rows in the same table that are within 30 days. That’s what the left join to DailyTransactions is doing. It’s finding those later rows, of which there may be 0, 1 or more. If it’s more than one, we want to add all of those values together, so that’s why we need to do a further bit of grouping at this stage. Finally, we can write our having clause, to filter down only to those results where the Amount from a particular day (dt.Amount) + the sum of amounts from later days (SUM(ot.Amount)) meets the criteria you set out.

    I based this on a table defined thus:

    create table Transactions (
        UserID int not null,
        DateAdded datetime not null,
        Amount decimal (38,2)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for vague question title. I've got a table containing huge list of, say,
Ive got a button in a UICell. I want a list(a table view like
I'm building a form with php/mysql. I've got a table with a list of
I've got a table of hardware and a table of incidents. Each hardware has
I've got a table with a header, a row with input fields, rows with
I have got a table in MS Access 2007 with 4 fields. Labour Cost
I've got a table that stores inventory for a store that looks like this
I've got a checkbox list in a table. (one of a number of CB's
I got a table with a custom TableCellEditor (extending DefaultCellEditor) with a JFormattedTextField as
If I've got a table containing Field1 and Field2 can I generate a new

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.