We help IT Professionals succeed at work.

SQL Query - Historical Data

Last Modified: 2014-10-27
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?
Watch Question

PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

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)
Vitor Montalv√£oIT Engineer
Distinguished Expert 2017

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


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.
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.