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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:05:44+00:00 2026-06-08T09:05:44+00:00

Some serious brain power needed!! I have an issue where I am trying to

  • 0

Some serious brain power needed!! I have an issue where I am trying to calculate a price overcharge as days from two given date ranges. Basically I have an invoice line as below:

BillID     AccountID     BilledFrom        BillTo      Price
 34         3456         10/10/2012       10/12/2012    86p 

I then have a pricing matrix which can is:

AccountID      EffectiveFrom       EffectiveTo     Price
 3456          09/09/2009          10/11/2012      86p
 3456          11/11/2012          20/11/2012      56p
 3456          21/11/2012          10/12/2013      24p
 3456          11/12/2013           null           18p -- null never expires

Currently the initial price is taken where the BillFrom date is between EffectiveFrom and EffectiveTo dates and billed per days. Now I want to work out the overcharging from the bill as you can see that the BillTo date exceeds the EffectiveTo date for the 86p line. So there is a period of 1 month where the customer has been overcharged as line 2 or the pricing should kick in. I need a function either in SQL or C# which will outcome:

OverchargeID    type      BillID       Reason       Days     Price
    1          Credit      34       PriceChange      10        36p       (86p-56p)
    2          Credit      34       PriceChange      19        64p       (86p-24p)

I will be dealing with 56000 records Which could have up to 10 price ranges. I have the below code to work it server side but it is not practical at all and takes around 10 minutes. Any solutions or amendments to this code would be appreciated.

foreach (InvoiceOverchargePriceChange_SelectResult line in result) {

            //Get the invoiceLineRecord
            InvoiceLine invoiceLine = _dataContext.InvoiceLines.Where(m=>m.InvoiceLineID == line.InvoiceLineID).FirstOrDefault();


            // get the prices for each line
            List<Pricing_SelectResult> prices = ctrl.Select(line.AccountID, null, null, null, null)
                                                    .Where(m=>m.BillingMethodID == line.BillingMethodID)
                                                    .OrderByDescending(m=>m.EffectiveFrom).ToList();

            // if the price count is greater than 1 then need to check if overcharges occurred
            if (prices.Count > 1) {

                DateTime date = new DateTime();
                int days = 0;
                decimal charge = 0; ;
                for (int i = 0; i < prices.Count(); i++) {
                    days = 0;
                    charge = 0;

                    //if it goes in we found our price that we used
                    if (invoiceLine.BillFrom >= prices[i].EffectiveFrom && prices[i].EffectiveTo != null) {

                        date = invoiceLine.BillTo;
                        if (prices[i].EffectiveTo == null) break;
                        //check the Bill to date does not exceed the effective To date, if it does go in
                        while (date >= prices[i].EffectiveTo) {



                            if (date == invoiceLine.BillTo) {

                                //if its first go set the price
                                charge = prices.Where(m => date >= m.EffectiveFrom).FirstOrDefault().Price;


                            }

                            if (charge == prices.Where(m => date >= m.EffectiveFrom).FirstOrDefault().Price) {

                                //increment the days counter
                                days++;
                                date = date.AddDays(-1);

                            }
                            else {

                                //insert the days and price into db here...


                                //reset the days
                                days = 0;
                                charge = prices.Where(m => date >= m.EffectiveFrom).FirstOrDefault().Price;




                            }


                        }

                    }

                }

            }


        }

Thanks in advance

  • 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-06-08T09:05:45+00:00Added an answer on June 8, 2026 at 9:05 am

    Try this…

     select 
          ROW_NUMBER() over (order by BillID, StartDate),
          case when (billprice>pricingprice) then 'Credit' else 'Debit' end,
          BillID,
          DATEDIFF(d, startdate,enddate)+1 as days,
          billprice-pricingprice
     from
     (
          select 
               BillID, 
               case when Effectivefrom>BilledFrom then Effectivefrom else BilledFrom end startdate, 
               case when isnull(effectiveto,GETDATE())<BillTo 
                    then isnull(effectiveto,GETDATE()) else billto-1 end enddate,
               bill.price as billprice,
               pricing.price as pricingprice
          from pricing
         inner join bill
                    on bill.accountid= pricing.accountid 
                    and (billedfrom<isnull(effectiveto,getdate())) and (BillTo>Effectivefrom)
                    and bill.price <> pricing.price 
     ) v
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Everyone I m dealing with some serious issue in django-admin.I have created models
I have some serious issues...I need to build a project from another developer in
I am having some serious issue while loading Staging Table from Data Store. My
I am having some serious problem with some e-mail issue. in short: loading a
I have begun some serious reading on this framework and i am seeing where
I am having some serious issues trying to tweak my layout in a table.
Okay so like everything else IE8 is giving some serious problems. When trying to
I have been facing some serious issues while executing this particular JSP page. CODE
I'm trying to write a rewrite rule but I've some serious problems with forwarding
Good people of SO, Today I have some serious concerns on my business layer

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.