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
A few comments on my answer.
First, I wasn’t not sure where the
type_of_connectionflag is present in the SQL Fiddle that you posted so I added it toConsumers.Second, I think you need to alter your
price_list2table to include thelimitstart and end values for the prices. Otherwise it will be very difficult to determine the price for each consumer.I used the following
price_list2table which will contain the start/end values for each limit:On to the query, using the tables and the original query that you posted you should be able to use something like this:
See SQL Fiddle with Demo
As you can see when joining on the
price_list2table I am joining on the start/end range of the limits. This allows you to determine what price should be used for the bill.