Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Counting child rows?

Author  Topic 

FruitBatInShades
Yak Posting Veteran

51 Posts

Posted - 2008-01-30 : 05:03:12
I am trying to count child rows but cannot get it to work. The count keeps returning 1. I tried counting SiteFolders.Path and Pages.Directory. Can some one explain what I may be doing wrong?

SELECT     SiteFolders.Path, Pages.PageName, Pages.UniqueID, COUNT(SiteFolders.Path) AS ChildCount
FROM SiteFolders LEFT OUTER JOIN
Pages ON SiteFolders.Path = Pages.Directory
GROUP BY SiteFolders.Path, Pages.PageName, Pages.UniqueID
ORDER BY SiteFolders.Path

	
content contact_us 34 1
content ThankYou 56 1
content Events 61 1
content PastEvents 62 1
content ViewEvents 63 1
content Directory 65 1
content\child1 shjsdhjsd 66 1
content\child1\baby1 uysuysd 67 1
content\child1\baby1 kjs943jhi 69 1
content\child2 lka87sd 68 1
content\child2 oks8sd98oi 72 1

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-30 : 05:19:26
Read this article and try if you can apply this to your problem...

http://vyaskn.tripod.com/hierarchies_in_sql_server_databases.htm
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-30 : 06:06:13
Try to count the OUTER table instead.
SELECT		SiteFolders.Path,
Pages.PageName,
Pages.UniqueID,
COUNT(Pages.UniqueID) AS ChildCount
FROM SiteFolders
LEFT JOIN Pages ON Pages.Directory = SiteFolders.Path
GROUP BY SiteFolders.Path,
Pages.PageName,
Pages.UniqueID
ORDER BY SiteFolders.Path



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

FruitBatInShades
Yak Posting Veteran

51 Posts

Posted - 2008-01-30 : 06:35:05
@Peso: It doesn't work either way :(
@visakh16: I can't change the database structure
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-30 : 07:34:34
Follow the advises in this blog post to get fast and accurate answers.
Until you do, all we can do is guessing...
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -