Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Expand data from a single record and range value

Author  Topic 

sumo
Starting Member

45 Posts

Posted - 2002-11-27 : 11:23:09
Is there an easy way to expand a single record that has 2 fields as a range into single records for each value in the range? Here's some sample data that I am working with:

Make    Model   BegYear EndYear SKU
ACURA CL 1998 2000 P4936267083
ACURA CL 1998 2000 P4941757083
ACURA CL 1998 2000 P4941767083


I'd like to take each of those records and expand them out into separate records for each year. For example, the first record above would expand into:

Make    Model   Year    SKU
ACURA CL 1998 P4936267083
ACURA CL 1999 P4936267083
ACURA CL 2000 P4936267083


I can only think of using cursors and iterating through the first query and inserting the data into a new table, but it seems that there'd be an easier, faster way through a more complex query.

Any help is appreciated!
TIA!

Michael Sumerano
Technical Web Administrator
eKeystone.com
http://www.ekeystone.com/

Edited by - sumo on 11/27/2002 11:24:33

nr
SQLTeam MVY

12543 Posts

Posted - 2002-11-27 : 11:49:12
Create a table with all the years in it and join to that.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-11-27 : 12:04:58
As NR mentioned, you need a table of Years that just list all years in your data range.

If you have that, then

SELECT
Make, Model, Year, SKU
FROM
Data
INNER JOIN
Years
ON
Years.Year BETWEEN BegYear AND EndYear

should do the trick.

- Jeff
Go to Top of Page

sumo
Starting Member

45 Posts

Posted - 2002-11-27 : 12:09:29
So simple, I feel stupid.

quote:

Create a table with all the years in it and join to that.



Michael Sumerano
Technical Web Administrator
eKeystone.com
http://www.ekeystone.com/
Go to Top of Page
   

- Advertisement -