Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Wierd Counter! Expand / Collapse
Author
Message
Posted Friday, October 24, 2008 7:31 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:50 PM
Points: 36,748, Visits: 31,195
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 Moden
"RBAR 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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #591187
Posted Friday, October 24, 2008 8:22 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, November 18, 2013 9:18 PM
Points: 51, Visits: 215
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 a

criteria:
- 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!
Post #591249
Posted Friday, October 24, 2008 8:28 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 11:42 AM
Points: 478, Visits: 1,410
You just need to add one criteria to the CASE statement. Like this:

UPDATE #Counter2
SET @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 = RowNumber
FROM #Counter2




Post #591255
Posted Friday, October 24, 2008 8:29 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 11:42 AM
Points: 478, Visits: 1,410
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.
Post #591258
Posted Friday, October 24, 2008 8:39 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:50 PM
Points: 36,748, Visits: 31,195
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 variables
DECLARE @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 Moden
"RBAR 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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #591265
Posted Friday, October 24, 2008 8:43 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:50 PM
Points: 36,748, Visits: 31,195
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 Moden
"RBAR 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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #591269
Posted Friday, October 24, 2008 8:49 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 11:42 AM
Points: 478, Visits: 1,410
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!
Post #591275
Posted Friday, October 24, 2008 9:22 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, November 18, 2013 9:18 PM
Points: 51, Visits: 215
Jeff and Graber

Thank you so much, and thank you for the explanation. It helps a lot to understand where my problem was. I really appreciate your help.
Post #591300
Posted Friday, October 24, 2008 12:00 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 9:38 AM
Points: 1,525, Visits: 4,071
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 Phelabaum
Consistency 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
Post #591422
Posted Friday, October 24, 2008 1:41 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:50 PM
Points: 36,748, Visits: 31,195
JohnDBA (10/24/2008)
Jeff and Graber

Thank 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 Moden
"RBAR 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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #591478
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse