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

The Archive Base Latest Questions

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

I have two tables: first one (amount): amountid date amount 1 01.01.2011 100 2

  • 0

I have two tables:

first one (amount):

  amountid      date         amount
    1         01.01.2011      100
    2         02.01.2011       50
    3         04.01.2011      200
    4         10.01.2011      20
    5         11.01.2011      5

second one (period):

  periodid      date
     1          01.01.2011
     2          03.01.2011
     3          05.01.2011

I need group first table by times from second table, and sum (amount):

for this example answer is:

      01.01.2011    150
      03.01.2011    200  
      05.01.2011    25

help me with sql query please.

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

    Sample data

    declare @Amount table
    (
      amountid int,
      [date] datetime,
      amount int
    )
    
    declare @Period table
    (
      periodid int,
      [date] datetime
    )
    
    insert into @Amount
    select 1, '20110101', 100 union all
    select 2, '20110102', 50  union all
    select 3, '20110104', 200 union all
    select 4, '20110110', 20  union all
    select 5, '20110111', 5
    
    insert into @Period
    select 1, '20110101' union all
    select 2, '20110103' union all
    select 3, '20110105'
    

    The query

    select P.StartDate,
           sum(A.amount) as amount
    from (
          select P1.[date] as StartDate,
                 coalesce((select min(P2.[date])
                           from @Period as P2
                           where P1.[date] < P2.[date]), '99991231') as endDate
          from @Period as P1
         ) as P      
      inner join @Amount as A
        on A.[date] >= P.StartDate and 
           A.[date] < P.EndDate
    group by P.StartDate   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is for an upcoming project. I have two tables - first one keeps
I have two tables like the below one in my DB. In the first
I have two tables: invoices & invoice_items. I need a one to many relationship
I have two tables. The first one contains all the item informations and the
I have two tables in my database. The first one, [nodeActivity] has the following
I have two tables: order and order_status . In the first one I store
I have two tables The First one contains information about staff who take courses
I have two tables user (one) and transaction (many) and I need to get
I have two tables first one consist of id`s which are associated with names
I have two tables in my application. The first one, Sections, stores content for

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.