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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:13:57+00:00 2026-06-18T10:13:57+00:00

I have a table which stores monthly billing information. CREATE TABLE [dbo].[billing_history]( [id] [numeric](18,

  • 0

I have a table which stores monthly billing information.

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

The consumer_id is a foreign key referencing the consumer details table.

What i want is to subtract every customer current reading from the reading of the previous month. This would generate the current bill. 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-18T10:13:59+00:00Added an answer on June 18, 2026 at 10:13 am

    You could use something similar to this, where you would replace the values of the month/year that you want to return:

    select b1.consumer_id,
      sum(b1.reading - isnull(b2.reading, 0)) Total
    from billing_history b1
    left join billing_history b2
      on b1.consumer_id = b2.consumer_id
      and month(b2.reading_date) =12
      and year(b2.reading_date) = 2012
    where month(b1.reading_date) = 1
      and year(b1.reading_date) = 2013
    group by b1.consumer_id;
    

    See SQL Fiddle with Demo.

    If you don’t want to pass in the values of the month and year to search and you only want the current/previous month, then you could use something similar to this using a CTE:

    ;with cur as
    (
      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())
    )
    select c.consumer_id, 
      sum(c.reading - isnull(pre.reading, 0)) TotalReading
    from cur 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
    

    See SQL Fiddle with Demo

    This version gets both the current/previous month and year values to be used. If you are not familiar with CTE syntax, this can also be written as:

    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;
    

    See SQL Fiddle with Demo.

    As you can see in my queries I used the aggregate function SUM() and a GROUP BY on the consumer_id. I did this in the event you had more than one reading per customer. If you know you will only have one reading per month, then you could remove the aggregate.

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

Sidebar

Related Questions

I have a table which stores all item information and its id. Now I
I have a table which stores users, another table which stores post information and
I have a table which stores information of a lot of twitter tweets including
Hi I have table called Sold Products which stores buying information for product &
I have a table which stores the data related to posts in this format
I have a table which stores test results like this: user | score |
Imagine I have a table which stores a series of sparse vectors. A sparse
(I am using PostgreSQL) I have a table which stores transactions to an account.
While working with ActiveRecord I have a table which stores a serialized array of
I have a MYSQL-Table which stores scores. Every time a user improves a new

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.