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.
Author |
Topic |
dewacorp.alliances
452 Posts |
Posted - 2006-09-06 : 18:58:05
|
Hi thereCould someone help regarding this. I got one table called tblA which have records like this:AccNo; Mth; AmtXXX; 1; 100XXX; 3; 150YYY; 6; 100YYY; 7; 150...I want to fill the gap of each AccNo where the Mth is missing (1 - 12). So the complete record will be:AccNo; Mth; AmtXXX; 1; 100XXX; 2; 0XXX; 3; 150XXX; 4; 0XXX; 5; 0XXX; 6; 0XXX; 7; 0XXX; 8; 0XXX; 9; 0XXX; 10; 0XXX; 11; 0XXX; 12; 0...I'm thinking to do a scan which is missing and put into temp table and put it back in. But I think there will efficient way to do this.Thanks |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-06 : 19:45:09
|
[code]select c.AccNo, c.Mth, isnull(a.Amt, 0) as Amtfrom( select AccNo, Mth from AccNoTable cross join ( select 1 as Mth union all select 2 union all select 3 union all select 4 . . . ) m) cleft jointblA aon c.AccNo = a.AccNo and c.Mth = a.Mth[/code] KH |
 |
|
dewacorp.alliances
452 Posts |
Posted - 2006-09-06 : 20:05:38
|
Thanks Khtan.That's the "select 1 as Mth union all select 2 union all select 3 union all select 4 . . ." ... I can probably replace this with a table that we have which containing month record between 1 and 12 ... can't I? |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-06 : 22:09:07
|
quote: Originally posted by valdyv Thanks Khtan.That's the "select 1 as Mth union all select 2 union all select 3 union all select 4 . . ." ... I can probably replace this with a table that we have which containing month record between 1 and 12 ... can't I?
Sure why not. KH |
 |
|
dewacorp.alliances
452 Posts |
Posted - 2006-09-08 : 00:27:13
|
Hi khtanWith script that you give me, how to make it that only insert the record has a gap on it. Cause at the moment I have a complete list and left outer join to it and get the one that is not on the list and then insert. Which I think this query is overhead.Thanks |
 |
|
|
|
|