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
rate_listand thebilling_historytable?limitvalue in theprice_listtable the range values? So it is0-50will have a price of 4 and51-100has10. If so, what is over 100?