Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping Year Ranges that have gaps using C# or SQL

Status
Not open for further replies.

wbochar

IS-IT--Management
Mar 14, 2003
72
US
I'm working with a lot of year ranges and in the end they need to be "StartYear - EndYear".

So the following would data..

Sandra 1972
Sandra 1973
Sandra 1974
Sandra 1975
Sandra 1976
Johnny 2000
Johnny 2001
Johnny 2002
Johnny 2003

would in the end be:

Sandra 1972-1976
Johnny 2000-2003

I've got this kind of thing working no problem using SQL, grouping and min/max.. but when this happens:

Sandra 1972
Sandra 1973
Sandra 1975
Sandra 1976
Sandra 1977

Grouping them together and getting the max and the min year would be wrong as there is no 1974.

it should output:

Sandra 1972-1973
Sandra 1975-1977

Whats the smartest way of handling this? order the data by year subtract the lowest from the highest year and compare the result with the rowcount?

records returned: 5
Sandra 1972
Sandra 1973
Sandra 1975
Sandra 1976
Sandra 1977
Highest year : 1977
Lowest year : 1972




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top