## Wierd Counter!

 Author Message Jeff Moden SSC Guru Group: General Forum Members Points: 506186 Visits: 44262 ggraber (10/24/2008)P.S. You may want to read Jeff's cool article on Running Totals which explains how this approach works http://www.sqlservercentral.com/articles/Advanced+Querying/61716/Heh... that's where I was going with this... I just wanted to know if he wanted his original table to be updated or if I was going to have to use a temp table. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs JohnDBA SSC Veteran Group: General Forum Members Points: 208 Visits: 232 Graber,You are very close to what I need. But, row 16 should start with 1 and not row 15 becuase: - the conter needs to start only when the code is = a in row 15 the first criteria is correct but code start with b ant not with acriteria:- If the value is between 10 and 15 and the code is = a then start count once I find the first value that meets the criteria up to 10 and start all over once I find another value that meets the criteria again. Thank you so much! Goldie Lesser Hall of Fame Group: General Forum Members Points: 3544 Visits: 1513 You just need to add one criteria to the CASE statement. Like this:`UPDATE #Counter2SET @Counter = Counter = CASE WHEN (@Counter >= 1 AND @Counter < 10) OR ([Value] >= 10 AND [Value] <=15 AND Code = 'a')                           THEN @Counter + 1                           ELSE 0 -- restart the counter                           END   ,@RowNumber = RowNumberFROM #Counter2` Goldie Lesser Hall of Fame Group: General Forum Members Points: 3544 Visits: 1513 Jeff Moden (10/24/2008)ggraber (10/24/2008)P.S. You may want to read Jeff's cool article on Running Totals which explains how this approach works http://www.sqlservercentral.com/articles/Advanced+Querying/61716/Heh... that's where I was going with this... I just wanted to know if he wanted his original table to be updated or if I was going to have to use a temp table.I figured. Funny how a lot of the questions on the forum revolve around the same few concepts. Jeff Moden SSC Guru Group: General Forum Members Points: 506186 Visits: 44262 JohnDBA (10/23/2008)Please help me!I have this values that I need to count based on condition. - If the value is between 10 and 15 and the code is = a then start count once I find the first value that meets the criteria up to 10 and start all over once I find another value that meets the criteria again. John,GGraber is exactly on the right track but there's a couple of things that are missed.Didn't start the count in regards to Code "a" .Used "0" instead of NULL.Forgot to include a Clustered Index to GUARANTEE the order of the update (although it worked without in this example, ya gotta have it to guarantee the order... and this only works with Updates, not Selects).So, using your fine test data and GGraber's good code, here's a very high performance solution using the "running total" method that GGraber cited... as with GGraber's code, the details are in the comments...`--===== Create a temporary working table with a dummy unique identifier (RowNumber) -- that will also be the object of the Clustered Index as a PK. Notice also, -- that we've added a "Counter" column to hold the result of your request. CREATE TABLE #Counter2( RowNumber INT IDENTITY(1,1), StartDate DATETIME, Value FLOAT, Code CHAR(1), Counter INT)--===== Copy all existing data from original table into our results table -- in the correct order. INSERT INTO #Counter2(StartDate, Value, Code) SELECT StartDate, Value, Code FROM #Counter ORDER BY StartDate --===== MUST have a clustered primary key to GUARANTEE this will work ALTER TABLE #Counter2 ADD PRIMARY KEY CLUSTERED (RowNumber)--===== Create the required local variablesDECLARE @Counter INT, -- Start value is NULL @RowNumber -- Dummy variable to "anchor" the "running" update--===== Calculate and store the value of the counter in the table using a "pseudo-cursor" -- the order of which is driven by a forced scan on the Clustered Index. -- Rules for the counter value are... -- -- 1. Any value not in the following criteria will result in NULL for the Counter -- 2. Data must be processed in order by StartDate. -- 3. Counter will start at "1" at the first occurance where the Value is between 10 and 15 and -- and the Code is "a". -- 4. Count will increase by 1 -- 5. When the Counter reaches the value of 10, reset the Counter to NULL -- 6. Do until all rows in RowNumber order have been updated. UPDATE #Counter2 SET @Counter = Counter = CASE WHEN ([Value] BETWEEN 10 AND 15) AND Code = 'a' AND @Counter IS NULL THEN 1 -- Rules 1 & 3 WHEN @Counter = 10 THEN NULL -- Rule 5 ELSE @Counter + 1 -- Rule 4 END, @RowNumber = RowNumber FROM #Counter2 WITH(INDEX(0)) --LOOK! Must refer to the clustered index or could bomb! -- Rules 2 and 6--===== Display the results in RowNumber order SELECT * FROM #Counter2 ORDER BY RowNumber--===== Housekeeping so we can run this example again, if we want. -- This is NOT necessary in final production stored procs because -- it will automatically drop at the end of the proc (how poetic ;-) DROP TABLE #Counter2`Someone will likely figure out and post a way to do this with ROW_NUMBER() or RANK(), but this solution works on SQL Server 2000, as well. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Jeff Moden SSC Guru Group: General Forum Members Points: 506186 Visits: 44262 ggraber (10/24/2008)Jeff Moden (10/24/2008)ggraber (10/24/2008)P.S. You may want to read Jeff's cool article on Running Totals which explains how this approach works http://www.sqlservercentral.com/articles/Advanced+Querying/61716/Heh... that's where I was going with this... I just wanted to know if he wanted his original table to be updated or if I was going to have to use a temp table.I figured. Funny how a lot of the questions on the forum revolve around the same few concepts.Heh... Isn't that the truth? I missed several of the posts above because I was creating a post and you beat me to one of the things I thought was missing. I gotta say it's a real pleasure to see someone document their code... nice job!Just a note... You do have to remember that you MUST have a clustered index on the correct columns and then force it to be used to guarantee that this method will work correctly. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Goldie Lesser Hall of Fame Group: General Forum Members Points: 3544 Visits: 1513 Jeff Moden (10/24/2008) Just a note... You do have to remember that you MUST have a clustered index on the correct columns and then force it to be used to guarantee that this method will work correctly.Thanks for the reminder! JohnDBA SSC Veteran Group: General Forum Members Points: 208 Visits: 232 Jeff and GraberThank you so much, and thank you for the explanation. It helps a lot to understand where my problem was. I really appreciate your help. Garadin SSCoach Group: General Forum Members Points: 16517 Visits: 4107 Jeff Moden (10/24/2008)ggraber (10/24/2008)Jeff Moden (10/24/2008)ggraber (10/24/2008)P.S. You may want to read Jeff's cool article on Running Totals which explains how this approach works http://www.sqlservercentral.com/articles/Advanced+Querying/61716/Heh... that's where I was going with this... I just wanted to know if he wanted his original table to be updated or if I was going to have to use a temp table.I figured. Funny how a lot of the questions on the forum revolve around the same few concepts.Heh... Isn't that the truth? I missed several of the posts above because I was creating a post and you beat me to one of the things I thought was missing. I gotta say it's a real pleasure to see someone document their code... nice job!Just a note... You do have to remember that you MUST have a clustered index on the correct columns and then force it to be used to guarantee that this method will work correctly.I figured I'd let you answer one of these, since I've snagged like the past 10 (Although this one didn't quite work out the way I had hoped it would Seth PhelabaumConsistency is only a virtue if you're not a screwup. ;-)Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation Jeff Moden SSC Guru Group: General Forum Members Points: 506186 Visits: 44262 JohnDBA (10/24/2008)Jeff and GraberThank you so much, and thank you for the explanation. It helps a lot to understand where my problem was. I really appreciate your help.You're very welcome, John. Thanks for the feedback. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs