SUMing the results of a COUNT() in MySQL - mysql

Is a UNION query required to achieve the following. I have a table with data that looks like this:
some_id | some_date
--------------------
5 | 2016-04-03
3 | 2016-04-03
2 | 2016-04-03
5 | 2016-04-03
I'd like to get the total number of times we've seen any and all ID for the date 2016-04-03. So the SUM would be 3 here, with 5 having a count of 2, 3 having a count of 1, 2 having a count of 1.
Is a UNION required to make this work?
This is using MySQL 5.6+

Use COUNT(DISTINCT):
SELECT COUNT(DISTINCT some_id)
FROM YourTable
WHERE some_date = '2016-04-03'

I do not think this requires a UNION
SELECT some_id, SUM(1) as cnt, some_date
FROM some_table
GROUP BY some_id, some_date
WITH ROLLUP
Here is a test.

Related

How do I count the number of rows that have different dates

I want to count the total numbers of row in SQL database.
"tableA" :
id | date |
---+------------+
1 | 2019-09-03 |
2 | 2019-09-03 |
3 | 2019-09-04 |
4 | 2019-09-05 |
I want to execute it as new column name "total" that should have like this :
total
-----
3
because they are 3 different dates.
I know that the result must use 2 query like this:
SELECT date
FROM tableA
GROUP BY date AS total;
SELECT COUNT(total)
FROM tableA;
How to combine 2 queries like that or there is another way?
Use count(distinct date)
SELECT COUNT(distinct date) FROM tableA
select count( distinct date) as total from tableA;

Get the sum of a column along with the records

I have a table of the type:
id | amount
-----------
1 | 10
2 | 5
3 | 7
I would like to get the rows records plus the sum of 'amount' column all in 1 query, something like this:
1 10 22
2 5 22
3 7 22
I am trying:
SELECT *, sum(amount)
FROM table GROUP BY id
But it is not working. Any ideas how could I solve this? Is it possible? Is it efficient for a very large database? Thanks
Try this,
SELECT t.*, (select sum(amount) from tbl t) as total_amount
FROM table t1 GROUP BY t1.id

MySQL - count records and list people

I have table with results:
date | user_id | content
-----------------------------------------
2017-01-14 | 1 | lorem
2017-01-02 | 2 | dsfdf
2017-01-02 | 1 | asfds dsfsda
2017-01-27 | 3 | sdfdfds fsdf
And I want count row for all users and receive result like this:
user_id | count
-----------------------------------------
1 | 2
2 | 1
3 | 1
I try:
select distinct(user_id), count(*) from aso_repairs where date like '2017-01-%'
But this don't work ;-( Any help?
use the GROUP BY clause:
SELECT
user_id,
count(user_id)
FROM aso_repairs
WHERE
date LIKE '2017-01-%'
GROUP BY user_id
You shouldn't use count(*), at least in MySQL 3.23 this is far more expensive than count(somecolumn) although it yields the same result.
Add the missing group by clause:
SELECT
user_id, COUNT(*)
FROM
aso_repairs
WHERE
date LIKE '2017-01-%'
GROUP BY user_id;
Also, the like operator won't let you use any index if there was one on the date column. Consider providing the actual date boundaries:
SELECT
user_id, COUNT(*)
FROM
aso_repairs
WHERE
date BETWEEN '2017-01-01' AND '2017-01-31'
GROUP BY user_id;

Select all rows with multiple occurrences

Ok, I have a single MySQL table with the name 'test' and 3 columns.
ID | playername | lastloginip
-----------------------------
1 | user 1 | 1
2 | user 2 | 2
3 | user 3 | 3
4 | user 4 | 4
5 | user 5 | 5
6 | user 6 | 1
7 | user 7 | 1
8 | user 8 | 2
Now, I would like to select ALL the rows where the lastloginip is found multiple times in the table, and then give the count of those rows.
In this case, it should return the number 5
as user 1, 2, 6, 7 and 8 have a lastloginip that is found multiple times.
I already tried using
SELECT COUNT(*)
FROM (
SELECT *
FROM test
GROUP BY lastloginip
HAVING COUNT(*) > 1
) t
But that gave back the number 2 instead of 5.
I am not sure how to set up this query correctly. Most of my findings on the internet keep showing only 2 rows or giving the number 2 instead of 5.
First COUNT(), then SUM():
SELECT SUM(occurences)
FROM
(
SELECT COUNT(*) AS occurences
FROM test
GROUP BY lastloginip
HAVING COUNT(*)>1
) t
Try this query.
SELECT SUM(loginip)
FROM(
SELECT
lastloginip,
COUNT(lastloginip)
as loginip
FROM test
GROUP BY lastloginip
HAVING COUNT(ID)>1
)t
You can fetch the sum of occurrences using the above code and if you want to view the records with multiple occurences, refer to the query below-
Select *
from test
where lastloginip in (
select *
from
(select lastloginip
from test
group by lastloginip
having count(lastloginip)>1
)
as a)

Mysql multi count in one query

How can I count records for two columns in one table using different query criteria?
Table looks like:
user_id | date | status
------------------------------
1 | 2011-01-02 | 1
2 | 2011-01-03 | 1
3 | 2011-01-02 | 0
4 | 2011-01-03 | 1
1 | 2011-01-02 | 1
I want to count two values in one query. The first one is number of user_id group by status and the second is count of user_id group by date.
How can I do that?
You can't have different GROUP BY clauses in the same query -- each count will have to be in an independent query.
But you can return the output in a single query/resultset using subselects (subquery in the SELECT clause):
SELECT COUNT(a.user_id) AS numUsersPerStatus,
(SELECT COUNT(b.user_id)
FROM YOUR_TABLE b
GROUP BY b.date) AS numUsersPerDate
FROM YOUR_TABLE a
GROUP BY a.status
You don't.
You should use two queries. There's no advantage to doing this with a single query.
If you really want to do it you can try this:
SELECT 'date' AS grptype, date AS grp, COUNT(DISTINCT user_id) AS cnt
FROM yourtable
GROUP BY date
UNION ALL
SELECT 'status' AS grptype, status AS grp, COUNT(DISTINCT user_id) AS cnt
FROM yourtable
GROUP BY status
Result:
grptype grp cnt
date 2011-01-02 2
date 2011-01-03 2
status 0 1
status 1 3
However I would strongly advise against doing this. You want two different and unrelated result sets so you should use two separate queries.