?
Solved

MSSQL Frequency of Years From Days Field

Posted on 2016-11-29
2
Medium Priority
?
52 Views
Last Modified: 2016-11-29
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.
0
Comment
Question by:ttist25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41905758
try..


--


CREATE TABLE grouper
(
	Days INT
)
GO

INSERT INTO grouper VALUES (200),(100),(860),(368),(479),(6000),(88000)
GO

SELECT      SUM(CASE WHEN days <= 365 THEN 1 ELSE 0 END) '<1Year'		
		 ,	SUM(CASE WHEN days > 365 AND days <= 365*2 THEN 1 ELSE 0 END) '1 - 2 Year'		
		 ,	SUM(CASE WHEN days > 365*2 AND days <= 365*3 THEN 1 ELSE 0 END) '2 - 3 Year'		
		 ,	SUM(CASE WHEN days > 365*3 AND days <= 365*4 THEN 1 ELSE 0 END) '3 - 4 Year'		
		 ,	SUM(CASE WHEN days > 365*4 AND days <= 365*5 THEN 1 ELSE 0 END) '4 - 5 Year'	
		 ,  SUM(CASE WHEN days <= 365 THEN 1 ELSE 0 END) ' > 5Years'			 
FROM grouper

--

Open in new window



Output

<1Year      1 - 2 Year  2 - 3 Year  3 - 4 Year  4 - 5 Year   > 5Years
----------- ----------- ----------- ----------- ----------- -----------
2           2           1           0           0           2

(1 row(s) affected)

Open in new window


Hope it helps !!
0
 
LVL 1

Author Closing Comment

by:ttist25
ID: 41905850
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!
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question