January 8, 2009 at 9:14 am
Chris Morris (1/8/2009)
Garadin (1/8/2009)
Think I just found a fitting avatar for myself for that matter.Isn't that Muffin?
My turn to Google! Never heard of Muffin the Mule before... but no, that's not exactly what I was going for with that one.
January 8, 2009 at 9:15 am
Lynn Pettis (1/8/2009)
hmmm, avatar I too have found
Gits unite old, yes!
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 8, 2009 at 9:17 am
hmmm, hijacked this thread I think, back to topic I think we should...
January 8, 2009 at 9:19 am
Garadin (1/8/2009)
The joke doesn't make nearly as much sense if you haven't seen the movie. The butler in the movie is "very sneaky", and keeps appearing randomly whenever he's needed with nobody seeing him coming. Pretty amusing stuff. It's an Adam Sandler movie, and you typically either like his movies or you don't(I normally do), and I thought it was pretty decent. It's been out for several years now, so I'd imagine it'd be available if you were so inclined to watch it =).
Heh I'll look for it in Woolworths...Zavvi...I'll download it! Cheers Seth.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 8, 2009 at 9:24 am
Garadin (1/8/2009)
Chris Morris (1/8/2009)
Garadin (1/8/2009)
Think I just found a fitting avatar for myself for that matter.Isn't that Muffin?
My turn to Google! Never heard of Muffin the Mule before... but no, that's not exactly what I was going for with that one.
Joking aside IMO it's a brilliant choice. Apart from anything else, the emphasis on teaching will give you quite a bit of leeway with the "give a man a fish" scenario. We've all been there, Jeff's had a particularly nasty one recently.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 8, 2009 at 11:34 am
Here is the test data for the situation...
select cast('a' as varchar(5)) act,
cast(1 as tinyint) nbr,
cast('nick' as varchar(10)) name,
cast('2008-01-01' as smalldatetime) dt
into tmptst
insert into tmptst values('a', 1, 'nick', '2008-01-02')
insert into tmptst values('a', 1, 'nick', '2008-01-03')
insert into tmptst values('a', 2, 'nick', '2008-01-05')
insert into tmptst values('b', 1, 'nick', '2008-01-01')
insert into tmptst values('b', 1, 'nick', '2008-01-02')
insert into tmptst values('b', 1, 'nick', '2008-01-03')
insert into tmptst values('b', 2, 'nick', '2008-01-05')
insert into tmptst values('c', 1, 'nick', '2008-01-01')
insert into tmptst values('c', 1, 'nick', '2008-01-02')
insert into tmptst values('c', 1, 'nick', '2008-01-03')
January 8, 2009 at 11:46 am
Have you read the Article 'Solving the "Running Totals" & "Ordinal Rank" Problems in SS 2K/2K5'? It is the second link in my signature block below. If you haven't read, please read it now. If, after reading it, you still have questions on how to implement the solution let us know.
January 8, 2009 at 2:13 pm
This works for me - let me know if you see some thing wrong in here...
drop table tmptst
select cast('a' as varchar(5)) act,
cast(1 as tinyint) nbr,
cast('nick' as varchar(10)) name,
cast('2008-01-01' as smalldatetime) dt,
CAST(NULL AS INT) GRPCNT
into tmptst
insert into tmptst values('a', 1, 'nick', '2008-01-02', null)
insert into tmptst values('a', 1, 'nick', '2008-01-03', null)
insert into tmptst values('a', 2, 'nick', '2008-01-05', null)
insert into tmptst values('b', 1, 'nick', '2008-01-01', null)
insert into tmptst values('b', 1, 'nick', '2008-01-02', null)
insert into tmptst values('b', 1, 'nick', '2008-01-03', null)
insert into tmptst values('b', 2, 'nick', '2008-01-05', null)
insert into tmptst values('c', 1, 'nick', '2008-01-01', null)
insert into tmptst values('c', 1, 'nick', '2008-01-02', null)
insert into tmptst values('c', 1, 'nick', '2008-01-03', null)
CREATE CLUSTERED INDEX IX_key
ON dbo.tmptst (Act, nbr, name, Dt)
DECLARE @PrevGrpCnt INT --Running count resets when account changes
SET @PrevGrpCnt = 0
DECLARE @PrevAct varchar(5) --The "anchor" and "account change detector"
SET @PrevAct = ''
DECLARE @PrevName varchar(5) --The "anchor" and "account change detector"
SET @PrevName = ''
DECLARE @PrevNbr tinyint --The "anchor" and "account change detector"
SET @PrevNbr = 0
UPDATE dbo.tmptst
SET --===== Running Total
@PrevGrpCnt = GrpCnt = CASE WHEN Act = @PrevAct and @Prevname = name and @Prevnbr = nbr
THEN @PrevGrpCnt + 1
ELSE 1 -- Restarts count at "1"
END,
--===== "Anchor" and provides for "account change detection"
@Prevact = Act,
@Prevname = name,
@Prevnbr = nbr
FROM dbo.tmptst
WITH (INDEX(IX_key),TABLOCKX)
January 8, 2009 at 3:22 pm
hmmm, got it you do
I don't have time at the moment to actually test your code, but a quick check of the code looks good. I'll try to double check it later unless someone else beats me to it.
January 8, 2009 at 3:36 pm
January 8, 2009 at 6:24 pm
Chris Morris (1/8/2009)
Apart from anything else, the emphasis on teaching will give you quite a bit of leeway with the "give a man a fish" scenario. We've all been there, Jeff's had a particularly nasty one recently.
Which post are you talking about? :pinch:
--Jeff Moden
Change is inevitable... Change for the better is not.
January 8, 2009 at 7:29 pm
I did not really time it as there was no need..:) it completed without causing any concerns. May be a minute or so.
Thanks guys for pointing me to the article and making me think a little bit rather than just giving a solution.
Thanks - learnt a new technique.
January 8, 2009 at 8:25 pm
SQLMAIN (1/8/2009)
May be a minute or so.
That would be about right for 15M rows.
Just a bit of caution, though... make sure you follow ALL the instructions in the article. Forgetting just one of them can give you some really bad data. I have to say it "out loud" because so many people have taken a shortcut here and there or simply forgotten a "step" in the article and BOOM! Instant bad data... don't be afraid to use the technique but do be very afraid if you don't do it right. ๐
The example code you wrote above looks like you've included everything.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 9, 2009 at 6:48 am
Jeff Moden (1/8/2009)
Chris Morris (1/8/2009)
Apart from anything else, the emphasis on teaching will give you quite a bit of leeway with the "give a man a fish" scenario. We've all been there, Jeff's had a particularly nasty one recently.Which post are you talking about? :pinch:
This one...
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 9, 2009 at 8:01 am
Heh... ah yes... THAT one. And because I'm a professional, I'm also supposed to be nice in the face of such abuse. I rewrote that one about 5 times trying to be nice. What it really needed was a barrage of high speed pork chops. ๐
How'd I do? :hehe:
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 16 through 30 (of 38 total)
You must be logged in to reply to this topic. Login to reply