|
-
Feb 20th, 2009, 11:49 AM
#1
Thread Starter
Lively Member
[RESOLVED] Simple SQL Count Question
I am using SQL Server 2005 but for this question I know that is not a big deal.
This query gets me my desired results:
SELECT DISTINCT SerialNum, CommentNum
FROM CSSComments WHERE BatchID = 'Test'
It returns four records which is correct.
I just need a count showing that I have 4 records.
I am trying this but I get a syntax error:
SELECT Count(*) as Count FROM
(SELECT DISTINCT SerialNum, CommentNum
FROM CSSComments WHERE BatchID = 'Test')
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.
How do I get a return value of 4.
Thanks
-
Feb 20th, 2009, 12:30 PM
#2
Re: Simple SQL Count Question
Try
Select SerialNum, CommentNum,count(*)
From CSSComments
WHERE BatchID = 'Test'
GROUP BY SerialNum, CommentNum
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Feb 20th, 2009, 12:32 PM
#3
Addicted Member
Re: Simple SQL Count Question
Untested, but try:
SELECT COUNT(DISTINCT SerialNum, CommentNum) as "Count" FROM CSSComments WHERE BatchID = 'Test'
-
Feb 20th, 2009, 12:34 PM
#4
Thread Starter
Lively Member
Re: Simple SQL Count Question
That basically gives me the exact same results as my SQL except that you added the count column.
What I need is an ExecuteScalar just returning the number 4 showing that I have 4 records.
Thanks Gary
-
Feb 20th, 2009, 12:36 PM
#5
Thread Starter
Lively Member
Re: Simple SQL Count Question
Minolwen,
Got this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Thanks
-
Feb 20th, 2009, 12:36 PM
#6
Re: Simple SQL Count Question
Only don't use "Count" .... the quotes will cause a problem.... instead give it a name (other than count) ...
SELECT COUNT(DISTINCT SerialNum, CommentNum) as RecordCount FROM CSSComments ...
-tg
-
Feb 20th, 2009, 12:39 PM
#7
Thread Starter
Lively Member
Re: Simple SQL Count Question
techgnome
same error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Thanks
-
Feb 20th, 2009, 12:39 PM
#8
Re: Simple SQL Count Question
Actually, I don't think your original query was all that far off...
SELECT Count(*) as RecordCount FROM
(SELECT DISTINCT SerialNum, CommentNum
FROM CSSComments WHERE BatchID = 'Test')
OR
SELECT COUNT(DISTINCT SerialNum + CommentNum) as RecordCount FROM CSSComments
either of those should work...
-tg
-
Feb 20th, 2009, 12:44 PM
#9
Thread Starter
Lively Member
Re: Simple SQL Count Question
Success!!
techgnome, the first query still didn't work. It gave me the same error that I was getting in my original post about an invalid ")".
The second query works like a charm.
Thanks for everyones input, will mark as Resolved!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|