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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:00:24+00:00 2026-05-12T00:00:24+00:00

For simplicities sake, I’ll make up a similar example to what I have: Let’s

  • 0

For simplicities sake, I’ll make up a similar example to what I have:

Let’s say a db has a table of orders with an OrderDate field and a Company field. Then there’s a table of Companies and each record has a YearEndingDate (which signifies that the year ends on that date each year, e.g. 6/6).

I need to add up all of the orders for each year.

I assume it would have to be something like this but I can’t quite figure it out:

SELECT SUM(orderValue),
CASE WHEN orderDate <= YearEndingDate THEN DatePart(year, orderDate)
CASE WHEN orderDate > YearEndingDate THEN DatePart(year, orderDate) + 1
END as Year
FROM Orders
INNER JOIN Company ON Company.companyID = Order.companyID
GROUP By Company, Year

Any ideas?

  • 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-12T00:00:24+00:00Added an answer on May 12, 2026 at 12:00 am

    Not sure what RDMS you are using but this should do the trick. The datepart and dateadd stuff are tsql specific but I’d assume you’d have access to similar functions on whatever platform you are using. The case in the where determines which year value to use.

    Answer:

    select c.companyid
          ,yearendingdate + '/' + convert(varchar, datepart(yy, dateadd(yy,1,o.orderdate))) as yearending
          ,sum(ordervalue) as numberoforders
      from @orders o
           join @companies c
             on o.companyid = c.companyid
     where orderdate between case when (cast(yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate)) as datetime) >= o.orderdate)
                             then yearendingdate + '/' + convert(varchar, datepart(yy, dateadd(yy,-1,o.orderdate)))
                             else yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate))
                              end
                         and 
                             case when (cast(yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate)) as datetime) >= o.orderdate)
                             then yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate))
                             else yearendingdate + '/' + convert(varchar, datepart(yy, dateadd(yy,1,o.orderdate)))
                              end
     group by c.companyid, o.orderdate, yearendingdate
    

    Code to figure out problem:

    declare @orders table (OrderDate datetime
                          ,CompanyID varchar(20)
                          ,OrderValue int)
    
    insert into @orders
    values (getdate(),'MS',2)
    
    insert into @orders
    values (DateAdd(year, -1, getdate()),'MS',3)
    
    insert into @orders
    values (DateAdd(year, -1, getdate()),'MS',1)
    
    insert into @orders
    values (DateAdd(year, 1, getdate()),'MS',4)
    
    insert into @orders
    values (DateAdd(year, 1, getdate()),'Blizzard',2)
    
    insert into @orders
    values (getdate(),'MS',11)
    
    declare @companies table (CompanyID varchar(20)
                             ,YearEndingDate varchar(20))
    
    insert into @companies
    values ('MS', '05/6')
    
    insert into @companies
    values ('Blizzard', '07/01')
    
    select c.companyid
          ,o.orderdate
          ,yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate)) as sameyear
          ,yearendingdate + '/' + convert(varchar, datepart(yy, dateadd(yy,1,o.orderdate))) as plusyear
          ,yearendingdate + '/' + convert(varchar, datepart(yy, dateadd(yy,-1,o.orderdate))) as minusyear
      from @orders o
           join @companies c
             on o.companyid = c.companyid
    
    select c.companyid
          ,yearendingdate + '/' + convert(varchar, datepart(yy, dateadd(yy,1,o.orderdate))) as yearending
          ,sum(ordervalue) as numberoforders
      from @orders o
           join @companies c
             on o.companyid = c.companyid
     where orderdate between case when (cast(yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate)) as datetime) >= o.orderdate)
                             then yearendingdate + '/' + convert(varchar, datepart(yy, dateadd(yy,-1,o.orderdate)))
                             else yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate))
                              end
                         and 
                             case when (cast(yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate)) as datetime) >= o.orderdate)
                             then yearendingdate + '/' + convert(varchar, datepart(yy, o.orderdate))
                             else yearendingdate + '/' + convert(varchar, datepart(yy, dateadd(yy,1,o.orderdate)))
                              end
     group by c.companyid, o.orderdate, yearendingdate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 281k
  • Answers 281k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The IL is JIT'd (JIT = Just In Time) compiled… May 13, 2026 at 3:50 pm
  • Editorial Team
    Editorial Team added an answer You need to retain the value assigned to displayedMonthYear. There's… May 13, 2026 at 3:50 pm
  • Editorial Team
    Editorial Team added an answer In theory, you don't need a newer compiler, just an… May 13, 2026 at 3:50 pm

Related Questions

For simplicities sake, I'll make up a similar example to what I have: Let's
I've got the GOF sitting on my desk here and I know there must
In the past I've used the bind1st and bind2nd functions in order to do
I have the following method that is supposed to be a generic Save to
I've read several XSRF solutions that rely on adding more tokens to the response,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.