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 |
mapradeepkumar
Starting Member
1 Post |
Posted - 2008-05-20 : 12:44:04
|
Hi I have a table ID RollNo FeeHead Amount1 1001 234 10002 1001 224 11003 1001 234 12004 1001 235 13005 1002 234 10006 1002 224 11007 1002 234 12008 1002 235 13009 1002 11 15010 1003 234 100011 1003 224 110012 1003 234 120013 1003 235 130014 1003 11 15015 1003 10 250Now i need total 7 rows per rollno irrespective of number of rows it has ie total 7 records(record count) should be there RollNo RecordCount 1001 4 1002 51003 6As 1001 has only 4 record count so i should be able to add another 3 records to make it 7the other columns can have null values.i can do this using a loop but i don't want to use a loop, can any one suggest a way to do this in sql2000 or sql2005 Thanks in Advance |
|
ssunny
Posting Yak Master
133 Posts |
Posted - 2008-05-20 : 13:50:52
|
Just write the insert statements.Or here's the shortcut if you don't want to copy/paste bunch of insert into's...Let's call the table as a.insert into a (ID,RollNo,FeeHead,Amount)select 5,1001,null,nullunion allselect 6,1001,null,nullunion allselect 7,1001,null,nullSunny. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-20 : 14:24:18
|
quote: Originally posted by mapradeepkumar Hi I have a table ID RollNo FeeHead Amount1 1001 234 10002 1001 224 11003 1001 234 12004 1001 235 13005 1002 234 10006 1002 224 11007 1002 234 12008 1002 235 13009 1002 11 15010 1003 234 100011 1003 224 110012 1003 234 120013 1003 235 130014 1003 11 15015 1003 10 250Now i need total 7 rows per rollno irrespective of number of rows it has ie total 7 records(record count) should be there RollNo RecordCount 1001 4 1002 51003 6As 1001 has only 4 record count so i should be able to add another 3 records to make it 7the other columns can have null values.i can do this using a loop but i don't want to use a loop, can any one suggest a way to do this in sql2000 or sql2005 Thanks in Advance
Insert into table (RollNo,FeeHead,Amount)SELECT t.RollNo,NULL,NULLFROM (SELECT RollNo,COUNT(*) AS Rows FROM table GROUP BY RollNo) tCROSS JOIN master..spt_values vWHERE v.type='p'AND v.number BETWEEN 1 AND 7AND v.number>t.Rows |
 |
|
|
|
|
|
|