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)
 Help in joining two query's

Author  Topic 

redbrad0
Posting Yak Master

176 Posts

Posted - 2005-08-06 : 11:35:38
I have a main query that is returning the records and then for each item that is displayed I have to run another query just to count the Sub Domains they have setup with their domain. I would like for the count of the sub domains to be something I could sort by in the main SQL Query. Can someone help me out?

Main Query
SELECT UsersDomains_ID, UsersDomains_URL, UsersDomains_ServerID, UsersDomains_Status, UsersDomains_DateUpdated, UsersServers_Name FROM Users_Domains, Users_Servers WHERE UsersDomains_ServerID=UsersServers_ID ORDER BY UsersDomains_Status ASC, UsersDomains_DateUpdated ASC

Counting SubQuery
SELECT COUNT(UsersDomainsSub_ID) AS Tot FROM Users_DomainsSub GROUP BY UsersDomainsSub_DomainID HAVING (UsersDomainsSub_DomainID = 62)

Quality NT Web Hosting & Design

nr
SQLTeam MVY

12543 Posts

Posted - 2005-08-06 : 14:51:29
You have a filter in the having clause - very bad.

SELECT COUNT(*) AS Tot FROM Users_DomainsSub where UsersDomainsSub_DomainID = 62 GROUP BY UsersDomainsSub_DomainID

I suspect something like
SELECT UsersDomains_ID, UsersDomains_URL, UsersDomains_ServerID, UsersDomains_Status, UsersDomains_DateUpdated, UsersServers_Name,
tot = (select count(*) from Users_DomainsSub where UsersDomainsSub_DomainID = UsersDomains_ID)
FROM Users_Domains, Users_Servers
WHERE UsersDomains_ServerID=UsersServers_ID
ORDER BY UsersDomains_Status ASC, UsersDomains_DateUpdated ASC

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

redbrad0
Posting Yak Master

176 Posts

Posted - 2005-08-06 : 19:20:07
Thanks worked perfect

Quality NT Web Hosting & Design
Go to Top of Page
   

- Advertisement -