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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:44:54+00:00 2026-05-27T16:44:54+00:00

This one is quite tricky i’ve been scratching my head all day. I have

  • 0

This one is quite tricky i’ve been scratching my head all day.

I have a table containing billing periods

ID     | DATEBEGIN | DATEEND  | CUSTOMER_ID
=======+===========+==========+=================
1      | 1/1/2011  | 30/1/2011 | 1

I have a table containing ‘sub customers’

ID     | NAME    
=======+===========
1      | JOHN
2      | JACK
3      | Elwood

I have a table containing items purchased on a subscription (wholesale account

ID     | DATE      | SUBCUSTOMER_ID | CUSTOMER ID
=======+===========+================+==============
1      | 15/1/2011 | 1              | 1
2      | 18/1/2011 | 1              | 1
3      | 25/1/2011 | 2              | 1
4      | 28/1/2011 | 3              | 1

So I want to count ‘credits’ to deduct from their account. So the subscription is per ‘sub customer’.

So at the end of the billing period (30/1/2011 from first table). I need to count the distinct sub customers (there are 3). They are charged pro-rata from the first purchase they make during the billing period.

Days Having Subscription  |  SUBCUSTOMER_ID   |  Pro Rata Rate   | CUSTOMER_ID
==========================+===================+==================+==============
3                         |  3                | 3/30             | 1
6                         |  2                | 6/30             | 1
16                        |  1                | 16/30            | 1

The output should therefore be

CUSTOMER_ID | BILLING CREDITS
============+========================
1           | 25/30

I have to count it pro rata, previously it would be unfair to bill a full period even if they purchase an item 1 day prior to the billing date

  • 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-27T16:44:54+00:00Added an answer on May 27, 2026 at 4:44 pm
    SELECT  customerId, SUM(DATEDIFF(dateend, fp) + 1) / (DATEDIFF(dateend, datestart) + 1)
    FROM    (
            SELECT  b.*, MIN(date) AS fp
            FROM    billing b
            JOIN    purchase p
            ON      p.customerId = b.customerId
                    AND p.date >= b.datebegin
                    AND p.date < b.dateend + INTERVAL 1 DAY
            GROUP BY
                    b.id, p.subcustomerId
            ) q
    GROUP BY
            customerId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't quite wrap my head around this one... A table like the one
Not quite sure how to get this one. I have a staff table and
This one is a tricky one I have been looking through the event viewer
This one's been tricky to quantify, so I may not have this question worded
The title for this one was quite tricky. I'm trying to solve a scenario,
This one is quite obviously a function pointer : typedef int (* foobar) (int
This one seems to be quite ridiculous but how can I open a VS
I haven't quite worked this one out yet - how do I set DEBUG
Done quite a lot of searching around this one and so far I've only
I've seen a few similar questions to this one, but none that were quite

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.