Count total number of callers? - sql

I'm currently doing this query to find the guy who makes the most calls:
SELECT
`commenter_name`,
COUNT(*) AS `calls`
FROM `comments`
GROUP BY `commenter_name`
ORDER BY `calls` LIMIT 1
What I want now is to be able to find out how many total unique callers. I tried using DISTINCT but I didn't get anywhere.

SELECT COUNT(DISTINCT 'commenter_name') FROM 'comment';

SELECT DISTINCT `commenter_name` FROM `comments`

Select Count(*) From (Select DISTINCT commenter_name from comment) as CmtCnt;

Related

how to get count of total number of rows following a group by?

I wanted to group some data and then count the number of rows after the duplicate data has been combined into groups.
data:
idx
7706
7706
1000
want to return total count = 2
select count(*) (
select nb.idx
group by nb.idx
) as test
First thing that came to mind was this.
and then I saw someone do this
select count(*) over()
group by nb.idx
I'm not sure I totally understand this second one, but I wanted to try various things and compare the speed of the various methods.
use count(distinct idx)
select count(distinct idx)
from tablname
Try below query..
select count(*), nb.idx from table_name
group by nb.idx

SQL select count distinct

I want to know how many items of distinct name I have in my database.
When I use:
select count(distinct name) from products
I obviously gain only number of different, distinct names I have in my
database. I was experimenting with group by, but as a total beginner I
failed. I'll appreciate any help.
Group by name and use count() to get the counts for each group
select name, count(*)
from products
group by name
select count(name) as ct, name from products group by name
SELECT NAME, COUNT(*) FROM products GROUP BY name

How to count unique rows in Oracle

I have an oracle database table with a lot of columns. I'd like to count the number of fully unique rows. The only thing I could find is:
SELECT COUNT(DISTINCT col_name) FROM table;
This however would require me listing all the columns and I haven't been able to come up with syntax that will do that for me. I'm guessing the reason for that is that this query would be very low performance? Is there a recommended way of doing this?
How about
SELECT COUNT(*)
FROM (SELECT DISTINCT * FROM Table)
It depends on what you are trying to accomplish.
To get a count of the distinct rows by specific column, so that you know what data exists, and how many of that distinct data there are:
SELECT DISTINCT
A_CODE, COUNT(*)
FROM MY_ARCHV
GROUP BY A_CODE
--This informs me there are 93 unique codes, and how many of each of those codes there are.
Another method
--How to count how many of a type value exists in an oracle table:
select A_CDE, --the value you need to count
count(*) as numInstances --how many of each value
from A_ARCH -- the table where it resides
group by A_CDE -- sorting method
Either way, you get something that looks like this:
A_CODE Count(*)
1603 32
1600 2
1605 14
I think you want a count of all distinct rows from a table like this
select count(1) as c
from (
select distinct *
from tbl
) distinct_tbl;
SELECT DISTINCT **col_name**, count(*) FROM **table_name** group by **col_name**

SQL count distinct

How can I do a count distinct selection? Basically I have all these charges and I want to count only the number of distinct employees involved with that charge. (I already have other fields for the group by)
SELECT COUNT(DISTINCT employeeid) FROM ....
Simple as: select count(distinct(field)) from table
Live sample: http://sqlfiddle.com/#!3/81379/2/3
You can put a DISTINCT in a COUNT (in TSQL):
SELECT COUNT(DISTINCT(employee)) ...

adding count( ) column on each row

I'm not sure if this is even a good question or not.
I have a complex query with lot's of unions that searches multiple tables for a certain keyword (user input). All tables in which there is searched are related to the table book.
There is paging on the resultset using LIMIT, so there's always a maximum of 10 results that get withdrawn.
I want an extra column in the resultset displaying the total amount of results found however. I do not want to do this using a separate query. Is it possible to add a count() column to the resultset that counts every result found?
the output would look like this:
ID Title Author Count(...)
1 book_1 auth_1 23
2 book_2 auth_2 23
4 book_4 auth_.. 23
...
Thanks!
This won't add the count to each row, but one way to get the total count without running a second query is to run your first query using the SQL_CALC_FOUND_ROWS option and then select FOUND_ROWS(). This is sometimes useful if you want to know how many total results there are so you can calculate the page count.
Example:
select SQL_CALC_FOUND_ROWS ID, Title, Author
from yourtable
limit 0, 10;
SELECT FOUND_ROWS();
From the manual:
http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_found-rows
The usual way of counting in a query is to group on the fields that are returned:
select ID, Title, Author, count(*) as Cnt
from ...
group by ID, Title, Author
order by Title
limit 1, 10
The Cnt column will contain the number of records in each group, i.e. for each title.
Regarding second query:
select tbl.id, tbl.title, tbl.author, x.cnt
from tbl
cross join (select count(*) as cnt from tbl) as x
If you will not join to other table(s):
select tbl.id, tbl.title, tbl.author, x.cnt
from tbl, (select count(*) as cnt from tbl) as x
My Solution:
SELECT COUNT(1) over(partition BY text) totalRecordNumber
FROM (SELECT 'a' text, id_consult_req
FROM consult_req cr);
If your problem is simply the speed/cost of doing a second (complex) query I would suggest you simply select the resultset into a hash-table and then count the rows from there while returning, or even more efficiently use the rowcount of the previous resultset, then you do not even have to recount
This will add the total count on each row:
select count(*) over (order by (select 1)) as Cnt,*
from yourtable
Here is your answare:
SELECT *, #cnt count_rows FROM (
SELECT *, (#cnt := #cnt + 1) row_number FROM your_table
CROSS JOIN (SELECT #cnt := 0 AS variable) t
) t;
You simply cannot do this, you'll have to use a second query.