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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:17:04+00:00 2026-06-18T12:17:04+00:00

I have got two tables for a Microhydel Application, one consists the monthly billing

  • 0

I have got two tables for a Microhydel Application, one consists the monthly billing record of Consumers from which monthly customer Bill is generated.

CREATE TABLE billing_history(
    [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [reading_date] [date] NOT NULL,
    [reading_year] [smallint] NULL,
    [reading] [numeric](18, 0) NOT NULL,
    [consumer_id] [int] NOT NULL,
    [paid_amount] [numeric](18, 0) NULL)

I have another table which stores the different slab for per unit cost for both commercial and domestic users.

CREATE TABLE [rate_list](
    [flag] [varchar](50) NULL,
    [limit] [numeric](18, 0) NOT NULL,
    [price] [numeric](18, 0) NOT NULL,
    [service_charges] [numeric](18, 0) NOT NULL,
    [surcharge] [numeric](18, 0) NOT NULL
) 

For e.g For a Domestic customer consuming 50 units or less electricity monthly will be charged differently then a Commercial customer consuming the same amount of electricity. Similarly consuming units over this slab will have another rate applied on them.

Thanks to @bluefeet i already have the query to generate the numbers of units consumed from the first table using the query

select c.consumer_id, 
  sum(c.reading - isnull(pre.reading, 0)) TotalReading
from
(
  select consumer_id,
    reading,
    month(getdate()) curMonth,
    year(getdate()) curYear,
    case when month(getdate()) = 1 then 12 else month(getdate()) -1 end preMonth,
    case when month(getdate()) = 1 then year(getdate())-1 else year(getdate()) end preYear
  from billing_history
  where month(reading_date) = month(getdate())
    and year(reading_date) = year(getdate())
) c
left join billing_history pre
  on c.consumer_id = pre.consumer_id
  and month(pre.reading_date) = c.preMonth
  and year(pre.reading_date) = c.preYear
group by c.consumer_id;

However I need to generate Monthly bill for each customer so that for e.g according to the rates in the rate_list table. The Key here is DOMESTIC/COMMERCIAL which has different slabs for the number of units consumed.

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-06-18T12:17:05+00:00Added an answer on June 18, 2026 at 12:17 pm

    A few comments on my answer.

    First, I wasn’t not sure where the type_of_connection flag is present in the SQL Fiddle that you posted so I added it to Consumers.

    Second, I think you need to alter your price_list2 table to include the limit start and end values for the prices. Otherwise it will be very difficult to determine the price for each consumer.

    I used the following price_list2 table which will contain the start/end values for each limit:

    CREATE TABLE [price_list2](
        [flag] [varchar](50) NULL,
        [limitStart] [numeric](18, 0) NOT NULL,
        [limitEnd] [numeric](18, 0) NOT NULL,
        [price] [numeric](18, 0) NOT NULL,
        [service_charges] [numeric](18, 0) NOT NULL,
        [surcharge] [numeric](18, 0) NOT NULL);
    

    On to the query, using the tables and the original query that you posted you should be able to use something like this:

    select c.consumer_id,
      r.totalreading * p.price TotalBill
    from Consumers c
    inner join
    (
      select c.consumer_id, 
        sum(c.reading - isnull(pre.reading, 0)) TotalReading
      from
      (
        select consumer_id,
          reading,
          month(getdate()) curMonth,
          year(getdate()) curYear,
          case when month(getdate()) = 1 then 12 else month(getdate()) -1 end preMonth,
          case when month(getdate()) = 1 then year(getdate())-1 else year(getdate()) end preYear
        from billing_history
        where month(reading_date) = month(getdate())
          and year(reading_date) = year(getdate())
      ) c
      left join billing_history pre
        on c.consumer_id = pre.consumer_id
        and month(pre.reading_date) = c.preMonth
        and year(pre.reading_date) = c.preYear
      group by c.consumer_id
    ) r
      on c.consumer_id = r.consumer_id
    inner join price_list2 p
      on c.type_of_connection = p.flag
      and r.totalreading between p.limitStart and p.limitEnd
    

    See SQL Fiddle with Demo

    As you can see when joining on the price_list2 table I am joining on the start/end range of the limits. This allows you to determine what price should be used for the bill.

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

Sidebar

Related Questions

I have got two tables. One is a User table with a primary key
I have got a datatable with records from two different database tables. Each of
Let's say I've got two tables: one with customer data, one with location data.
I've got two tables (articles and tags) that have a one-to-many relationship. I remember
I've got two tables. One is users, which has fields uid and name. The
I have got two tables: Event +----------+---------+--------------+ | event_id | name | date |
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I have got two input fields which i would like to make editable with
I've got two long lists A and B which have the same length but
I have got two tables in SQL Server 2005: USER Table: information about user

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.