October 21, 2011 at 10:48 am
.Una funcion recursiva para la suma de los "n" primeros numeros de la serie
A=1+2+3+4+...+n
October 21, 2011 at 11:00 am
Homework or a test?
Take a look at: http://msdn.microsoft.com/en-us/library/ms186243.aspx
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
October 21, 2011 at 11:03 am
.
October 21, 2011 at 11:04 am
Thought so.
Did you take a look at the recursive CTE article I linked to?
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
October 21, 2011 at 11:37 am
Of course, the real way to do this in T-SQL isn't recursive, but that would violate your assignment.
select sum(X)
from (select top (n) X
from dbo.MyTable
order by MySequenceColumn) as Subq1 ;
Another recursive option would be a recursive UDF, instead of a recursive CTE. You might want to find out which one(s) are acceptable for the assignment. Either one will be slower, less efficient, and just plain a bad idea, compared to the set-based one here.
But homework is sometimes like that. After all, if school ever actually resembled real life, that would be bad! 😛
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
October 21, 2011 at 2:44 pm
Actually, if you really want to impress, do the problem as assigned. Then show how much more efficient the non-recursive solution is. For bonus credit, do a little research and explain why. (Then you could come back and tell us!)
October 21, 2011 at 5:13 pm
champions (10/21/2011)
.Una funcion recursiva para la suma de los "n" primeros numeros de la serieA=1+2+3+4+...+n
Tell your instructor to read the following article for why this should be taught ONLY as a very bad thing to do...
http://www.sqlservercentral.com/articles/T-SQL/74118/
Also, the quickest way to find the SUM of sequential ascending numbers starting a 0 or 1 is...
SUM of numbers 0/1 through n = (n2 + n) / 2
... but as Gus suggested, that would probably violate the intent of the homework.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply