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

The Archive Base Latest Questions

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

I have this very tricky problem and Im trying to figure it out for

  • 0

I have this very tricky problem and Im trying to figure it out for a while now,

I have this query result set

    SELECT * FROM Orders

    OrderID | OrderAmount | OrderDate | Expiry Date
    1        $100          2008-01-01    2009-12-31
    2        $200          2009-01-01    2010-12-31
    3        $300          2010-01-01    2011-12-31
    4        $3            2010-01-01    2010-06-31
    5        $400          2007-01-01    2009-05-31

Now, how can i break down each order by OrderDate – ExpiryDate daterange per YEAR

I want the result something like this in my RDLC report

    ORDERS CONSUMED PER YEAR    

    OrderID  |  YEAR     |    Consumed Amount
     1         2008           $50
     1         2009           $50
     2         2009           $100
     2         2010           $100
     3         2010           $150
     3         2011           $150  
     4         2010           $3
     5         2007           $160
     5         2008           $160
     5         2009           $80   <---- another tricky part

The computation is base on the term, eq. (2 year term of $300 means $150 per year)

How can i do this in MS-SQL Query?

** I know the title seems incorrect ^^, i just cant find the right title

Edited: added more samples and explanation

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

    If this is SQL Server 2005 or later, you can use a common table expression to recursively build a table of months and use that to calculate the order amount consumed per year.

    A few notes:

    1. This solution assumes that the time periods are based on whole months. The approach is to determine the amount of the order consumed per month, then multiply that by the number of months in the year. You could modify this to use an amount consumed per day if needed.

    2. In the final select, I cast from money to a decimal with higher precision to try and avoid rounding problems. You may have to adjust this to fit your needs, but I’m not sure that you will be able to avoid rounding problems entirely.

    3. The results for OrderID 5 don’t match your sample results. This is because the example has 5 months in 2009, not 6.


    create table #Orders
    (
        OrderID int,
        OrderAmount money,
        OrderDate datetime,
        ExpiryDate datetime
    )
    
    insert into #Orders values(1, 100, '2008-01-01', '2009-12-31'),
                              (2, 200, '2009-01-01', '2010-12-31'),
                              (3, 300, '2010-01-01', '2011-12-31'),
                              (4, 3,   '2010-01-01', '2010-06-30'),
                              (5, 400, '2007-01-01', '2009-05-31')
    
    ;with cte_months
    as
    (
        select OrderID, OrderDate, year(OrderDate) OrderYear
        from #Orders
        union all
        select m.OrderID, dateadd(month, 1, m.OrderDate), year(dateadd(month, 1, m.OrderDate)) 
        from cte_months m
            inner join #Orders o on m.OrderID = o.OrderID
        where dateadd(month, 1, m.OrderDate) <= o.ExpiryDate
    )
    select m.OrderId, m.OrderYear, cast(sum(o.MonthlyAmount) as money) as ConsumedAmount
    from
    (
        select OrderID, cast(OrderAmount as decimal(12,6)) / (datediff(month, orderdate, ExpiryDate) + 1) as MonthlyAmount
        from #Orders
    ) o inner join cte_months m on o.OrderID = m.OrderID
    group by m.OrderID, m.OrderYear
    
    drop table #Orders
    

    And the results:

    OrderId     OrderYear   ConsumedAmount
    ----------- ----------- ---------------------
    1           2008        50.00
    1           2009        50.00
    2           2009        100.00
    2           2010        100.00
    3           2010        150.00
    3           2011        150.00
    4           2010        3.00
    5           2007        165.5172
    5           2008        165.5172
    5           2009        68.9655
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now this is a tricky problem for which I'm not able to figure out
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
I have another orientation problem. But this one is very tricky. My RootViewController is
Here is very tricky situation i have tried many things but cant figure it
I have a tricky problem. I have a very complex view in MS SQL
We have a very tricky interop problem wherein the thread used to initialize a
I have a very tricky problem and after long searching (google, stackoverflow, ...) i
I have this very simple example that I am using to learn structs in
I have this very straight forward question regarding Thread and Timer classes in Java
I have this very simple C++ class: class Tree { public: Node *head; };

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.