| Author |
Topic |
|
master640
Starting Member
6 Posts |
Posted - 2008-04-04 : 09:20:49
|
| OKAY I have one table "Data" with about 7 million records. Now I want to create the following output table. THe range (1 to 15) can always change in the "data" table. How would I write a script to do this? Data Start End Time1 15 5:4520 26 2:26152 160 7:52Output Value Time1 5:452 5:453 5:454 5:455 5:456 5:457 5:458 5:459 5:4510 5:4511 5:4512 5:4513 5:4514 5:4515 5:4520 2:2621 2:2622 2:2623 2:2624 2:2625 2:2626 2:26152 7:52153 7:52154 7:52155 7:52156 7:52157 7:52158 7:52159 7:52160 7:52 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-04-04 : 09:26:00
|
| [code]Select Min(Value) as Start, Max(Value) as End, Timefrom TableGroup by Time[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-04 : 09:56:49
|
quote: Originally posted by harsh_athalye
Select Min(Value) as Start, Max(Value) as End, Timefrom TableGroup by Time Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
i read it as the other way round. they have the range and need to create a record for every 'number' in betweenEm |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-04-04 : 09:59:07
|
Oh, my bad! I thought it otherwise. Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
master640
Starting Member
6 Posts |
Posted - 2008-04-04 : 10:07:48
|
| elancaster is correct |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-04-04 : 10:09:57
|
Try this:declare @t table( Start int, [End] int, [Time] datetime)insert @tselect 1, 15, '05:45' union allselect 20, 26, '02:26' union allselect 152, 160, '07:52' select distinct sv.number as value, t.[Time] from @t t join master..spt_values sv on sv.number between t.Start and t.[End] and sv.number > 0 Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-04 : 10:17:35
|
| [code]create table #test (st int, en int, tim datetime)insert into #testselect 1,15,'05:45' union allselect 20,26,'02:26';WITH test (st,en, tim, Level)AS(select st,en,tim,level = stfrom #testUNION ALLselect t.st,t.en,t.tim, level + 1 from #test t join test on test.tim = t.timwhere level between t.st and (t.en - 1))select * from test[/code]i think it works...?Em |
 |
|
|
master640
Starting Member
6 Posts |
Posted - 2008-04-04 : 10:18:30
|
| But there is about 7 million of these records and ranges, so I need a more automated query |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-04 : 10:25:47
|
| what do you mean by more automted?Em |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-04-04 : 10:28:16
|
| Creation of table and insert part is just to mimic sample data. All you need is the final query in your case.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
|