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 |
|
Heinz23
Yak Posting Veteran
84 Posts |
Posted - 2008-09-04 : 19:31:48
|
| Hi all,I need something similar like this: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=100581 The main difference: After counting I want to delete the single entries.Date................................ Value2008-04-01 14:48:15.327 ... A2008-04-01 13:53:41.923 ... A2008-04-02 15:16:25.327 ... B2008-04-02 16:31:07.383 ... C2008-04-02 16:25:07.383 ... A2008-04-02 16:28:07.383 ... A2008-04-03 16:50:34.217 ... B2008-04-03 13:23:30.767 ... C2008-04-04 13:41:41.627 ... D2008-04-04 13:55:00.497 ... EI don't care about 'Value', this column could be ignored. I need a script I start once a day. It should count all records for all previous days, day by day, and write the total amount for each day into another SQL table. After that these processed rows should be deleted from the table. So in above sample let's say today is 2008-04-04 and I start the SP. After that the table should look like this:2008-04-04 13:41:41.627 ... D2008-04-04 13:55:00.497 ... EAnd the Summary table should look like this:Date........... Count2008-04-01 ... 22008-04-02 ... 42008-04-03 ... 2Or would it be better to do the counting already while writing the single rows? Currently I just add new records to the first table without checking anything. It might be possible to modify the 'AddRecord'-SP so instead of writing lots of single lines for each day I could already compute the total directly. I've just rejected this idea as this might decrease the performance?Many thanks! |
|
|
preethi
Starting Member
11 Posts |
Posted - 2008-09-04 : 23:06:38
|
I will go for a solution like this:DECLARE @Today datetimeSET @Today = DATEADD(day, 0, DATEDIFF(day, 0, GETDATE()))INSERT INTO <DailySummary> ([datecolumn],[CountValue])SELECT DATEADD(day, 0, DATEDIFF(day, 0, [datecolumn])), COUNT(*)FROM <Table>WHERE [datecolumn]< @TodayGROUP BY DATEADD(day, 0, DATEDIFF(day, 0, [datecolumn]))DELETE <Table> WHERE [datecolumn] < @Today I will add some error handling code to make sure that I do not delete if Insert fails.For your second question, it depends on the usage. If the server is quite busy most of the time, I would suggest that you should simply dump data (like what you did) and do the rollup during off peak hours. If you do not have time for large processing, but your server has some time for processing all the time, I will go with Instant summary approach.Hope this Helps!Cheers,Preethiviraj KulasinghamMCITP: DBA |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-09-04 : 23:10:06
|
[code]DECLARE @table1 TABLE( [Date] datetime, Value CHAR(1))INSERT INTO @table1SELECT '2008-04-01 14:48:15.327', 'A' UNION ALLSELECT '2008-04-01 13:53:41.923', 'A' UNION ALLSELECT '2008-04-02 15:16:25.327', 'B' UNION ALLSELECT '2008-04-02 16:31:07.383', 'C' UNION ALLSELECT '2008-04-02 16:25:07.383', 'A' UNION ALLSELECT '2008-04-02 16:28:07.383', 'A' UNION ALLSELECT '2008-04-03 16:50:34.217', 'B' UNION ALLSELECT '2008-04-03 13:23:30.767', 'C' UNION ALLSELECT '2008-04-04 13:41:41.627', 'D' UNION ALLSELECT '2008-04-04 13:55:00.497', 'E'DECLARE @table2 TABLE( [Date] datetime, Cnt int)DECLARE @today datetimeSELECT @today = '2008-04-04'INSERT INTO @table2 ([Date], Cnt)SELECT DATEADD(DAY, DATEDIFF(DAY, 0, [Date]), 0), COUNT(*)FROM @table1WHERE [Date] < @todayGROUP BY DATEADD(DAY, DATEDIFF(DAY, 0, [Date]), 0)DELETE t1FROM @table1 t1WHERE [Date] < @todaySELECT *FROM @table1/*Date Value ----------------------- ----- 2008-04-04 13:41:41.627 D2008-04-04 13:55:00.497 E(2 row(s) affected)*/SELECT *FROM @table2/*Date Cnt ------------------------ ----------- 2008-04-01 00:00:00.000 2 2008-04-02 00:00:00.000 4 2008-04-03 00:00:00.000 2 (3 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-09-04 : 23:11:58
|
forgot to refresh KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|