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 |
|
Freddie
Starting Member
29 Posts |
Posted - 2007-10-06 : 06:01:20
|
| Hello All:i am inserting a counter from a procedure based on the year/month.I would like to reset the counter to 1 every month.example of data:count year/month_______________16 20071117 20071118 20071119 20071120 20071121 200712 this counter should be reset to 122 20071223 200712MY PROCEDURE____________DECLARE @i INT SET @i = 1DECLARE @DateID INT, @YMN CHAR(6)DECLARE c CURSORFOR SELECT DateID,YMN FROM TEST WHERE WkDayIn = 'Yes' AND HolidIn = 'No'OPEN cFETCH NEXT FROM c INTO @DateID,@YMNWHILE @@FETCH_STATUS = 0 BEGIN UPDATE TEST SET BusDay = @i WHERE DateID = @DateID SET @i = @i + 1FETCH NEXT FROM c INTO @DateID,@YMNENDCLOSE cDEALLOCATE cThank YouF. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-06 : 06:16:50
|
| Where do you want to show data?If you use reports, group the report by year/month and use recordnumber and reset it for each groupIf you use SQL Server 2005, then make use of Row_number() functionMadhivananFailing to plan is Planning to fail |
 |
|
|
Freddie
Starting Member
29 Posts |
Posted - 2007-10-09 : 08:59:34
|
| I want to insert into a table. I am using sql server 2000. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-09 : 09:09:56
|
[code]DECLARE @Sample TABLE (Counter INT, YearMonth INT)INSERT @SampleSELECT 16, 200711 UNION ALLSELECT 17, 200711 UNION ALLSELECT 18, 200711 UNION ALLSELECT 19, 200711 UNION ALLSELECT 20, 200711 UNION ALLSELECT 21, 200712 UNION ALL-- this counter should be reset to 1SELECT 22, 200712 UNION ALLSELECT 23, 200712SELECT d.Counter - x.Counter + 1, d.YearMonthFROM @Sample AS dINNER JOIN ( SELECT YearMonth, MIN(Counter) AS Counter FROM @Sample GROUP BY YearMonth ) AS x ON x.YearMonth = d.YearMonth[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-10 : 01:54:03
|
orSelect (select count(*) from @Sample where counter<=t.counter and YearMonth=T.YearMonth) as counter, yearmonth from @sample as T MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|