Select distinct and count number of rows - mysql

I have a table with this structure
+----------+----------+
| user_id | tema_id |
+----------+----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 2 |
| 6 | 3 |
| 7 | 1 |
+----------+----------+
What I want to get in only one query is the total of different tema_id by tema_id. I have this query but it returns the different tema_id but the count column to one instead of the total of that tema_id.
SELECT tema_id, COUNT(DISTINCT(tema_id)) as total
FROM push_subscriptions
GROUP BY tema_id
Return this:
+----------+----------+
| tema_id | total |
+----------+----------+
| 1 | 1 | -> must be 3
| 2 | 1 | -> must be 2
| 3 | 1 | -> must be 2
+----------+----------+
Thank you

A simple count(*) should do the trick:
select tema_id, count(*) as total
from push_subscriptions
group by tema_id;
Fiddle

Related

How to work out total distinct values minus grouped distinct values in MySQL

Given the sample table of:
+----+----------+---------+------+
| id | userName | storeId | cost |
+----+----------+---------+------+
| 1 | foo | 1 | 10 |
| 2 | bar | 1 | 10 |
| 3 | baz | 5 | 5 |
| 4 | baz | 3 | 20 |
| 5 | qux | 1 | 5 |
| 6 | qux | 4 | 20 |
| 7 | qux | 15 | 30 |
| 8 | qux | 17 | 40 |
| 9 | qux | 3 | 5 |
| 10 | quux | 6 | 20 |
+----+----------+---------+------+
I would like to work out how many people purchased at each store and how many did not. I want the report to display the results grouped by store.
I know the statement select storeId, count(distinct username) as total from purchases group by storeId provides me with how many people purchased in each store, but I want to subtract the result of the query select count(distinct userName) from purchases; in another column. I would expect the sample output to display as follows.
+---------+-----------+--------------+
| storeId | purchased | notPurchased |
+---------+-----------+--------------+
| 1 | 3 | 2 |
| 3 | 2 | 3 |
| 4 | 1 | 4 |
| 5 | 1 | 4 |
| 6 | 1 | 4 |
| 15 | 1 | 4 |
| 17 | 1 | 4 |
+---------+-----------+--------------+
You can use NOT condition with IN() function
As long a subse3lect gives back only only one,
you can use following
SELECT
storeId,
COUNT(DISTINCT username) AS total,
((SELECT
COUNT(DISTINCT userName)
FROM
purchases) - COUNT(DISTINCT username)) notPurchased
FROM
purchases
GROUP BY storeId
storeId | total | notPurchased
------: | ----: | -----------:
1 | 3 | 2
3 | 2 | 3
4 | 1 | 4
5 | 1 | 4
6 | 1 | 4
15 | 1 | 4
17 | 1 | 4
db<>fiddle here

Join 2 tables with distinct

I have 2 tables, follow and following
follow
+---------+----------------+
| user_id | follow_user_id |
+---------+----------------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 100 | 10 |
+---------+----------------+
following
+---------+-------------------+
| user_id | following_user_id |
+---------+-------------------+
| 1 | 2 |
| 3 | 4 |
| 4 | 6 |
| 200 | 500 |
+---------+-------------------+
I want to concat 2 tables without duplicate.
Here is the result that I want.
+---------+----------------+-----------+
| user_id | target_user_id | category |
+---------+----------------+-----------+
| 1 | 2 | follow |
| 2 | 3 | follow |
| 3 | 4 | follow |
| 4 | 6 | following |
| 100 | 10 | follow |
| 200 | 500 | following |
+---------+----------------+-----------+
Condition 1 - Remove duplicated row
Condition 2 - Have to add category column with each table's name
Condition 3 - If category is duplicated, it can be follow or following. it doesn't matter.
Condition 4 - follow_user_id as target_user_id and following_user_id as target_user_id
In this case, should I have to use join? or union?
Any suggestion, very appreciate.
Thanks!
Just use union and group by, the SQL as below:
select
user_id,target_user_id,min(tag) as category
from
(
select user_id,follow_user_id as target_user_id, 'follow' as tag from follow
union
select user_id,following_user_id as target_user_id, 'following' as tag from following
) tmp
group by
user_id,target_user_id
order by
user_id,target_user_id;
+---------+----------------+-----------+
| user_id | target_user_id | category |
+---------+----------------+-----------+
| 1 | 2 | follow |
| 2 | 3 | follow |
| 3 | 4 | follow |
| 4 | 6 | following |
| 100 | 10 | follow |
| 200 | 500 | following |
+---------+----------------+-----------+
6 rows in set (0.00 sec)

MYSQL group by for separate id

So here is the structure and query: http://www.sqlfiddle.com/#!9/de7ec8/4
And this is the result:
+-------------+--------+
| id_requests | status |
+-------------+--------+
| 1 | 0 |
| 1 | 0 |
| 1 | 1 |
| 2 | 1 |
+-------------+--------+
4 rows in set
and im looking for this result:
+-------------+--------+
| id_requests | status |
+-------------+--------+
| 1 | 0 |
| 1 | 1 |
| 2 | 1 |
+-------------+--------+
3 rows in set
I want to group by status but for each id_requests, any help will be appreciated.
Seems you are looking for selecting distinct rows for this use distinct clause
SELECT distinct a.id_requests, b.status
FROM `admin_availability_requests` a

Mysql : select count when id have multiple same value

+------+---------+
| id | object |
+------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
| 3 | 3 |
| 3 | 4 |
+------+---------+
i want to select count id where have a same value, so the result be, id 1 have 4 same value, id 2 have 2 same value, id 3 have 3 same value .
+------+
| id |
+------+
| 4 |
| 2 |
| 3 |
+------+
thanks for help, master.
SELECT id, COUNT(object) FROM tablename GROUP BY id
SELECT COUNT( * ) FROM `test` GROUP BY id

Get the greatest number of the products

The goal
I want to make a ranking and I need to get the 10 firsts of a column.
The problem
There's the following table within my application:
+------+---------+
| User | Product |
+------+---------+
| 1 | 1 |
+------+---------+
| 2 | 1 |
+------+---------+
| 3 | 1 |
+------+---------+
| 4 | 1 |
+------+---------+
| 5 | 2 |
+------+---------+
| 6 | 2 |
+------+---------+
| 7 | 2 |
+------+---------+
| 8 | 3 |
+------+---------+
| 9 | 3 |
+------+---------+
And I want to make a ranking following this pattern:
+---------+----------+
| Product | Quantity |
+---------+----------+
| 1 | 4 |
+---------+----------+
| 2 | 3 |
+---------+----------+
| 3 | 2 |
+---------+----------+
How can I do this?
You can do this:
SELECT product, COUNT(product)
FROM yourtable
GROUP BY product
ORDER BY COUNT(product) DESC
LIMIT 10;
Something like this should do it.
SELECT TOP 10 Product, COUNT(*)
FROM Table
GROUP BY Product
ORDER BY COUNT(*) DESC;