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 |
|
miked1978
Starting Member
25 Posts |
Posted - 2008-11-25 : 08:01:03
|
| I'm doing a simple record count which counts the Mondate field:select Mondate, count (Mondate) as Mondate1from tablename1Group By MondateOrder by MondateThis table holds 7 weeks of data and is updated each week (Tuesday) which inserts the current week and takes out the oldest week. Each week the record count gets bigger so if the record count is the same as the previous weeks then 99.9% of the time something is wrong. I want to code something in that takes the current week's count and match it to the previous weeks count. If they are the same then I want it to raise a flag or something. Can someone please help? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-25 : 08:14:31
|
[code]DECLARE @Sample TABLE ( monDate DATETIME )INSERT @Sample ( monDate )SELECT '20081117' UNION ALLSELECT '20081117' UNION ALLSELECT '20081110'DECLARE @currentWeek DATETIMESET @currentWeek = '20081122'-- SimsalabimDECLARE @previousWeek DATETIMESELECT @currentWeek = DATEADD(DAY, DATEDIFF(DAY, 0, @currentWeek) / 7 * 7, 0), @previousWeek = DATEADD(DAY, -7, @currentWeek)SELECT @currentWeek AS monDate, SUM(CASE WHEN @currentWeek = monDate THEN 1 ELSE 0 END) AS thisMonDate, SUM(CASE WHEN @previousWeek = monDate THEN 1 ELSE 0 END) AS previousMonDateFROM @SampleWHERE monDate >= @previousWeek AND monDate < DATEADD(DAY, 1, @currentWeek)[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
miked1978
Starting Member
25 Posts |
Posted - 2008-11-25 : 08:23:50
|
| Peso, thank you. I'm going to try this. |
 |
|
|
|
|
|
|
|