SQL Query - Historical Data

I have a SQL table with historical data on users of a specific system. I need to report on how one field has changed over time. Here's an example of the data:


User      Type      Effective Date
User 1      A      1/1/2010
User 1      B      5/1/2010
User 1      A      7/1/2010
User 2      B      1/1/2010
User 2      A      2/1/2010


The data basically means that User 1 was type A from 1/1 - 4/30, type B from 5/1 - 6/30 and back to type A from 7/1 through today.

How can I write a query that expands this data so I can chart the change of user types over time?
bezellviAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
what chart type are you considering? and what is it's data requirements?

i.e. what is the "expected result" please - that's your job.

while you think about that could you also tell us what version of SQL Server you are using please (it could make a difference to the solution options)
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ordering it by User and EffectiveDate but I don't know what's the output that you want. Can you post an example of the pretended output?

SELECT User, EffectiveDate, Type
FROM YourTableName
ORDER BY User, EffectiveDate

Open in new window

0
bezellviAuthor Commented:
I am looking to create a stacked bar chart with the date in the x axis and the number of users in the y axis, colored by user type. I somehow need to create a query that lets me count the number of users in each category at any given date in time.
0
John_VidmarCommented:
WITH CTE
AS
(	SELECT	*
	,	rn = ROW_NUMBER() OVER (PARTITION BY [User] ORDER BY [Effective Date])
	FROM	SomeTable
)
SELECT	a.[User]
,	[EffectiveFrom]	=	a.[Effective Date]
,	[EffectiveTo]	=	ISNULL(b.[Effective Date], current_timestamp)
,	a.[Type]
FROM	CTE	a
LEFT
JOIN	CTE	b	ON	a.[User] = b.[User]
			AND	a.rn = b.rn - 1
ORDER
BY	a.[User]
,	a.[Effective Date]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.