I need help to create a query in sql 2005.
I have 4 dates: 1 standard startdate and 3 date to compare.
I need to count the datediffs in days in ranges of 10.
datediff(day, startdate, enddate1)
How many times the range from 1 till 10, from 11 till 20 and from 21 till 30 will appear.
This for every 3 enddates!
Then a sum of all (3 enddates) 1 till 10 aso ( 510 times < 11, 275 times > 10 and < 21)
After this calculating the percentage of the ranges by all enddates.
Like 510/785 = XX.XX% and 275/785 = XX.XX%
Does I make myself clear?
Thanks in advance
1SeoAdvies
@mitch-wheat, It’s not a poor question it’s a difficult question to explain!
There is no table to explain this. Nearly Every table has a datatimefield!
I will try it again to explain this in very simple english.
I have a table with a datetime field. In a excelsheet I have an other datetime.
Both have the same key.
I know how to calculate the datediff in days.
I want to know how many time the datediff between 1 and 10 will appear.
example:
datediff(day, DateTime1, DateTime2) = 2
datediff(day, DateTime1, DateTime2) = 3
datediff(day, DateTime1, DateTime2) = 11
datediff(day, DateTime1, DateTime2) = 2
datediff(day, DateTime1, DateTime2) = 3
datediff(day, DateTime1, DateTime2) = 11
datediff(day, DateTime1, DateTime2) = 7
datediff(day, DateTime1, DateTime2) = 6
The result is
< 11 = ***6*** (smaller then 11)
10 and <21 = 2 (between 10 and 21)
Sum the results 6 + 2 = 8
Calculate percentage:
smaller then 11 = 6\8 = 75%
Greater then 11 + <21 = 2\8 = 25%
The bold /invers items are the values I need!
Does I make myself now clear????????
Thanks in advance
SQL Fiddle
MS SQL Server 2008 Schema Setup:
Query 1:
Results: