Results 1 to 7 of 7

Thread: [RESOLVED] HOW to count records in database??

  1. #1

    Thread Starter
    Addicted Member NinaWilliam's Avatar
    Join Date
    May 2005
    Location
    @Home
    Posts
    133

    [RESOLVED] HOW to count records in database??

    in my database each doctor has multiple slips (one to many)..i want to cout the total
    number of slips for each doctor and display it in the datagrid...in a culomn called
    "Uncompleted Slips".. so it will display the total number of slips near the doctor name

    How can i do that??

    SQL Server 2000
    Last edited by NinaWilliam; Mar 8th, 2006 at 02:00 AM.

  2. #2
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: HOW to count records in database??

    I presume your column is something like

    RowNo | Slips
    1 5
    2 10
    3 6

    In which case you can simply use

    Select Sum(Slips)
    FROM yourTable

    Which would return a single row with an item containing a value 21 !

    Note that this is a normal select statment. Thus you could do

    Select Sum(Slips) From yourTable Where Doctor = DoctorID

    Hope this helps !

  3. #3
    New Member
    Join Date
    May 2005
    Posts
    3

    Re: HOW to count records in database??

    I am assuming
    DoctorId SlipId are the collumn name.
    U can Get By
    Select doctorId,Count(SlipId)
    From tableName
    Group By
    DoctorId

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: HOW to count records in database??

    Code:
    Select DoctorId,Sum(1) From SomeTable Group by DoctorId
    Count() does funny things with NULL values - Sum(1) is more intuitive to me.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Member CommanderEl's Avatar
    Join Date
    Feb 2005
    Location
    Adelaide, Australia
    Posts
    40

    Talking Re: HOW to count records in database??

    apart from the fact they both do different things
    count counts the number or rows returned when you group by something intelligent while sum will add up a column's values, grouped or not

  6. #6

    Thread Starter
    Addicted Member NinaWilliam's Avatar
    Join Date
    May 2005
    Location
    @Home
    Posts
    133

    Re: HOW to count records in database??

    thanks guys..

  7. #7
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: HOW to count records in database??

    Quote Originally Posted by szlamany
    Count() does funny things with NULL values - Sum(1) is more intuitive to me.
    It works, but Sum is more costly than the following, which solves for the same thing:

    SELECT COUNT(*) FROM DATABASE WHERE DOCTORID = @ID

    COUNT(*) counts rows.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

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