Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Interesting query Expand / Collapse
Author
Message
Posted Tuesday, January 8, 2013 4:15 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 11:18 AM
Points: 380, Visits: 903
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.


-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
Post #1404129
Posted Tuesday, January 8, 2013 4:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 8, 2014 1:45 AM
Points: 5,221, Visits: 5,120
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




Want an answer fast? Try here
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
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1404134
Posted Tuesday, January 8, 2013 4:25 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 11:18 AM
Points: 380, Visits: 903
Sorry for confusion. I am trying to remove a user defined function already built, not the sum function

-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
Post #1404136
Posted Tuesday, January 8, 2013 4:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 8, 2014 1:45 AM
Points: 5,221, Visits: 5,120
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.




Want an answer fast? Try here
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
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1404139
Posted Tuesday, January 8, 2013 4:36 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 11:18 AM
Points: 380, Visits: 903
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
..............................
...................


-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
Post #1404146
Posted Tuesday, January 8, 2013 5:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 8, 2014 1:45 AM
Points: 5,221, Visits: 5,120
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




Want an answer fast? Try here
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
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1404166
Posted Tuesday, January 8, 2013 5:14 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 11:18 AM
Points: 380, Visits: 903
Correct Sir...

-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
Post #1404173
Posted Tuesday, January 8, 2013 6:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:43 AM
Points: 6,890, Visits: 14,254
;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
Post #1404206
Posted Tuesday, January 8, 2013 7:03 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 11:18 AM
Points: 380, Visits: 903
Thanks So much Chris and it works. I recognize the deep thought you might have put in it. Thanks for your time...

-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
Post #1404227
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse