Link to home
Start Free TrialLog in
Avatar of ttist25
ttist25

asked on

MSSQL Frequency of Years From Days Field

Good morning!

I have a table with a field of elapsed days between two points.  I'm trying to do a frequency of rows converting days into years.  

For example, given the following data:

Days |
--------
200   |
--------
100   |
--------
860   |
--------
368   |
--------
479   |
--------

The result would look something like:
<1 year = 2
1 - 2 years = 2
2 - 3 years = 1

I want to group the frequencies as follows:
< 1 year
1 - 2 years
2 - 3 years
3 - 4 years
4 -5 years
> 5 years

I'm thinking some kind of case statement will get me there but I'm not sure how to combine it with a group by (if that's even possible).  

Thanks in advance for any help.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ttist25
ttist25

ASKER

Thanks Pawan!  

That worked great.  I modified the last line to read:
SUM(CASE WHEN days > 365*5 THEN 1 ELSE 0 END) ' > 5 Years'

But that's exactly what I needed.  

Thanks again!