ONE SQL query to count records on different conditions - mysql

How can I count the total number of records, the number of unique of users and number of records on which the status is 2 from the table participants in one query?
I know how to accomplish this using 3 separate queries:
SELECT COUNT() FROM participants
SELECT COUNT() FROM participants GROUP BY user
SELECT COUNT(*) FROM participants WHERE status = 2
But this doesn't really seem efficient?
Table participants
id user status
10 john#example.com 1
11 john#example.com 1
12 john#example.com 1
13 sally#mailing.com 1
14 sally#mailing.com 2
15 edward#halloworld.com 1
16 edward#halloworld.com 1
17 edward#halloworld.com 2
18 mike#bestmail.net 2
19 mike#bestmail.net 1
29 nat#worldcom.com 0

Just use conditional aggregation:
select count(*) as numrecords, count(distinct user) as numusers,
sum(status = 2) as numstatus_2
from participants p;

Since you want just one result (per requirement), you don't need a group by clause at all, and all of these requirements can be created as arguments for the count function:
SELECT COUNT(*) AS total_records,
COUNT(DISTINCT user) AS distinct_users_count,
COUNT(CASE status WHEN 2 ELSE NULL END) AS status_2_count
FROM participants

Related

Mysql Count distinct IDs in Sum(if())

I have following tables.
Case
ID
Name
1
Anton
2
Elfriede
3
Osram
Appointments
ID
Duration
Case
1
70
1
2
70
2
Fastdocu with out appointment
ID
Duration
Case
1
15
2
2
15
2
3
50
3
3
8
3
I need sum up all duration and all appointments and the cases that are not in Appointments.
The result should be
Duration 88
Appointments 3
Cases 1
Following Statement I have written
Select sum(duration), count(DISTINCT ID), SUM(IF(`Case` NOT IN (1,2),1,0)) From Fastdocu
Suddenly the result is
Duration 88
Appointments 3
Cases 2
I must sum only distinct ids at the cases count. But the distinct seems to be forbidden in the if.
How can I count the cases right?
Use COUNT() with a CASE expression instead of SUM():
SELECT SUM(duration) total_duration,
COUNT(DISTINCT ID) distinct_ids,
COUNT(DISTINCT CASE WHEN `Case` NOT IN (1,2) THEN `Case` END) cases_not_in_Appointments
FROM Fastdocu
Since COUNT() does not have an ELSE branch it will not count Cases that do not satisfy the condition Case NOT IN (1,2) because in this case the result of the CASE expression will be NULL.
If you want to count the distinct ids and not the distinct Cases change to:
COUNT(DISTINCT CASE WHEN `Case` NOT IN (1,2) THEN ID END)
See the demo.
Results:
total_duration
distinct_ids
cases_not_in_Appointments
88
3
1

configure query to bring rows which have more than 1 entries

How to get those entries which have more than 1 records?
If it doesn't make sense... let me explain:
From the below table I want to access the sum of the commission of all rows where type is joining and "they have more than 1 entry with same downmem_id".
I have this query but it doesn't consider more entries scenario...
$search = "SELECT sum(commission) as income FROM `$database`.`$memcom` where type='joining'";
Here's the table:
id mem_id commission downmem_id type time
2 1 3250 2 joining 2019-09-22 13:24:40
3 45 500 2 egbvegr new time
4 32 20 2 vnsjkdv other time
5 23 2222 2 vfdvfvf some other time
6 43 42 3 joining time
7 32 353 5 joining time
8 54 35 5 vsdvsdd time
Here's the expected result: it should be the sum of the id no 2, 7 only
ie. 3250+353=whatever.
It shouldn't include id no 6 because it has only 1 row with the same downmem_id.
Please help me to make this query.
Another approach is two levels of aggregation:
select sum(t.commission) income
from (select sum(case when type = 'joining' then commission end) as commission
from t
group by downmem_id
having count(*) > 1
) t;
The main advantage to this approach is that this more readily supports more complex conditions on the other members of each group -- such as at most one "joining" record or both "joining" records and no more than two "vnsjkdv" records.
Use EXISTS:
select sum(t.commission) income
from tablename t
where t.type = 'joining'
and exists (
select 1 from tablename
where id <> t.id and downmem_id = t.downmem_id
)
See the demo.
Results:
| income |
| ----- |
| 3603 |
You can use subquery that will find all downmem_id having more than one occurrence in the table.
SELECT Sum(commission) AS income
FROM tablename
WHERE type = 'joining'
AND downmem_id IN (SELECT downmem_id
FROM tablename t
GROUP BY downmem_id
HAVING Count(id) > 1);
DEMO

