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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:17:05+00:00 2026-05-27T09:17:05+00:00

I have an application that works out the total cost of usage … Usage

  • 0

I have an application that works out the total cost of usage …

Usage Table

terminal   calldate      callduration   tariff
1          2011-01-01    00:01:00       1
1          2011-01-01    00:03:30       1
1          2011-01-02    00:10:00       1
1          2011-01-05    01:00:00       1

Tariff Table

id     included      extraunit        extracost
1      00:05:00      00:01:00         10

Use the above tables I work out the total cost per terminal per month, first I convert all times to seconds and then use the following formula to calculate the cost

cost = ((totalduration - included) / extraunit ) * extracost
695  = ((4470          - 300)      / 60        ) * 10

if totalduration - included is negative the cost will be 0

I do the calculation in PHP. This all works fine – i can create an invoice per terminal and the costs are show per terminal per month.

Month        Terminal     Cost
2011-01      1            695

What I now need to do is add a column to the original usage table (i may use a separate table – but keeping it simple for this question) to record the cost of each record within the usage table.

So – I know the total cost per month and can query the usage table to get all the records that make up that cost – what I cannot figure out is how I allocate a cost while taking the included time into account. If there was no included time I would divide the total cost by the total duration and then work out how much each record costs …. I can do that by working out the cost per second 695 / 4170 = 0.1666666667 – I then work out the cost per record. This is the output I am trying to achieve.

terminal   calldate      callduration   tariff      cost
1          2011-01-01    00:01:00       1           0.00
1          2011-01-01    00:03:30       1           0.00
1          2011-01-02    00:10:00       1          95.00 (rounded to 2 decimal places)
1          2011-01-05    01:00:00       1         600.00 (rounded to 2 decimal places)

Line 1 user 60 seconds from the included minutes, line 2 uses 210 seconds, and line 3 uses 30 seconds of the 5 minutes included – the remainder of line 3 (570 seconds) is calculated.

Can someone please point me in the right direction – I don’t think this is going to be possible to do purely in MySQL … but it would be very nice if I could at least get only the records that need a cost applying – ie ignoring the included records.

The usage table contains 80 million records per month (there are 800,000 terminals) – so it needs to scale quite well …

Update

The process for creating the total cost per terminal each month takes approx 3 hours – to keep this time down I group the call durations using – sum(time_to_sec(callduration)) this allows me to get the total cost per month per terminal. This cost is the number one importance and should be available as soon as possible – the cost per usage is not as urgent and is required “some time” after – this is why i can work it out using a separate process – maybe its better to revisit that original plan ? and maybe work out the cost per usage line and then total these up ?

Update 2

Added some expected output and some more detail on how the cost per record is calculated – I have all of the calculations – thats the straight forward part – the difficulty is working out what is included and what is not …

Any help / advice is appreciated.

  • 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-27T09:17:06+00:00Added an answer on May 27, 2026 at 9:17 am

    The cost per usage is going to be hard to calculate and to get it to sum and match the overall cost if the overall cost is calculated on an aggregate basis.
    For example

    terminal   calldate      callduration   tariff
    1          2011-01-01    00:01:00       1 - uses 1m of the included time
    1          2011-01-01    00:03:30       1 - uses 3m30 of the included time
    1          2011-01-02    00:10:00       1
    

    What happens here? Does this include 30s of included time? If so how is the other 9m30s broken up given your units are 1m? is it calculated pro-rata?

    In terms of the monthly summary I would maybe build it up day by day per terminal so you would get something like this

    terminal   calldate      included   extra_duration   cost
    1          2011-01-01    00:04:30      00:00:00        0
    1          2011-01-02    00:05:00      00:09:30       90
    1          2011-01-05    00:05:00      00:10:30      100
    

    Then each day you can sum up the time allocated per terminal the day before and add a new line with the updated calculation.

    EDIT
    I believe you can do this in pure sql. This should get you started:

    SET @cumul = 0;
    SELECT id,(@cumul:= @cumul + TIME_TO_SEC( duration ) ) FROM `usage`;
    

    From that, you should either be able to add a cumulative duration column and work from that or you can probably stitch it into an UPDATE and just log the actual cost per usage using a CASE statement.

    EDIT
    Ok, given your comments I think this works if you want to do it in pure SQL. It requires 2 extra columns (cumul_duration and cost).

    SET @cumul = 0;
    UPDATE `usage` set cumul_duration=(@cumul:= @cumul + TIME_TO_SEC( duration ));
    update `usage` set cost=(
    SELECT
        CASE WHEN cumul_duration<=300
        THEN 0
        ELSE (
            CASE WHEN (cumul_duration-TIME_TO_SEC(duration)>=300)
            THEN (TIME_TO_SEC(duration)*10/60)
            ELSE ((cumul_duration-300)*10/60) END
    ) END
    )
    

    I haven’t worked out how to do it all in one query and I haven’t tested it against more than one tariff but it works on your sample data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application that works in our stage/test environment fine but once
I have an application that perfectly works on iPhone os 2.2.1 but when I
We have a C# application that works fine on windows XP and windows vista
I have an application that is works fine and the JFrame for it is
I have a C++ application that works and compile perfectly. I can execute and
I have a Rails application that works with mongodb. I want to deploy it
I have a web application project that works with a quite large database (over
I have an application on appspot that works fine through regular browser, however when
I have to write a C# application that works with a SQL server database
I have an Asp.Net MVC application that works in the vs.net development web server.

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.