January 8, 2013 at 4:15 am
Hi All
First schema:
create table tab1 (forumId int Primary key,ParentId int,HitNumber int)
insert into tab1
select 3,9,119 UNION ALL
select 4,NULL,88 UNION ALL
select 5,NULL,72 UNION ALL
select 6,NULL,1463 UNION ALL
select 8,5,499 UNION ALL
select 9,NULL,65 UNION ALL
select 10,3,1065 UNION ALL
select 11,NULL,128 UNION ALL
select 13,9,789 UNION ALL
select 15,NULL,54
;WITH CTE AS
(
SELECT forumId,ParentId,HitNumber
FROM [tab1]
WHERE ForumID=9
UNION ALL
SELECT DD2.forumId,DD2.ParentId,DD2.HitNumber
FROM tab1 as DD2
INNER JOIN CTE ON DD2.parentId=CTE.ForumId
)
SELECT SUM(HitNumber) FROM CTE
--select * from CTE
In Above table , ForumId 9 is Parent to ForumId 3 and 13. 3 Is parent to 10.
Now as you can see, I have hard coded the query for ForumId=9. But I need the SUM of hits for All
ForumIds. So I need SUM of Hits for ForumID 9,3,13,10.
I don't have to use a funtion. In fact the whole exercise is for removing function for Azure.
Help pls.
Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.
January 8, 2013 at 4:23 am
What function are you trying to remove?
The SUM?
I would of thought that Azure would come with the bulit in aggregating functions as standard.
http://msdn.microsoft.com/en-us/library/windowsazure/ee336248.aspx#aggregate
Azure does use the standard aggregating functions
January 8, 2013 at 4:25 am
Sorry for confusion. I am trying to remove a user defined function already built, not the sum function
Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.
January 8, 2013 at 4:26 am
S_Kumar_S (1/8/2013)
Sorry for confusion. I am trying to remove a user defined function already built, not the sum function
Ok what is the problem you are having with the code, it seems to work for me.
January 8, 2013 at 4:36 am
Problem is :
I have hard coded the query for ForumId=9. But I need the SUM of hits for All
ForumIds individually. Something like:
ForumId SumOfHitCount
9 2038
3 1184
..............................
...................
Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.
January 8, 2013 at 5:08 am
So for each forum ID, you want the number of hits, including the number of hits for any child forums.
488
5571
61463
92038
11128
1554
101065
8499
31184
13789
January 8, 2013 at 5:14 am
Correct Sir...
Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.
January 8, 2013 at 6:41 am
;WITH CTE AS
(
SELECT forumId,ParentId,HitNumber,
Seq = CAST(','+CAST(forumId AS VARCHAR(2)) AS VARCHAR(20))
FROM #tab1
WHERE ParentId IS NULL
UNION ALL
SELECT DD2.forumId, DD2.ParentId, DD2.HitNumber,
Seq = CAST(CTE.Seq + ',' + CAST(DD2.forumId AS VARCHAR(2)) AS VARCHAR(20))
FROM #tab1 AS DD2
INNER JOIN CTE
ON CTE.ForumId = DD2.parentId
)
SELECT c.forumId, c.ParentId,
x.TotalHitNumber
FROM CTE c
CROSS APPLY (
SELECT TotalHitNumber = SUM(HitNumber)
FROM CTE
WHERE Seq+',' LIKE '%,'+CAST(c.forumId AS VARCHAR(2))+',%'
) x
ORDER BY c.forumId
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, 2013 at 7:03 am
Thanks So much Chris and it works. I recognize the deep thought you might have put in it. Thanks for your time...
Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply