[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
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 ! :duck:
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
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.
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 :bigyello:
Re: HOW to count records in database??
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.