Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Problem counting records

 
   Database Forums (Home) -> General Discussions RSS
Next:  [Info-Ingres] National Small Business Network/Dir..  
Author Message
Sir Hystrix

External


Since: Nov 30, 2007
Posts: 2



(Msg. 1) Posted: Fri Nov 30, 2007 8:58 am
Post subject: Problem counting records
Archived from groups: comp>databases>ms-sqlserver (more info?)

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.

 >> Stay informed about: Problem counting records 
Back to top
Login to vote
Dan Guzman1

External


Since: Aug 22, 2004
Posts: 469



(Msg. 2) Posted: Fri Nov 30, 2007 8:58 am
Post subject: Re: Problem counting records [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> 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

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Sir Hystrix" wrote in message

> 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.

 >> Stay informed about: Problem counting records 
Back to top
Login to vote
Manfred Sorg

External


Since: Nov 28, 2007
Posts: 2



(Msg. 3) Posted: Fri Nov 30, 2007 8:58 am
Post subject: Re: Problem counting records [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: Problem counting records 
Back to top
Login to vote
Sir Hystrix

External


Since: Nov 30, 2007
Posts: 2



(Msg. 4) Posted: Fri Nov 30, 2007 8:58 am
Post subject: Re: Problem counting records [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dan Guzman wrote:
>> 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
>

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

Cheers.
 >> Stay informed about: Problem counting records 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
SQL Counting number of non-distinct rows? - Hi, I have a table that for ease has this data in: R1, R2, R....z --------------------- A | 12 A | 22 A | 30 B | 0 B | -1 B | -3 C | 100 I want to generate a table for each distinct row in R1, gives a count of all the rows with data corresponding For....

Get all records from Last Two Days? - Hi Everyone, I use the following to get records from the last two days in MySql: where date_entered <= curdate() and date_entered >= DATE_SUB(curdate(),INTERVAL 2 day) I'm looking to do the same in MS-Sql server but I'm just not getting it. I'v...

Can SQL Lose Records? - We are running SQL 7 with a front end that links to the tables through ODBC. In our main table, the user has no way to delete a record through the interface, though it is possible to delete it by opening the ODBC link. Users would have no reason to..

Add records in a table - Hi: I need to add some records in a table called location(primary key: loc_id). What I want to do is for each location in the table, I add the same record but with a different loc_id, which can be a random string. All the other column should contain the...

SQL select records NOT in both tables - Hi I'm using Access 2002. I have 2 tables tblGroupContact, tblGroupPermission, both have 2 fields identical structure: ContactID GroupID (Both are Composite keys and both hold integers) tblGroupContact holds everybody and the groups they are members....
   Database Forums (Home) -> General Discussions All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]