I have a column that takes user names. How can I count the number of instances of a users name. For example I have 10 rows and in column username i want to count all names that show up multiple times. I would like to build a list of the top contributors to my database. So if username alex shows up 5 times and jeff shows up 3 and april shows up 2 times i will count this and from that I can build my list.
Try GROUP BY:
SELECT username, COUNT(*) AS user_count
FROM yourtable
GROUP BY username
ORDER BY user_count DESC
Try something like
SELECT USER_NAME, COUNT(USER_NAME) FROM YOUR_TABLE GROUP BY USER_NAME;
If you want to get a count of all the usernames then you just do the following SQL:
Select Count(*) from tablename
If you want to get just the count of unique usernames
Select Count(*) from tablename Group by username
Related
The following query works great to count and get the total sum of visitors to my page.
SELECT id, COUNT(id) AS count
FROM visits
WHERE status <> 'test'
GROUP BY id
UNION ALL
SELECT 'SUM' id, COUNT(id)
FROM visits
WHERE status <> 'test'
But inside the visits table I have a column called IP where I store the IP of the visitors. I want to change the above query so that visits with the same IP are not counted (I only want to count unique IPs). How can I do it?
I have tried HAVING COUNT(IP) > 1 but it doesn't work.
Thanks!!!
In my database table, I have a value denoted userID. The value user ID may appear multiple times. I am trying to write a query that will either return all data in the row of that table if the value in userID appears 3 or more times. Alternatively, a query that return a list of all userIDs that appear 3 or more times.
Here is what I have now:
SELECT
*
FROM
myTable
WHERE
userID IN (SELECT
userID
FROM
myTable
GROUP BY userID
HAVING COUNT(*) > 2)
GROUP BY userID
This query kinda works, but some of the rows returned only have a single or double occurrence.
Any ideas on how I can modify this query so that it works?
You made things harder tnen they are, it's all simple:
SELECT
userID
FROM
myTable
GROUP BY userID
HAVING COUNT(userID) > 2)
This is if you only need ID's, else you should use this in place of SELECT statement in your WHERE part
Add the rest of your columns in the GROUP BY userID for all of the rows and youre super close with the list of userID's. This'll give you the UserID's:
SELECT userID FROM your_table
GROUP BY userID
HAVING COUNT(*) >= 3
and this will be for all:
SELECT
*
FROM
your_table
WHERE
userID IN (SELECT
userID
FROM
your_table
GROUP BY userID
HAVING COUNT(*) >= 3)
GROUP BY UserID, column_name1, column_name2
I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;
I would like to determine two things from a single query:
Most prevalent column in a table
The amount of times such column was located upon querying the table
Example Table:
user_id some_field
1 data
2 data
1 data
The above would return user_id # 1 as being the most prevalent in the table, and it would return (2) for the total amount of times that it was located in the table.
I have done my research and I came across two types of queries.
GROUP BY user_id ORDER BY COUNT(*) DESC
SUM
The problem is that I can't figure out how to use these two queries in conjunction with one another. For example, consider the following query which successfully returns the most prevalent column.
$top_user = "SELECT user_id FROM table_name GROUP BY user_id ORDER BY COUNT(*) DESC";
The above query returns "1" based on the example table shown above. Now, I would like to be able to return "2" for the total amount of times the user_id (1) was found in the table.
Is this by any chance possible?
Thanks,
Evan
You can include count(*) in the SELECT list:
SELECT user_id, count(*) as totaltimes from table_name
GROUP BY user_id ORDER BY count(*) DESC;
If you want only the first one:
SELECT user_id, count(*) as totaltimes from table_name
GROUP BY user_id ORDER BY count(*) DESC LIMIT 1;
I want to display the user with the most posts. the posts are added by counting how many times their username is displayed in the database. How can I grab all the elements and check to see which value appears more then others?
So say my database looks like this:
id | username
1 | test
2 | test
3 | no test
"test" is shown the most, so how could I say
highest poster: "test"
This query returns username and number of occurrences, sorted in reverse order, so the first record is the one with more occurrences:
select username, count(id) from tablename
group by username
order by count(id) desc
UPDATE:
As pointed by thedugas and Joe Phillips, you can add a limit 1 clause to this query to get only the record with the highest number of occurrences
select username, count(id) as uc
from tableName
group by username
order by uc desc
limit 1
SELECT username
FROM mytable
GROUP BY username
ORDER BY COUNT(1) DESC
LIMIT 1