Results 1 to 9 of 9

Thread: [RESOLVED] Simple SQL Count Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    Resolved [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

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  3. #3
    Addicted Member
    Join Date
    Jun 2006
    Location
    New Orleans, LA
    Posts
    161

    Re: Simple SQL Count Question

    Untested, but try:


    SELECT COUNT(DISTINCT SerialNum, CommentNum) as "Count" FROM CSSComments WHERE BatchID = 'Test'

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    Re: Simple SQL Count Question

    Minolwen,

    Got this error:
    Msg 102, Level 15, State 1, Line 1
    Incorrect syntax near ','.

    Thanks

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    Re: Simple SQL Count Question

    techgnome

    same error:
    Msg 102, Level 15, State 1, Line 1
    Incorrect syntax near ','.

    Thanks

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    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
  •  



Click Here to Expand Forum to Full Width