Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / November 2007

Tip: Looking for answers? Try searching our database.

Problem counting records

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sir Hystrix - 30 Nov 2007 10:18 GMT
Hi,

I am struggling with a simple query, but I just don't see it.
I have the following example table.

Table Messages
ID  Subject  Reply_to
1   A        0
2   Ax       1
3   A        1
4   B        0
5   By       4
6   C        0

The table holds new messages as well as replies to messages.
Messages with Reply_to = 0 are top messages, the other messages are
replies to a top message. The subject of a reply message does not
necessarily have to be the same as the subject of the top message.

What I would like to have returned is this: a list of messages where
Reply_to = 0 and the number of replies to this message.

ID  Subject Num_replies_to
1   A       2
4   B       1
6   C       0

Any assistance would be greatly appreciated.
Manfred Sorg - 30 Nov 2007 12:13 GMT
Did you think of this:

select t1.ID, t1.Subject, count(1) as Num_replies_to
from tbl t1
left join tbl t2
on t2.Reply_to=t1.ID
where t1.Reply_to=0
group by t1.ID, t1.Subject

Bye, Manfred
Dan Guzman - 30 Nov 2007 12:16 GMT
> What I would like to have returned is this: a list of messages where
> Reply_to = 0 and the number of replies to this message.

A subquery like the example below is one method.

SELECT
   m.ID,
   m.Subject,
   (SELECT COUNT(*)
       FROM dbo.Messages
       WHERE Reply_to = m.ID
       ) AS Num_replies_to
FROM dbo.Messages AS m
WHERE Reply_to = 0

Signature

Hope this helps.

Dan Guzman
SQL Server MVP

> Hi,
>
[quoted text clipped - 24 lines]
>
> Any assistance would be greatly appreciated.
Sir Hystrix - 30 Nov 2007 13:30 GMT
>> What I would like to have returned is this: a list of messages where
>> Reply_to = 0 and the number of replies to this message.
[quoted text clipped - 10 lines]
> FROM dbo.Messages AS m
> WHERE Reply_to = 0

I knew it was simple. It had to be simple. I just didn't see it.
Many thanks to both Dan and Manfred.

Cheers.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.