SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Interesting query


Interesting query

Author
Message
S_Kumar_S
S_Kumar_S
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4841 Visits: 1101
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 16 yrs exp.
anthony.green
anthony.green
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62926 Visits: 8598
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



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


S_Kumar_S
S_Kumar_S
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4841 Visits: 1101
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 16 yrs exp.
anthony.green
anthony.green
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62926 Visits: 8598
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.



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


S_Kumar_S
S_Kumar_S
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4841 Visits: 1101
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 16 yrs exp.
anthony.green
anthony.green
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62926 Visits: 8598
So for each forum ID, you want the number of hits, including the number of hits for any child forums.

4 88
5 571
6 1463
9 2038
11 128
15 54
10 1065
8 499
3 1184
13 789



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


S_Kumar_S
S_Kumar_S
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4841 Visits: 1101
Correct Sir...

Online Trainer For SQL DBA and Developer @RedBushTechnologies with 16 yrs exp.
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97010 Visits: 20698
;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



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
S_Kumar_S
S_Kumar_S
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4841 Visits: 1101
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 16 yrs exp.
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