Count unique users from db - mysql

I have the following table structure in my db (MySQL):
id group_id item_id project_id user_id
Users can have multiple entries withing the same project. How do I count unique users withing a particular project (minus project owner id)?
SELECT COUNT(user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3
GROUP BY user_id
This looks right but I don't believe I'm getting the right results. Am I missing something?

Select Count(Distinct user_id)
From MyTable
Where project_id = $myProject
And user_id != 3

Add DISTINCT to your COUNT and eliminate the GROUP BY.
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3

You don't need a GROUP BY clause for this.
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3;
If you want to list the member count for each group in the same query, you can GROUP BY project_id:
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
GROUP BY project_id;
By grouping on user_id as you do now, every row in the resultset will contain 1.

Try Distinct?
SELECT DISTINCT COUNT(user_id) AS cnt FROM myTable WHERE project_id = $myProject AND user_id != 3

Related

How to exclude specific duplicates from MySQL group_by dataset?

I'm using MySQL 5.7.12. I have a simple query that looks like the following:
SELECT COUNT(id) as count, pic_id
FROM user_pictures
GROUP BY pic_id
I would like to exclude all the duplicate USER_ID records from grouping. So I did the following, however, it's not returning any result:
SELECT COUNT(id) as count, pic_id
FROM user_pictures
GROUP BY pic_id
HAVING count(user_id) = 1
What am I missing here?
Use DISTINCT so it doesn't count duplicates of the same user_id:
SELECT COUNT(id) as count, pic_id
FROM user_pictures
GROUP BY pic_id
HAVING count(DISTINCT user_id) = 1

Count and group by in same query

bid_count is returning 1 ... how do I get it to return the correct bid count? I know that it's not returning the right count because I'm grouping by user_id. Can I alter the query so it counts also.
"SELECT id, bid, item_id, user_id, MAX(bid) AS max_bid, COUNT(bid) AS bid_count, bid_date
FROM bids
WHERE item_id = ?
GROUP BY user_id ORDER BY id DESC"
You shouldn't use unaggregated expressions in GROUP BY unless they are all same within the group.
If you want to return the record holding the counts by user_id, you should use an subquery to get the counts then join them to the result:
SELECT id, bid, item_id, user_id, bid_date, max_bid, bid_count
FROM bids
LEFT JOIN
(SELECT user_id,MAX(bid) AS max_bid, COUNT(bid) AS bid_count
FROM bids
WHERE item_id = ?
GROUP BY user_id ) as group_table
ON bids.user_id = group_table.user_id
WHERE bids.item_id = ?
Try grouping it based on item_id. item_id should the one that has different bids.

MySQL Count all and Count distinct over 2 columns same query

Currently I have the below query to count the number of rows with a distinct (customer username and store id) of the purchase of a certain item. How can I also count the total(all) rows where item_id = 3 in the same query?
SELECT count(*)
FROM (
SELECT DISTINCT customer_username, store_id
FROM transactions
WHERE item_id = 3)
You might try something like this:
SELECT COUNT(*), SUM(customer_cnt) FROM (
SELECT customer_username, store_id, COUNT(*) AS customer_cnt
FROM transactions
WHERE item_id = 3
GROUP BY customer_username, store_id
) t;
Try removing DISTINCT in your query:
SELECT count(*)
FROM (
SELECT customer_username, store_id
FROM transactions
WHERE item_id = 3)
Another way is a cross join of 2 inline views:
select x.distinct_rows, y.total_rows
from (select count(distinct concat(user_name, store_id)) as distinct_rows
from transactions
where item_id = 3) x
cross join (select count(*) as total_rows
from transactions
where item_id = 3) y

SQL: select all the rows from the two userid's with great number of rows

I would like to get all the rows from the two users with the greatest number of rows, that is, the two users with the greatest activity in a log table.
I have only found next solution: first, get the number of rows for every user, an limit it to 2:
SELECT userid, count(*) AS n_of_rows FROM my_table GROUP BY userid LIMIT 2;
Then, from the source code I'm querying the database (Python for example), query the database to get the rows of each user:
SELECT * FROM my_table where userid = $userid
Is it the best/elegant solution, taking into account SQL language itself and database performance?
Thanks!
I think what you're looking for is something like
select * from my_table where userid in
(select userid from my_table
group by userid
order by count(*) desc
limit 2)
To get the rows and keep the order, use a join with aggregation:
select t.*
from my_table t join
(select userid, count(*) as cnt
from my_table
group by userid
order by count(*) desc
limit 2
) top2
on t.userid = top2.userid
order by top2.cnt desc, userid;
Try this:
SELECT TOP 2 userid, count(*) AS n_of_rows
FROM my_table
GROUP BY userid
ORDER BY count(*) desc

How to find most repeating element from a table?

I have a table tbl_user and I want to find which user_name(s) have the most repeating values in the table. I only want to retrieve those user_id(s).
I also need a separate query to find the count the of duplicate user_name(s) in the table .
First query to find the biggest amount of repeated usernames:
SELECT user_name, COUNT(user_id) AS amount
FROM tbl_user
GROUP BY user_name
ORDER BY amount DESC
And then grab id`s:
# faster way using username from last query
SELECT user_id
FROM tbl_user
WHERE user_name = '$user_name'
# longer way
SELECT user_id
FROM tbl_user
WHERE user_name = (
SELECT user_name
FROM tbl_user
GROUP BY user_name
ORDER BY COUNT(amount) DESC
LIMIT 1
)
To find the count of duplicates:
SELECT count(id) AS num_duplicates FROM tbl_users GROUP BY user_name ORDER BY num_duplicates DESC
To find the user_id's of the most duplicated user_name:
SELECT user_id FROM tbl_user
WHERE username IN ( SELECT user_id
FROM tbl_users
GROUP BY user_name
ORDER BY count(id) DESC LIMIT 0, 1)
SELECT COUNT(*) FROM tbl_user
GROUP BY tbl_user.user_name
Will give you the number of duplicate user_names. You can use a scripting language to select out the highest one if you like, or just look at the list if it's not something you need automated.
If you mean you want to count the occurences of a particular username, something like this may work for you.
select user_name,user_id,count(*) as mycount from tbl_user group by user_name order by mycount desc limit 5
This should get the top 5 entries
You could select the user_names which are duplicated with this SQL:
SELECT COUNT(1) AS duplicates, user_name
FROM tbl_user
GROUP BY user_name
HAVING duplicates > 1
Having done this, you can then use it as a subquery to find out the count of duplicate user_names
SELECT COUNT(1) AS count_of_user_names, SUM(duplicates) AS duplicate_records
FROM (
SELECT COUNT(1) AS duplicates, user_name
FROM tbl_user
GROUP BY user_name
HAVING duplicates > 1
) AS subquery
Or use it to find out the user ids:
SELECT user_id
FROM tbl_user
WHERE user_name IN (
SELECT user_name
FROM tbl_user
GROUP BY user_name
HAVING COUNT(1) > 1
)