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 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

Related Questions

I have a database with a table called 'connection', for simplicities' sake, let's say
I have 2 tables. For simplicities sake 'u' has the following columns userid divisionid
Let's say I have an array with multiple hashes [#<Campaign id: 144> , #<Campaign
I have a very large table (millions of records) containing approximately 8 fields as
Just looking for opinions on the following 2 scenarios. We have a table where
I have a MySQL query that looks like this: UPDATE `Table` SET `Column` =
I have a table from a legacy system which does not have a primary
I have developed an application in ASP.NET 3.5 which utilizes the Membership and Roles
I have some the folowing requirements: ...The document must be encoded in UTF-8 ...
So, I am using C on a unix system and have access to the

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.