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 |
|
kickone
Starting Member
1 Post |
Posted - 2002-09-24 : 05:11:13
|
| ID MONTH TRAF---------------1 1 1501 2 101 3 02 1 102 2 103 1 0I want to create a stored procedure which will create for each <ID> 12 month, without duplicates.I want something like this.ID MONTH TRAF---------------1 1 1501 2 101 3 01 4 01 5 01 6 01 7 01 8 01 9 01 10 01 11 01 12 02 1 102 2 102 3 02 4 02 5 02 6 02 7 02 8 02 9 02 10 02 11 02 12 03 1 103 2 103 3 03 4 03 5 03 6 03 7 03 8 03 9 03 10 03 11 03 12 0I was tryed in different ways, but i get duplicates for the existent months i don't know how to insert only the months which are not in table.My code was: insert into traf (id, month. traf)select id,1,0from traficunion select id,2,0from traficunion select id,3,0from traficunion select id,4,0from traficunion select id,5,0from traficunion select id,6,0from traficunion select id,7,0from traficunion select id,8,0from traficunion select id,9,0from traficunion select id,10,0from traficunion select id,11,0from traficunion select id,12,0from traficunion Can somebody help me with some ideas or some code samples?Thanks |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-09-24 : 05:48:12
|
| see http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=20092 to get a sequence of numbersthen something likeselect b.ID, Month = seq, coalesce(tbl.Traf, 0)from (seq = select 1 union select 2 union ... union select 12) as ajoin (select distinct ID from tbl) as bleft outer join tbl on tbl.month = a.seq and tbl.ID = b.ID==========================================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. |
 |
|
|
|
|
|
|
|