Sum of grouped COUNT IN MySQL - mysql

I wrote this query
SELECT
country,
COUNT(DISTINCT tmp_tbl.user_guid) AS number_of_customers
FROM complete_tests c INNER JOIN
( SELECT DISTINCT d.dog_guid,
u.user_guid,
u.country
FROM dogs d INNER JOIN users u ON d.user_guid = u.user_guid
WHERE (u.exclude = 0 OR u.exclude IS NULL)
AND (d.exclude = 0 OR d.exclude IS NULL)
)
AS tmp_tbl ON c.dog_guid = tmp_tbl.dog_guid
GROUP BY country
ORDER BY number_of_customers DESC
And I need to add another variable that calculates the percentage of total
when I add
number_of_customers/SUM(number_of_customers)
or SUM(COUNT(DISTINCT tmp_tbl.user_guid)) / COUNT(DISTINCT tmp_tbl.user_guid)
it gives me error

Analytic functions come in handy here. Assuming you are using MySQL 8+:
SELECT country,
COUNT(DISTINCT tmp_tbl.user_guid) AS number_of_customers,
100.0 * COUNT(DISTINCT tmp_tbl.user_guid) /
SUM(COUNT(DISTINCT tmp_tbl.user_guid)) OVER () AS pct_customers
FROM complete_tests c
INNER JOIN
(
SELECT DISTINCT d.dog_guid, u.user_guid, u.country
FROM dogs d
INNER JOIN users u ON d.user_guid = u.user_guid
WHERE (u.exclude = 0 OR u.exclude IS NULL) AND
(d.exclude = 0 OR d.exclude IS NULL)
) AS tmp_tbl
ON c.dog_guid = tmp_tbl.dog_guid
GROUP BY
country
ORDER BY
number_of_customers DESC;

Related

I need to get specific ids from db if these are in current and last quarter using SQL

[DB Table]
SELECT b.first_name, b.last_name, a.pod_name, a.category, c.user_id,
SUM(IF(QUARTER(CURDATE())-1 OR (QUARTER(CURDATE())-2) AND a.user_id, 1, 0)) AS flag FROM kudos a
INNER JOIN users b ON a.user_id = b.id INNER JOIN users_groups c ON a.user_id = c.user_id
INNER JOIN groups d ON c.group_id = d.id WHERE a.group_name = 'G2' AND d.id IN (7,8,9,11,12,13,14,15,16,17,21,22,23,24,25,26,27,28)
AND QUARTER(CURDATE())-1 = a.quarter ORDER BY a.final_score+0 DESC
I need to get the user_ids of those users which are both in quarter 1 and 2 from table.
Tried above query but failed to get expected results.
Can someone please guide me on this?
if you only need user_id then you can do this :
select user_id
from tablename
where quarter in (1,2)
group by user_id
having count(distinct quarter) = 2
another way is to use window function, assuming you have one user id in each quarter:
select * from (
select * , count(*) over (partition by user_id) cn
from tablename
where quarter in (1,2)
) t where cn = 2

MySQL include all results in ranking

I'm using the following query to sort, using Order by for 4 different ranks: id, avg_rating, total_sent & points
However, I'm having trouble with including all the members in the results. I'd like to include all members, including the ones that have 0: total_sent, total_received, points, avg_rating, votes
Please help me understand what I'm missing. Thank you.
SELECT m.id,
m.Name,
m.City,
m.Zip_Code,
m.url,
r.avg_rating,
r.votes,
froms.from_ct total_sent,
tos.to_ct total_received,
froms.from_ct - tos.to_ct `points`
FROM members m
JOIN (
SELECT id_rated,
avg(rating) avg_rating,
count(*) votes
FROM member_ratings
GROUP BY id_rated
) r ON r.id_rated = m.id
JOIN ( SELECT id_from, COUNT(*) AS from_ct FROM member_points GROUP BY 1
) AS froms ON froms.id_from = m.id
JOIN ( SELECT id_received, COUNT(*) AS to_ct FROM member_points GROUP BY 1
) AS tos ON tos.id_received = m.id
WHERE m.Account_Active = 'TRUE'
GROUP BY m.id,
m.Name
ORDER BY `avg_rating` DESC;
Use LEFT JOINs for all tables
SELECT m.id,
m.Name,
m.City,
m.Zip_Code,
m.url,
r.avg_rating,
r.votes,
froms.from_ct total_sent,
tos.to_ct total_received,
froms.from_ct - tos.to_ct `points`
FROM members m
LEFT JOIN (
SELECT id_rated,
avg(rating) avg_rating,
count(*) votes
FROM member_ratings
GROUP BY id_rated
) r ON r.id_rated = m.id
LEFT JOIN ( SELECT id_from, COUNT(*) AS from_ct FROM member_points GROUP BY 1
) AS froms ON froms.id_from = m.id
LEFT JOIN ( SELECT id_received, COUNT(*) AS to_ct FROM member_points GROUP BY 1
) AS tos ON tos.id_received = m.id
LEFT JOIN member_points mp ON mp.id_points = m.id
WHERE m.Account_Active = 'TRUE'
GROUP BY m.id,
m.Name
ORDER BY `avg_rating` DESC;

Merge 2 SQL Queries/Tables

