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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:04:44+00:00 2026-05-23T14:04:44+00:00

The context of this problem is a billing system for a warehouse that stores

  • 0

The context of this problem is a billing system for a warehouse that stores widgets. The storage fee is based on the size of the widget (e.g $x * height * length * width). This part is easy. I use the following SQL to calculate Amount Due and aggregate by user id.

Select UserID, Sum((Height * Length * Width * 5) as AmtDue From Widget
Where StatusID=2
Group By UserID

5 is an arbitrary value for the moment. Eventually I’ll pass it as a parameter or get it from a table in the database. Assume it’s always 5 for the moment.

Here’s the part I’m struggling with. Fees are calculated at the end of the month and are prorated according to the number of days in that month the widget was stored in the warehouse (e.g. if the widget sits in the warehouse for 15 days out of a 30 day month the fee due is 50% of the amount). In my Widget table I have a column for DateReceived and DateShipped. DateReceived always has a datetime value. DateShipped is sometimes null (e.g. if the widget is still in storage). I have parameters available for CycleStart and CycleEnd which represent the start and end of the relevant billing month. I’m trying to modify my SQL query to include in the select expression a prorate factor that is equal to (# of days in storage / # of days in month). In other words AmtDue becomes (height * length * width * 5 * prorate factor) How do I do this?

Here’s an example to help illustrate how the prorate factor is calculated.

If @CycleStart = 7/1/11 and @CycleEnd = 7/31/11 mm/dd/yy

DateReceived  |  DateShipped  |  Prorate Factor

6/5/11              Null           100%
6/5/11              7/15/11      48.38%
7/30/11             8/5/11        6.45%
7/15/11             7/25/11      35.48%        

I’m really struggling with the conditional nature of this and the fact that we’re dealing with datetime values.

  • 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-23T14:04:44+00:00Added an answer on May 23, 2026 at 2:04 pm

    Here you go. This

    drop table #work
    go
    create table #work
    (
      id           int      not null identity(1,1) primary key clustered ,
      DateReceived datetime not null ,
      DateShipped  datetime     null ,
    )
    go
    
    insert #work ( DateReceived , DateShipped ) values ( '06/05/2011' , null         )
    insert #work ( DateReceived , DateShipped ) values ( '06/05/2011' , '07/15/2011' )
    insert #work ( DateReceived , DateShipped ) values ( '07/30/2011' , '08/05/2011' )
    insert #work ( DateReceived , DateShipped ) values ( '07/15/2011' , '07/25/2011' )
    go
    
    declare
      @CycleStart   datetime ,
      @CycleEnd     datetime ,
      @DaysInPeriod money
    
    set @CycleStart = '07/01/2011'
    set @CycleEnd   = '07/31/2011'
    set @DaysInPeriod = datediff(day,@CycleStart,dateadd(day,1,@CycleEnd))
    
    select * ,
           DaysInStorage = datediff( day ,
                             case when DateReceived < @CycleStart
                               then @CycleStart
                               else DateReceived
                             end ,
                             case when DateShipped > @CycleEnd
                               then dateadd(day,1,@CycleEnd)
                               else dateadd(day,1,coalesce(DateShipped,@CycleEnd))
                             end
                             ) ,
           DaysInPeriod  = @DaysInPeriod ,
           ProrateFactor = datediff( day ,
                             case when DateReceived < @CycleStart
                               then @CycleStart
                               else DateReceived
                             end ,
                             case when DateShipped > @CycleEnd
                               then dateadd(day,1,@CycleEnd)
                               else dateadd(day,1,coalesce(DateShipped,@CycleEnd))
                             end
                             ) / @DaysInPeriod
    from #work
    

    Yields the following

    id DateReceived            DateShipped             DaysInStorage DaysInPeriod ProrateFactor
    -- ----------------------- ----------------------- ------------- ------------ -------------
     1 2011-06-05 00:00:00.000 NULL                               30        31.00        1.00
     2 2011-06-05 00:00:00.000 2011-07-15 00:00:00.000            14        31.00        0.4838
     3 2011-07-30 00:00:00.000 2011-08-05 00:00:00.000             1        31.00        0.0645
     4 2011-07-15 00:00:00.000 2011-07-25 00:00:00.000            10        31.00        0.3548
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem where all URLs like context/path/file/anything/that/follows becomes context/path/file.php/anything/that/follows or context/path/file.xml/anything/that/follows .
For context - read this . Problem: class Program { static void Main() {
In the context of this question link text is possible from a Controller that
This is in the context of Automatic Differentiation - what would such a system
Firstly - Z is Up in this problem. Context: Top down 2D Game using
The main problem is that I recieve the following message: base {System.SystemException} = {Unable
This question is in context with the problem mentioned in this thread . I'm
I have this problem that I cannot fix. From my @Controller , i can
I'm having this problem for a while now. I tried so many things that
Historical Context: This problem ended up being not at all what I thought it

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.