Sql select where a column has been set atleast once

I have this table
**applications**
id user_id company_id shortlisted
1 10 99 0
2 10 100 1
3 10 101 1
4 10 102 0
5 11 99 1
6 12 99 0
6 12 101 0
What I want is to select all users
which have been shortlisted at-least once
which have not been shortlisted at all
For the first case, i have the following query:
SELECT user_id
from applications
where shortlisted=1
Group
By user_id
and this gives me the expected result like below
**applications**
user_id
10
11
But I'm trying the following query for the second case and it returns me an empty set:
Select user_id
from applications as Application
where shortlisted=0
and NOT EXISTS(Select user_id from applications where user_id=Application.user_id and shortlisted=1)
What am i missing?
PS: Please ignore any typos as i typed them manually for this post.
To get both results in a single query simply use aggregation:
select user_id, max(shortlisted) as was_shortlisted
from applications
group By user_id
You can use group by and having for both.
For the first:
select user_id
from applications
group By user_id
having max(shortlisted) = 1;
For the second:
select user_id
from applications
group By user_id
having max(shortlisted) = 0;
In all honesty, your version with the where is more efficient for the first query. This is just to show how closely related the queries are.
You can try following query;
select user_id from table1
group by user_id having MIN(shortlisted) = 1
This will give you to at least have shortlisted = 1 condition and don't have shortlisted = 0 records.

Sum values in mysql table where userid is identical

I have read the different answers here on SO, but I am stuck on this question. Please help.
I have this mysql view named "activeuser":
userid COUNT(*) ACRONYM
1 23 admin
2 2 doe
3 4 tompa
12 4 Marre
13 1 Mia
1 2 admin
3 1 tompa
12 1 Marre
13 1 Mia
2 1 doe
3 1 tompa
12 1 Marre
How can I sum the COUNT column so that I get the following wanted result?
userid COUNT(*) ACRONYM
1 25 admin
2 3 doe
3 6 tompa
12 6 Marre
13 1 Mia
EDITED:
I used this query to create the view:
CREATE VIEW activeuser AS
(SELECT boats_comments.userid, COUNT(boats_comments.userid), boats_user.acronym, boats_user.email
FROM boats_comments
INNER JOIN boats_user
ON boats_comments.userid = boats_user.id
GROUP BY boats_comments.userid
ORDER BY COUNT(boats_comments.userid) DESC)
UNION ALL
(SELECT boats_answers.userid, COUNT(boats_answers.userid), boats_user.acronym, boats_user.email
FROM boats_answers
INNER JOIN boats_user
ON boats_answers.userid = boats_user.id
GROUP BY boats_answers.userid
ORDER BY COUNT(boats_answers.userid) DESC)
UNION ALL
(SELECT boats_questions.userid, COUNT(boats_questions.userid), boats_user.acronym, boats_user.email
FROM boats_questions
INNER JOIN boats_user
ON boats_questions.userid = boats_user.id
GROUP BY boats_questions.userid
ORDER BY COUNT(boats_questions.userid) DESC)
My goal is to see which users are the most active by checking the number of comments, questions and answers... but I got stuck...
As the results in your view has duplicates I guess the underlying code for the view is grouping on something it maybe shouldn't be grouping on.
You can get the results you want by applying SUM to it:
select userid, sum("whatever column2 is named") as "Count", Acronym
from activeuser group by userid, Acronym;
select userid, count(*) from activeuser group by userid;

Select User Having Maximum Rating

I have a table which stores the rating given by the user.
eg:
UserId Rating
3 1
3 2
1 1
1 2
1 3
1 4
2 1
2 23
2 4
I need to retrieve the 10 users who have rated the maximum number of times.
eg:
1 rated 4 times
2 rated 3 times
3 rated 2 times...
Any idea how to write a query using mysql?
You have to group by userID, count the grouped rows, and order by the count in descending order, then you limit the query to 10 rows:
SELECT userID, count(*) times
FROM users
GROUP BY userID
ORDER BY times DESC
LIMIT 10
If you need the output exactly as shown above, try this:
SELECT CONCAT_WS(' ', userID, 'rated', count(*), 'times')
FROM users
GROUP BY userID
ORDER BY count(*) DESC
LIMIT 10
See this fiddle.