In a database we have about 1000 registered users. I'd like to know how many of those users have actually written a question or posted an answer. All the information can be take from the tblQA table and the userid is "intPosterID", the questions & answers each have their own ID, "PostID". Is there a query that can be run to count how many unique users have posted a question or answer?
To count distinct values, you can use distinct in aggregate function count(). The result i3 tells that we have 3 distinct values in the table.
Counting the distinct userIDs can be done via: SELECT COUNT( DISTINCT intPosterID ) FROM tblQA; COUNT( DISTINCT field ) returns a count of the number of rows with different values for the given field - intPosterID in this case.
Answer: COUNT function is an aggregate function that can be used in 3 ways. COUNT(*) – This would COUNT all the rows returned by the SELECT QUERY.
rowcount. This read-only property returns the number of rows returned for SELECT statements, or the number of rows affected by DML statements such as INSERT or UPDATE .
Counting the distinct userIDs can be done via:
SELECT COUNT( DISTINCT intPosterID ) FROM tblQA;
COUNT( DISTINCT field )
returns a count of the number of rows with different values for the given field - intPosterID
in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With