Basically I have an invoice lines for a below account as below:
BillID AccountID BilledFrom BillTo Days Price
38 3456 10/10/2012 10/11/2012 30 86p
39 3456 11/11/2012 11/12/2012 30 87p
40 3456 12/12/2012 30/12/2012 18 81p
The user would like to refund the customer for a partial date Range, therefore the user will need to enter a date from and date to date:
Date From: 18/10/2012 DateTo: 14/12/2012
This will cause credit lines to refund the client working out the days and price for each line. I need to return each line and show the cut off points. As you can see the ranges entered span across the 3 invoice lines.
The result needed is:
BillID AccountID BilledFrom BillTo RangeStart RangeEnd Days Price
38 3456 10/10/2012 10/11/2012 18/10/2012 10/11/2012 22 86p
39 3456 11/11/2012 11/12/2012 11/11/2012 11/12/2012 30 87p
40 3456 12/12/2012 30/12/2012 11/11/2012 11/12/2012 2 81p
The results basically will bring back the initial applicable invoice line but work out how this range fits and calculates the days against the bill from and bill to from the date range entered.
I need a function either in SQL to provide this result. Any Help would be appreciated. Thank you in advance.
try this:
SQL Fiddle Demo