I spent so much time googling today but i don't even know which keywords to use. So …
The project is an evaluation of a betting game (Football). I have 2 SQL Queries:
SELECT players.username, players.userid, matchdays.userid, matchdays.points, SUM(points) AS gesamt
FROM players INNER JOIN matchdays ON players.userid = matchdays.userid AND matchdays.season_id=5
GROUP BY players.username
ORDER BY gesamt DESC
And my second query:
SELECT max(matchday) as lastmd, points, players.username from players INNER JOIN matchdays ON players.userid = matchdays.userid WHERE matchdays.season_id=5 AND matchday=
(select max(matchday) from matchdays)group by players.username ORDER BY points DESC
The first one adds up the points of every matchday and shows the sum.
The second shows the points of the last gameday.
My Goal is to merge those 2 queries/tables so that the output is a table like
Rank | Username | Points last gameday | Overall points |
I don't even know where to start or what to look for. Any help would be appreciated ;)
use both query with join....use inner join if each userid have value in 2nd query also.also add userid in 2nd query also for join
SET #rank = 0;
SELECT #rank := rank + 1,
t1.username,
t2.points,
t1.gesamt
FROM (
SELECT players.username, players.userid puserid, matchdays.userid muserid, matchdays.points, SUM(points) AS gesamt
FROM players INNER JOIN matchdays ON players.userid = matchdays.userid AND matchdays.season_id=5
GROUP BY players.username
)t1
INNER JOIN
(
SELECT players.userid, max(matchday) as lastmd, points, players.username
from players INNER JOIN matchdays ON players.userid = matchdays.userid
WHERE matchdays.season_id=5 AND matchday=
(select max(matchday) from matchdays)group by players.username
)t2
ON t1.puserid = t2.userid
ORDER BY t1.gesamt
You can use conditional aggregation, i.e. sum the points only when the day is the last day:
SELECT
p.username,
SUM(case when m.matchday = (select max(matchday) from matchdays) then m.points end)
AS last_day_points,
SUM(m.points) AS total_points
FROM players p
INNER JOIN matchdays m ON p.userid = m.userid AND m.season_id = 5
GROUP BY p.userid
ORDER BY total_points DESC;
Or with a join instead of a non-correlated subquery (MySQL should come to the same execution plan):
SELECT
p.username,
SUM(case when m.matchday = last_day.matchday then m.points end) AS last_day_points,
SUM(m.points) AS total_points
FROM players p
INNER JOIN matchdays m ON p.userid = m.userid AND m.season_id = 5
CROSS JOIN
(
select max(matchday) as matchday
from matchdays
) last_day
GROUP BY p.userid
ORDER BY total_points DESC;

sql with one count() another count() in LEFT JOIN

having:
SELECT *, COUNT(MakeModel) as `count`
FROM (`table_a`)
GROUP BY `MakeModel`
ORDER BY `count` DESC
Works fine for me (count = 2)
extend this query with a LEFT JOIN like this:
SELECT *, COUNT(distinct b.rating) as ratings
FROM (`table_a`)
LEFT JOIN `model_ratings` AS b ON `b`.`MakeModel` = `table_a`.`MakeModel`
GROUP BY `table_a`.`MakeModel`
Also works fine (ratings = 3),
but combine the 2 counts like this:
SELECT *, COUNT(table_a.MakeModel) as `count`, COUNT(distinct b.rating) as ratings
FROM (`table_a`)
LEFT JOIN `model_ratings` AS b ON `b`.`MakeModel` = `table_a`.`MakeModel`
GROUP BY `table_a`.`MakeModel`
ORDER BY `count` DESC
then (count= 6 ) and (ratings = 3).
So count becomes ('count' times 'ratings').
But I need (count= 2 ) and (ratings = 3)
What am I missing here?
Found out by myself
COUNT() and GROUP inside the LEFT JOIN first
SELECT *, COUNT(table_a.MakeModel) as `count`, b.rating as ratings
FROM (`table_a`)
LEFT JOIN (
SELECT MakeModel, COUNT(distinct rating)
FROM `model_ratings`
GROUP BY MakeModel
) AS b ON `b`.`MakeModel` = `table_a`.`MakeModel`
GROUP BY `table_a`.`MakeModel`
ORDER BY `count` DESC

Mysql query distinct + multiple

I want to do this query:
SELECT *
FROM user k
INNER JOIN (
SELECT id, tagName, b.guid, name, owner, publicKey
FROM noteTags a
INNER JOIN (
SELECT *
FROM note
ORDER BY guid
LIMIT 0 , 12
)b ON a.guid = b.guid ORDER BY b.id DESC
)l ON k.owner = l.owner
But I want it to return DISTINCT b.guids.
Structure of the tables:
note
|
|=id
|=name
|=guid
|=owner
|=publicKey
noteTags
|
|=guid
|=tagName
user
|
|=owner
|=username
|=auth
Basically I want to select ALL data (with the limit on the deeper inner join) and return DISTINCT guids
Thanks!
Here's my initial answer,
SELECT *
FROM note a
INNER JOIN user b
ON a.owner = b.owner
INNER JOIN notetags c
ON a.guid = b.guid
INNER JOIN
(
SELECT guid, MAX(tagName) maxTag
FROM notetags
GROUP BY guid
) d ON c.guid = d.guid AND
c.tagName = d.maxTag
How about:
SELECT *
FROM user k
INNER JOIN (
SELECT id, tagName, b.guid, name, owner, publicKey
FROM noteTags a
INNER JOIN (
select id, name, MIN(guid) as guid, owner, publicKey
FROM note
GROUP BY guid
LIMIT 0 , 12
)b ON a.guid = b.guid ORDER BY b.id DESC
)l ON k.owner = l.owner