SQL 2005 :
I am working on set of SQL queries which generate a formula.
This Mathematical calculation, Example : (3000 -30)/1477*100 = 201.1 % has to be generated from set of Tables.
Value : 3000 is generated from this query ..
select sum(DaysSupply) as [Total Days Supply] from vOeOrderWide where patcustid = 4797
Output is [Total Days Supply]
Value : 30 is generated from this query ..
select top 1 DaysSupply from VoeOrderwide where patcustid = 4797 order by datefilled desc
Output is [DaysSupply]
Value : 1477 is generated from these set of queries ...
declare @d1 datetime;
declare @d2 datetime;
set @d1= (select top 1 DateFilled from vOeOrderWide where patcustid = 4797
order by DateFilled asc)
set @d2= (select top 1 DateFilled from vOeOrderWide where patcustid = 4797 order by DateFilled DESC )
select DATEDIFF(d,@d1,@d2) as [Days Between]
Output is [Days Between]
I want to combine all these queries and generate the formula as ..
[Total Days Supply] - [DaysSupply] / [Days Between] * 100
Hmm, I think I’d try something more along these lines:
Please note that I do not have an SQL server instance to test this against.