Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


a query like factorial


a query like factorial

Author
Message
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45098 Visits: 39912
schleep (3/19/2013)
Yikes. Thanks for pointing that out Jeff.

I've only (reluctantly) used it with very small sets, mostly in pre-2K5 code.
I'll be checking my code now...


You bet and thanks for the feedback.

If you have something big, a properly configured "Quirky Update" is both bullet-proof and lightning quick. It'll process a million rows in less than 3 seconds on most machines. There are some pretty strict rules to follow to make sure it doesn't go "quirky" on you, but it's usually worth it. It even beats the new functionality they added to SUM() OVER in 2012. It is, however, unsupported and if that's a problem for someone, then just use a recursive CTE or even a fire-hose cursor (likely better than the rCTE but haven't tested it) to do the running total.

--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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21075 Visits: 18259
masoudk1990 (3/19/2013)
Thank you Horatiu, mickyT, Erin Ramsay for your time and help.

@SQLRNNR
May you help me to trace your code on paper please?
Im a newbie and im not sure how does it work.

In my real table ID is NOT sequential. Thats why I tried to generate row number using ROW_NUMBER() function.

First, I think you forget to write WHERE statement in anchor and you meant something like this:


WITH CTE (ID,Value) AS
(
SELECT ID,
Value
FROM @valuestab
WHERE ID = (SELECT MIN(ID) FROM @valuestab) --I guess you forget this line
UNION ALL



So your anchor should generate this output:

ID|Value
--------
1 | 19

Now we must union anchor with second part of query until it satisfied all rows.
At first run this condition b.ID +1 = a.ID change our table to this:

ID|Value
--------
1 | 19
2 | 19 + 90

Now it makes me confused. Once again we should check this condition b.ID +1 = a.ID
Does it check this condition only on last row or it check it on all rows?
If it check it on all rows it should generate this output:

ID|Value
--------
1 | 19
2 | 19 + 90
2 | 19 + 90 <-----surplus
3 | 19 + 90 + 20

I have problem with recursive statements, I know what is this, I know how we should write it.
But I still have logical problem with its mechanism.

Thank you for help.


The where clause in the anchor was not forgotten. It was unnecessary.

You are going to need to provide a representative data sample and table structure if you want a different solution.

Based on the data you provided, the script works. But if you are looking for something different, you need to write out exactly what you are trying to do.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

masoudk1990
masoudk1990
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 299
Sorry for replying after so long, but I found an easy solution for this and I liked to share it here, perhaps it help someone in future.


DECLARE @factorial int

SET @factorial = 0

UPDATE tableName
SET @factorial = value = @factorial + value --value is column name



If you want to store factorial value in seperate column in each row:


DECLARE @factorial int

SET @factorial = 0

UPDATE tableName
SET @factorial = seperateColumn = @factorial + value --value is column name



___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
That what a "quirky update" is about...
But!
To make it bullet-proof you need to read J.Moden article and see what else should be added into your query, so it will always work properly (eg. clustered index and MAXDOP 1).
Otherwise, you may find surprising that sometimes you will end up with unexpected results...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search